• This topic has 2 replies, 3 voices, and was last updated May 1, 2020 by Greg B.

Getting Site VMs info using PowerShell a script

  • I am new to PowerShell and am not sure where to start. But my question is. Is there a script that can be run to pull/export to excel, just the VM information per site?

    I would like to do a weekly audit of the number of VMs my customers have weekly.

    We use something like this:

    function getxZertoSession ($zvm, $userName, $password) {
    add-type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    public class TrustAllCertsPolicy : ICertificatePolicy {
    public bool CheckValidationResult(
    ServicePoint srvPoint, X509Certificate certificate,
    WebRequest request, int certificateProblem) {
    return true;
    }
    }
    "@
    [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
    $xZertoSessionURL = $zvm + "session/add"
    $authInfo = ("{0}:{1}" -f $userName, $password)
    $authInfo = [System.Text.Encoding]::UTF8.GetBytes($authInfo)
    $authInfo = [System.Convert]::ToBase64String($authInfo)
    $headers = @{Authorization = ("Basic {0}" -f $authInfo) }
    $body = '{"AuthenticationMethod": "1"}'
    $contentType = "application/json"
    $xZertoSessionResponse = Invoke-WebRequest -Uri $xZertoSessionURL -Headers $headers -Method POST -Body $body -ContentType $contentType
    return @{"x-zerto-session" = $xZertoSessionResponse.headers.get_item("x-zerto-session") }
    }

    # Initializations
    $ZVMServer = "myserver.mydomain.local:9669"
    $ZertoRestURL = "https://$($ZVMServer)/v1/"

    $ZertoVMs = @()
    $VPGSettingsCollection = @()

    # Get Credentials for ZVM API
    $Credentials = Get-Credential -Message "Please enter Username and Password for ZVM $($ZVMServer)" -UserName "$($env:USERNAME)"
    $username = $Credentials.UserName
    $password = $Credentials.GetNetworkCredential().Password

    # Get Zerto Session Header for Auth
    $ZertoSession = getxZertoSession "$($ZertoRestURL)" $username $password

    # Connect to vCenter
    Connect-VIServer 'myvcenter' -Credential $Credentials

    # Get all the VPGs and store in $VPGSettingsCollection
    $AllVPGs = Invoke-RestMethod -Method Get -Uri ("$($ZertoRestURL)VPGs") -ContentType "application/json" -Headers $ZertoSession
    foreach ($VPG in $AllVPGs) {
    $VPG_Id = '{"VpgIdentifier": "' + $VPG.VpgIdentifier + '"}'
    $VPGsettingsObject = Invoke-RestMethod -Method POST -Uri ("$($ZertoRestURL)VPGSettings") -ContentType "application/json" -Body $VPG_Id -Headers $ZertoSession
    $VPGsettingsObject

    $VPGSettings = Invoke-RestMethod -Method GET -Uri ("$($ZertoRestURL)VPGSettings/$($VPGsettingsObject)") -ContentType "application/json" -Headers $ZertoSession
    Invoke-RestMethod -Method DELETE -Uri ("$($ZertoRestURL)VPGSettings/$($VPGsettingsObject)") -ContentType "application/json" -Headers $ZertoSession
    $VPGSettingsCollection += $VPGsettings
    }

    # Get all the running VMs from VMware, excluding the VRAs themselves
    $VMwareVMs = Get-Datacenter 'MyDatacenter' | Get-Cluster | Get-Vm | Where { $_.PowerState -eq 'PoweredOn' -and $_.Name -notlike "Z-VRA-*" }

    # Start building array $ZertoVMs from relevant items in $VPGSettingsCollection
    ForEach ($VPG in $VPGSettingsCollection) {
    ForEach ($VM in $VPG.Vms) {
    $VmInfo = "" | Select Name, FailoverIP, VPG
    $Id = "VirtualMachine-" + $VM.VmIdentifier.Split('.')[1] $VMInfo.Name = ($VMwareVMs | Where { $_.Id -eq $Id }).Name
    $VMInfo.FailoverIP = $VM.Nics[0].Failover.Hypervisor.IpConfig.StaticIp
    $VMInfo.VPG = $VPG.basic.name
    $ZertoVMs += $VMInfo
    }
    }

    $ZertoVMs | Export-Csv "MyZertoVMs.csv"

    I’ve been using the ZertoApiWrapper module and find it quite good, https://www.powershellgallery.com/packages/ZertoApiWrapper/1.4.0.20200410.  I use the below:

    Connect-ZertoServer -zertoServer server1 -zertoPort 9669 -credential $myCred
    $vpg = Get-ZertoVpg -name MyVpg
    $vms = Get-ZertoProtectedVm -protectedSiteIdentifier $vpg.protectedsite.identifier
    There’s probably a better way to get the protected site id for your purposes so you don’t have to target a specific vpg, but I needed to get the vpg anyway so wasn’t an issue.  Hope this helps.
You must be logged in to create new topics. Click here to login