Reading SRM Logs and Validating Array Pairing Issues using PowerCL

I Introduction: VMware Site Recovery Manager (SRM) is a disaster recovery and business continuity solution that helps protect virtualized environments. When utilizing SRM, it is important to ensure proper array pairing to ensure data replication and failover capabilities. In this article, we will explore how to use PowerCLI to read SRM logs and validate if there are any issues with array pairing. By automating this process, we can easily identify and troubleshoot array pairing problems, ensuring the reliability of our disaster recovery solution. 1. Establishing Connection to vCenter Server: To begin, establish a connection to the vCenter Server using the vSphere PowerCLI module in PowerShell. This will allow us to interact with the vCenter Server API and retrieve the necessary information. Use the following commands to connect to the vCenter Server:

# Import the vSphere PowerCLI module
Import-Module VMware.PowerCLI

# Connect to the vCenter Server
Connect-VIServer -Server <vCenter_Server_IP> -User <Username> -Password <Password>


Replace “, “, and “ with the appropriate values for your vCenter Server. 2. Retrieving SRM Logs: SRM logs contain valuable information about the replication and recovery process. We can use PowerCLI to retrieve SRM logs for analysis. The following command retrieves the SRM logs for a specific site:

$siteName = "SiteA" # Replace with the name of your SRM site
$logs = Get-Log -Name "srm-*.log" -Location "C:\ProgramData\VMware\VMware vCenter Site Recovery Manager\Logs\$siteName"


This script retrieves the SRM logs for the specified site and stores them in the `$logs` variable. 3. Parsing SRM Logs for Array Pairing Issues: To identify any array pairing issues, we need to parse the SRM logs for relevant error messages or warnings. PowerCLI provides the `Select-String` cmdlet, which allows us to search for specific patterns in text files. Here’s an example of how to parse the SRM logs for array pairing issues:

$pairingIssuePattern = "Array pairing issue" # Define the pattern to search for
$pairingIssues = $logs | Select-String -Pattern $pairingIssuePattern


In this script, we define the pattern to search for, such as “Array pairing issue”. We then use the `Select-String` cmdlet to search for matches of the pattern in the SRM logs stored in the `$logs` variable. Any matching lines will be stored in the `$pairingIssues` variable. 4. Validating Array Pairing Issues: Once we have identified any array pairing issues, we can validate them by checking the array pairing status in SRM. PowerCLI provides the `Get-SrmArrayPair` cmdlet, which retrieves information about the array pairing status. Here’s an example of how to validate array pairing issues:

$siteName = "SiteA" # Replace with the name of your SRM site
$arrayPairs = Get-SrmArrayPair -Site $siteName

if ($arrayPairs) {
    foreach ($arrayPair in $arrayPairs) {
        if ($arrayPair.State -ne "Connected") {
            Write-Host "Array pairing issue detected with $($arrayPair.ArrayManager.Name)"
        }
    }
} else {
    Write-Host "No array pairing information found."
}


In this script, we retrieve the array pairing information using the `Get-SrmArrayPair` cmdlet. We then iterate through each array pair and check if the state is “Connected”. If it is not, we output a message indicating an array pairing issue. 5. Generating a Report: To consolidate the information and create a report, we can export the array pairing issues to a CSV file. Here’s an example of how to generate a report:

$reportPath = "C:\Reports\ArrayPairingReport.csv"

if ($pairingIssues) {
    $pairingIssues | Export-Csv -Path $reportPath -NoTypeInformation
    Write-Host "Array pairing issues exported to $reportPath"
} else {
    Write-Host "No array pairing issues found."
}


In this script, we export the array pairing issues stored in the `$pairingIssues` variable to a CSV file using the `Export-Csv` cmdlet. The report is saved to the specified `$reportPath`. If no array pairing issues are found, a corresponding message is displayed. Conclusion: Using PowerCLI, we can automate the process of reading SRM logs and validating array pairing issues. By parsing the logs and checking the array pairing status, we can quickly identify any issues and ensure the reliability of our disaster recovery solution. Automating this process saves time and allows for proactive troubleshooting, minimizing the impact of potential failures.

Troubleshooting VMkernel and vobd.log in vSphere Client using PowerShell and Publishing Errors to a File

Introduction: VMkernel and vobd.log are essential components of vSphere infrastructure, providing valuable insights into the health and performance of your virtual environment. Troubleshooting issues related to VMkernel and vobd.log logs can be time-consuming if done manually. In this article, we will explore how to leverage PowerShell to automate the process of troubleshooting VMkernel and vobd.log and publish all errors to a file, simplifying the troubleshooting process and saving valuable time.

1. Establishing Connection to vSphere Environment: The first step is to establish a connection to the vSphere environment using the vSphere PowerCLI module in PowerShell. This allows us to interact with the vSphere API and retrieve the necessary information. Use the following commands to connect to the vSphere environment:

powershell
# Import the vSphere PowerCLI module
Import-Module VMware.PowerCLI

# Connect to the vSphere environment
Connect-VIServer -Server <vCenter_Server_IP> -User <Username> -Password <Password>

Make sure to replace “, “, and “ with your actual vCenter Server details. 2. Retrieving VMkernel Logs: The VMkernel logs provide information about the virtualization layer, including host hardware, networking, and storage. To retrieve VMkernel logs, we can use the `Get-VMHost` cmdlet and specify the desired log file. Here’s an example of how to retrieve VMkernel logs for all hosts in the vSphere environment:

powershell
# Retrieve VMkernel logs for all hosts
$VMHosts = Get-VMHost
$VMkernelLogs = @()

foreach ($VMHost in $VMHosts) {
    $VMkernelLog = Get-Log -VMHost $VMHost -LogFile vmkernel.log
    $VMkernelLogs += $VMkernelLog
}

The above script retrieves the `vmkernel.log` file for each host in the environment and stores it in the `$VMkernelLogs` array.

3. Parsing VMkernel Logs for Errors: Once we have retrieved the VMkernel logs, we can parse them to identify any errors or warning messages. We can use regular expressions to search for specific patterns indicating errors. Here’s an example of how to parse the VMkernel logs for errors:

powershell
$ErrorPattern = "error|warning" # Define the error pattern to search for
$Errors = @()

foreach ($VMkernelLog in $VMkernelLogs) {
    $Matches = Select-String -Pattern $ErrorPattern -InputObject $VMkernelLog.LogMessages
    
    if ($Matches) {
        $Errors += $Matches
    }
}

In the above script, we define the error pattern to search for, such as “error” or “warning”. We then iterate through each VMkernel log and use the `Select-String` cmdlet to find matches for the error pattern. If any matches are found, they are stored in the `$Errors` array.

4. Retrieving vobd.log: The vobd.log file contains information related to vSphere Object Block Device (VOBD) operations, including datastore and storage-related events. To retrieve the vobd.log file, we can use the `Get-Log` cmdlet and specify the `vobd.log` file. Here’s an example:

powershell
$VobdLog = Get-Log -Name vobd.log

The above script retrieves the `vobd.log` file and stores it in the `$VobdLog` variable.

5. Parsing vobd.log for Errors: Similar to parsing VMkernel logs, we can parse the vobd.log file to identify any errors or warning messages. Here’s an example of how to parse the vobd.log file for errors:

powershell
$VobdErrors = Select-String -Pattern $ErrorPattern -InputObject $VobdLog.LogMessages

In the above script, we use the `Select-String` cmdlet to search for matches of the error pattern in the vobd.log file. Any matches are stored in the `$VobdErrors` variable.

PowerShell script that can help you orchestrate SQL VMs with databases in a VMware environment

# Connect to vCenter Server
Connect-VIServer -Server <vCenter_Server_IP_Address> -User <Username> -Password <Password>

# Define the SQL VMs and their associated databases
$SQLVMs = @{
    "SQLVM1" = @("Database1", "Database2")
    "SQLVM2" = @("Database3", "Database4")
    # Add more SQL VMs and their databases as needed
}

# Loop through each SQL VM
foreach ($SQLVM in $SQLVMs.Keys) {
    $SQLVMDatabases = $SQLVMs[$SQLVM]

    # Power on the SQL VM
    Start-VM -VM $SQLVM -Confirm:$false

    # Wait for the SQL VM to power on
    do {
        Start-Sleep -Seconds 5
        $VM = Get-VM -Name $SQLVM
    } while ($VM.PowerState -ne "PoweredOn")

    # Loop through each database in the SQL VM
    foreach ($Database in $SQLVMDatabases) {
        # Perform any required actions on the database
        # For example, you can use the Invoke-SqlCmd cmdlet to execute SQL queries or scripts against the database
        Invoke-SqlCmd -ServerInstance $SQLVM -Database $Database -Query "SELECT * FROM TableName"
        # Add more actions as needed
    }

    # Power off the SQL VM
    Stop-VM -VM $SQLVM -Confirm:$false
}

# Disconnect from vCenter Server
Disconnect-VIServer -Server <vCenter_Server_IP_Address> -Confirm:$false

Write-Host "SQL VM orchestration completed."

Make sure to replace “, “, and “ with your actual vCenter Server details. In this script, you define the SQL VMs and their associated databases in the `$SQLVMs` hashtable. Each SQL VM is a key in the hashtable, and the value is an array of database names. The script then loops through each SQL VM, powers it on, waits for it to be powered on successfully, and performs any required actions on each database. In the example, it uses the `Invoke-SqlCmd` cmdlet to execute a SELECT statement against each database. You can modify this section to perform any other actions you require, such as backups, restores, or database maintenance tasks. After performing the actions on the databases, the script powers off the SQL VM and proceeds to the next SQL VM in the loop. Finally, the script disconnects from the vCenter Server. Remember to adjust the script as per your specific SQL VM and database configuration and requirements.

PowerShell script that can help you validate Esxtop and compute metrics for all VMs and datastores

# Connect to vCenter Server
Connect-VIServer -Server <vCenter_Server_IP_Address> -User <Username> -Password <Password>

# Retrieve all VMs and datastores
$VMs = Get-VM
$Datastores = Get-Datastore

# Define the output file path
$OutputFile = "C:\EsxtopValidation.csv"

# Create an empty array to store the results
$Results = @()

# Loop through each VM
foreach ($VM in $VMs) {
    $VMName = $VM.Name
    
    # Retrieve compute metrics for the VM
    $VMStats = Get-Stat -Entity $VM -Realtime -Stat cpu.usage.average,memory.usage.average
    
    # Calculate average CPU and memory usage
    $CPUUsage = $VMStats | Where-Object {$_.MetricId -eq "cpu.usage.average"} | Measure-Object -Property Value -Average | Select-Object -ExpandProperty Average
    $MemoryUsage = $VMStats | Where-Object {$_.MetricId -eq "memory.usage.average"} | Measure-Object -Property Value -Average | Select-Object -ExpandProperty Average
    
    # Create a custom object with VM compute metrics
    $VMResult = [PSCustomObject]@{
        VMName = $VMName
        CPUUsage = $CPUUsage
        MemoryUsage = $MemoryUsage
    }
    
    # Add VM compute metrics to the results array
    $Results += $VMResult
}

# Loop through each datastore
foreach ($Datastore in $Datastores) {
    $DatastoreName = $Datastore.Name
    
    # Retrieve datastore metrics
    $DatastoreStats = Get-Stat -Entity $Datastore -Realtime -Stat datastore.datastoreIops.average,datastore.datastoreLatency.average
    
    # Calculate average datastore IOPS and latency
    $DatastoreIOPS = $DatastoreStats | Where-Object {$_.MetricId -eq "datastore.datastoreIops.average"} | Measure-Object -Property Value -Average | Select-Object -ExpandProperty Average
    $DatastoreLatency = $DatastoreStats | Where-Object {$_.MetricId -eq "datastore.datastoreLatency.average"} | Measure-Object -Property Value -Average | Select-Object -ExpandProperty Average
    
    # Create a custom object with datastore metrics
    $DatastoreResult = [PSCustomObject]@{
        DatastoreName = $DatastoreName
        DatastoreIOPS = $DatastoreIOPS
        DatastoreLatency = $DatastoreLatency
    }
    
    # Add datastore metrics to the results array
    $Results += $DatastoreResult
}

# Export the results to a CSV file
$Results | Export-Csv -Path $OutputFile -NoTypeInformation

# Disconnect from vCenter Server
Disconnect-VIServer -Server <vCenter_Server_IP_Address> -Confirm:$false

Write-Host "Esxtop validation completed. Results exported to $OutputFile."

Make sure to replace “, “, and “ with your actual vCenter Server details. Also, specify the desired output file path in the `$OutputFile` variable. This script connects to your vCenter Server, retrieves all VMs and datastores, and then uses `Get-Stat` cmdlet to fetch real-time compute metrics for each VM and datastore. It calculates the average CPU and memory usage for each VM and the average IOPS and latency for each datastore. The results are then exported to a CSV file specified in the `$OutputFile` variable. Please note that this script requires the VMware PowerCLI module to be installed on the machine where you are running the script. You can install it by running `Install-Module -Name VMware.PowerCLI` in a PowerShell session. Once you run the script, it will provide you with the path to the generated CSV file, where you can review the computed metrics for all VMs and datastores. Remember to adjust the script as per your specific requirements and environment.

Clones using VAAI and migrate them to a different datastore

To create clones using VAAI and migrate them to a different datastore, and then export the data to a CSV file for reporting purposes, you can use the PowerCLI module. Here’s an example script:

powershell
# Connect to vCenter Server
Connect-VIServer -Server <vCenterServer> -User <username> -Password <password>

# Specify the source VM and the destination datastore
$sourceVM = "SourceVM"
$destinationDatastore = "DestinationDatastore"

# Create a clone of the source VM on the destination datastore using VAAI
$cloneVM = New-VM -Name "CloneVM" -VM $sourceVM -Datastore $destinationDatastore -RunAsync -UseVAAI

# Wait for the clone operation to complete
$cloneTask = Get-Task -Name $cloneVM.ExtensionData.Name
$cloneTask | Wait-Task

# Power on the clone VM
Start-VM -VM $cloneVM

# Export VM details to CSV
$vmDetails = Get-VM -Name $cloneVM.Name
$vmDetails | Export-Csv -Path "C:\Path\To\Report.csv" -NoTypeInformation

# Disconnect from vCenter Server
Disconnect-VIServer -Server <vCenterServer> -Confirm:$false

Make sure to replace “, “, and “ with your actual vCenter Server details. Also, update “ and “ with the names of your source VM and destination datastore, respectively. This script creates a clone of the source VM on the destination datastore using VAAI. It then waits for the clone operation to complete and powers on the clone VM. Finally, it retrieves the VM details and exports them to a CSV file for reporting. Please note that the PowerCLI module must be installed and properly configured for this script to work.

Performance metics on VSAN

To check the performance of a VMware vSAN using PowerShell, you can utilize the PowerCLI module to retrieve relevant performance metrics. Here’s an example script:

powershell
# Connect to vCenter Server
Connect-VIServer -Server <vCenterServer> -User <username> -Password <password>

# Specify the vSAN cluster name
$clusterName = "vSANCluster"

# Get the vSAN cluster object
$cluster = Get-Cluster -Name $clusterName

# Get the vSAN performance statistics for the cluster
$performanceStats = Get-Stat -Entity $cluster -Stat "vsan.*" -Realtime

# Loop through each performance stat and display the values
foreach ($stat in $performanceStats) {
    $statName = $stat.MetricId.Replace("vsan.", "")
    $statValue = $stat.Value
    $statUnit = $stat.Unit
    Write-Host "$statName: $statValue $statUnit"
}

# Disconnect from vCenter Server
Disconnect-VIServer -Server <vCenterServer> -Confirm:$false

Make sure to replace “, “, and “ with your actual vCenter Server details. Also, update “ with the name of your vSAN cluster. This script connects to the vCenter Server, retrieves the vSAN cluster object, and then fetches real-time performance statistics using the `Get-Stat` cmdlet. It loops through each performance stat, extracts the metric name, value, and unit, and displays them on the console. Please note that the PowerCLI module must be installed and properly configured for this script to work. Additionally, you may need to adjust the `Get-Stat` cmdlet parameters to retrieve specific vSAN performance metrics based on your requirements.

Power shell script to create and clone 100 VMs from one storage and move send 10000 Iops on each VM

# Connect to the vCenter Server
Connect-VIServer -Server <vCenterServer> -User <username> -Password <password>

# Specify the source VM and storage
$sourceVM = "SourceVM"
$sourceDatastore = "SourceDatastore"

# Specify the destination datastore
$destinationDatastore = "DestinationDatastore"

# Specify the number of VMs to create and clone
$numberOfVMs = 100

# Specify the number of IOPS to send on each VM
$iops = 10000

# Loop through and create/cloning VMs
for ($i = 1; $i -le $numberOfVMs; $i++) {
    $newVMName = "VM$i"

    # Clone the VM from the source to the destination datastore
    $cloneSpec = New-Object VMware.Vim.VirtualMachineCloneSpec
    $cloneSpec.Location = New-Object VMware.Vim.VirtualMachineRelocateSpec
    $cloneSpec.Location.Datastore = Get-Datastore -Name $destinationDatastore
    $cloneSpec.PowerOn = $false

    $sourceVMObj = Get-VM -Name $sourceVM
    New-VM -Name $newVMName -VM $sourceVMObj -Location (Get-Folder) -Datastore (Get-Datastore -Name $destinationDatastore) -CloneSpec $cloneSpec

    # Power on the newly created VM
    Start-VM -VM $newVMName

    # Send IOPS on the VM
    $vm = Get-VM -Name $newVMName
    $disk = $vm | Get-HardDisk
    $disk | Set-HardDisk -Iops $iops
}

# Disconnect from the vCenter Server
Disconnect-VIServer -Server <vCenterServer> -Confirm:$false

Make sure to replace “, “, “, “, “, “ with your actual vCenter Server details and VM/storage names. Please note that this script assumes you have the VMware PowerCLI module installed and properly configured.

PowerShell Modules for Rubrik

So once you connect to the Rubrik Module :

Requirements:

  1. PowerShell version 4 or higher
  2. PowerCLI version 6.0 or higher
  3. Rubrik version 2.2 or higher
  4. (optional) Windows Management Framework 5.0 

Installation :

+++++++++

  1. Ensure you have the Windows Management Framework 5.0 or greater installed.
  2. Open a Powershell console with the Run as Administrator option.
  3. Run Set-ExecutionPolicy using the parameter RemoteSigned or Bypass.
  4. Run Install-Module -Name Rubrik -Scope CurrentUser to download the module from the PowerShell Gallery. Note that the first time you install from the remote repository it may ask you to first trust the repository.

Option 2: Installer Script

  1. Download the master branch to your workstation.
  2. Open a Powershell console with the Run as Administrator option.
  3. Run Set-ExecutionPolicy using the parameter RemoteSigned or Bypass.
  4. Run the Install-Rubrik.ps1 script in the root of this repository and follow the prompts to install, upgrade, or delete your Rubrik Module contents.

Than lets see some commands:

 

These are the list of commands which you can execute from PowerShell.

Now lets start with how we connect :

So you get connected to the cluster. Once you are connected:

When we run this we see multiple instances loaded on the cluster .

Lets check the list of filesets now:

 

It gives you amazing set of information which you can customize accordingly as in how you need them in your environment.

You can use : Get-Command -Module Rubrik command to explore the list on Rubrik Module

Please comment on this article if you need any information regarding the powershell execution against Rubrik Module.

Who deleted my VM?

This is the best one which is mostly asked by customers .

$SqlServer = "SQLSERVER";
$SqlDB = "VCDB";
$MYVM = "TEST"
$TypeofEvent = "vim.event.VmRemovedEvent"
# The vim.event.VmRemovedEvent is a Removed action from VC you can also use :
# vim.event.VmGuestShutdownEvent
# vim.event.VmPoweredOffEvent
# vim.event.VmConnectedEvent
 
Function Read-VIDB ($SqlQuery)
{
# Setup SQL Connection
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SqlServer; Database = $SqlDB; Integrated Security = True"
 
# Setup SQL Command
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection
 
# Setup .NET SQLAdapter to execute and fill .NET Dataset
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
 
#Execute and Get Row Count
$nRecs = $SqlAdapter.Fill($DataSet)
 
if ($nRecs -gt 0)
{
# Do Stuff
$dataSet.Tables | Select-Object -Expand Rows
}
}
 
$SqlQuery = "SELECT CREATE_TIME, USERNAME, VM_NAME, HOST_NAME, EVENT_TYPE FROM VMWareDS.VPX_EVENT WHERE (VM_NAME = N'$MYVM') AND (EVENT_TYPE = '$TypeofEvent')"
$MyResults = Read-VIDB $SqlQuery
$MyResults