• This topic has 3 replies, 3 voices, and was last updated June 24, 2022 by Sandeep N.

Powershell Configure VM Failover NIC IP

  • Can ANYONE out there provide WORKING powershell code that EXPORTS  VPG VM data – change/input VM FAILOVER NIC IP data and then IMPORTS VPG VM FAILOVER NIC IP DATA
    This is VERY BADLY needed – I keep finding bits and pieces out in the universe – ZERTO CUSTOMERS NEED THIS FUNCTIONALITY.  WORKING SOLID.  NO ERRORS.  HARD as #$@%@   finding this code.

    ALSO – ZERTO’s web site here is super AWFUL – there should be a nice TABLE OF CONTENTS for ALL POWERSHELL code etc.    – AGAIN, CUMBERSOME.  DIFFICULT.  RIDICULOUS.

    thanks IN ADVANCE WHO HAS THE ‘GOLDEN CODE’ that WORKS.

    I am looking for similar codes as we have some re-IP issues with windows 2012 system.

    Hi,

    I have this working powershell script for updating vpg/vm NIC settings. I still need to update the script to update vm network name. But take  a look and let me know if this is helpful.

    This will only update the vm ip settings using the data from a csv file. i have a separate script to pull that data.

     

    #getVPG = $args[0]

    $getVPG = “test”

    ################################################
    # Configure the variables below
    ################################################

    $zerto = “https://zvm-ip:zvm-port/v1/session/add”
    $strZVMIP = “yourZerto”
    $strZVMPort = “xxxx”
    $strZVMUser = “@user.com”
    $strZVMPwd = ‘password’
    $ExcludesourceSite = @(“sample”)

     

    $CSVExportFile = “C:\path\ZertoSettings-old.csv”

    ########################################################################################################################
    # Nothing to configure below this line – Starting the main function of the script
    ########################################################################################################################
    ################################################
    # Importing CSV and building list of VPGs
    ################################################
    $CSVImport = Import-Csv $CSVExportFile
    $VPGsToConfigure = $CSVImport | select -ExpandProperty VPGName -Unique
    ################################################
    # Setting certificate exception to prevent authentication issues to the ZVM
    ################################################
    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
    ################################################
    # Building Zerto API string and invoking API
    ################################################
    $BaseURL = “https://” + $ZertoServer + “:9669/v1/”
    $ZertoSessionURL = $BaseURL + “session/add”
    $Header = @{“Authorization” = “Basic “+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($ZertoUser+”:”+$ZertoPassword))}
    $Type = “application/json”

     

    $BaseURL = “https://” + $strZVMIP + “:”+$strZVMPort+”/v1/”
    $xZertoSessionURL = $BaseURL +”session/add”
    $authInfo = (“{0}:{1}” -f $strZVMUser,$strZVMPwd)
    $authInfo=[System.Text.Encoding]::UTF8.GetBytes($authInfo)
    $authInfo=[System.Convert]::ToBase64String($authInfo)
    $headers=@{Authorization=(“Basic {0}” -f $authInfo)}
    $Type = “application/json”
    $body = ‘{“AuthenticationMethod”: “1”}’

    # Authenticating with API
    try{
    $xZertoSessionResponse = Invoke-WebRequest -Uri $xZertoSessionURL -Headers $headers -Method Post -Body $body -ContentType $Type -UseBasicParsing
    $ZertoSessionSuccess = $TRUE
    }
    catch{
    $ZertoSessionSuccess = $FALSE
    Write-Host $_.Exception.ToString()
    $error[0] |Format-List -Force
    }

    $ZertoSession = $xZertoSessionResponse.headers.get_item(“x-zerto-session”)
    $ZertoSessionHeader = @{“Accept”=”application/json”
    “x-zerto-session”=”$ZertoSession”}
    ################################################
    # IF not authenticated with Zerto no point continuing
    ################################################
    IF (($ZertoSessionSuccess -eq $TRUE) -and ($CSVImport -ne $null))
    {
    ################################################
    # Starting for each VPG action
    ################################################
    ForEach ($VPGName in $VPGsToConfigure)
    {
    If($VPGName -eq $getVPG)
    {
    # Output to host
    “——————————”
    “Configuring VPG: $VPGName”

    # Getting VPG Identifier
    $VPGID = $CSVImport | Where-Object {$_.VPGName -eq $VPGName} | select -ExpandProperty VPGID -Unique

    # Getting list of VMs to reconfigure
    $VMsToConfigure = $CSVImport | Where-Object {$_.VPGName -eq $VPGName} | select -ExpandProperty VMName -Unique
    “Configuring VMs: $VMsToConfigure”

    # Creating edit VPG JSON
    $VPGJSON =
    “{
    “”VpgIdentifier””:””$VPGID””
    }”

    ################################################
    # Posting the VPG JSON Request to the API
    ################################################
    $CreateVPGURL = $BaseURL+”vpgSettings”
    Try
    {
    $VPGSettingsID = Invoke-RestMethod -Method POST -Uri $CreateVPGURL -Body $VPGJSON -ContentType $Type -Headers $ZertoSessionHeader
    $ValidVPGSettingsID = $True
    }
    Catch
    {
    $ValidVPGSettingsID = $False
    $_.Exception.ToString()
    $error[0] | Format-List -Force
    }

    IF ($ValidVPGSettingsID -eq $True)
    {

    # Getting VPG settings
    $VPGSettingsURL = $BaseURL+”vpgSettings/”+$VPGSettingsID
    Try
    {
    $VPGSettings = Invoke-RestMethod -Method GET -Uri $VPGSettingsURL -ContentType $Type -Headers $ZertoSessionHeader
    }
    Catch
    {
    $_.Exception.ToString()
    $error[0] | Format-List -Force
    }

    # Setting recovery site ID (needed for network settings)
    $VPGRecoverySiteID = $VPGSettings.Basic.RecoverySiteIdentifier

    #OrgvDc List (Recovery Org vDC: )
    $ListOrgvDCURL = $BaseURL+”virtualizationsites/”+$VPGRecoverySiteID+”/orgvdcs”
    $ListOrgvDCs = Invoke-RestMethod -Uri $ListOrgvDCURL -Headers $zertoSessionHeader -ContentType “application/json”
    $GetOrgvDCName = $ListOrgvDCs | Where{$_.Identifier -eq $VPGRecoveryOrgvDCIdentifier}

    #List Client OrgvDC Networks (Gives Failover Network / Test Network)
    $OrgvDCNetworkURL = $BaseURL+”virtualizationsites/”+$VPGRecoverySiteIdentifier+”/orgvdcs/”+$GetOrgvDCName.Identifier+”/networks”
    $OrgvDCNetworks = Invoke-RestMethod -Uri $OrgvDCNetworkURL -Headers $zertoSessionHeader -ContentType “application/json”

    # Getting network info
    $VPGNetworkURL = $BaseURL+”virtualizationsites/$VPGRecoverySiteID/networks”
    Try
    {
    $VPGNetworkCMD = Invoke-RestMethod -Method GET -Uri $VPGNetworkURL -ContentType $Type -Headers $ZertoSessionHeader
    }
    Catch
    {
    $_.Exception.ToString()
    $error[0] | Format-List -Force
    }

    ################################################
    # Starting per VM actions
    ################################################
    ForEach ($VMName in $VMsToConfigure)
    {
    # Getting VM settings from the CSV
    $VMSettings = $CSVImport | Where-Object {$_.VPGName -eq $VPGName -and $_.VMName -eq $VMName}
    $VMID = $CSVImport | Where-Object {$_.VPGName -eq $VPGName -and $_.VMName -eq $VMName} | select -ExpandProperty VMID -Unique
    $VMNICIDs = $VMSettings.NICID
    ################################################
    # Starting per VM NIC actions
    ################################################

    }
    ForEach ($VMNICID in $VMNICIDs)
    {
    # Getting VM NIC settings
    $VMNICSettings = $VMSettings | Where-Object {$_.NICID -eq $VMNICID}
    $VMNICFailoverNetworkName = $VMNICSettings.FailoverNetworkName
    $VMNICFailoverNetworkID = $VMNICSettings.FailoverNetworkID
    $VMNICFailoverNetworkGuestEnabled = $VMNICSettings.GuestCustomizationEnabled
    $VMNICFailoverNetworkVMIPAddresss = $VMNICSettings.VMIpAddress
    $VMNICFailoverNetworkVMIPMode = ($VMNICSettings.VMIpMode).ToLower()
    $VMNICFailoverNetworkIsNICConnected = $VMNICSettings.IsNICConnected
    $VMNICFailoverNetworkIsPrimary = $VMNICSettings.IsPrimary

    $VMNICFailoverTestNetworkName = $VMNICSettings.TestFailoverNetworkName
    $VMNICFailoverTestNetworkID = $VMNICSettings.TestFailoverNetworkID
    $VMNICFailoverTestNetworkGuestEnabled = $VMNICSettings.TestGuestCustomizationEnabled
    $VMNICFailoverTestNetworkVMIPAddresss = $VMNICSettings.TestVMIpAddress
    $VMNICFailoverTestNetworkVMIPMode = ($VMNICSettings.TestVMIpMode).ToLower()
    $VMNICFailoverTestNetworkIsNICConnected = $VMNICSettings.TestIsNICConnected
    $VMNICFailoverTestNetworkIsPrimary = $VMNICSettings.TestIsPrimary

    $VMNICFailoverTestNetworkIdentifer = $OrgvDCNetworks | Where-Object{$_.VirtualizationNetworkName -eq $VMNICFailoverTestNetworkName}|Select NetworkIdentifier
    $VMNICFailoverNetworkIdentifier = $OrgvDCNetworks|Where-Object{$_.VirtualizationNetworkName -eq $VMNICFailoverNetworkName}|Select NetworkIdentifier

    ################################################
    # Building VMNIC JSON
    ################################################
    #For Testing hardcoded some of the values
    $VMNICJSON =
    ” {
    “”vmIdentifier””: “”$VMID””,

    “”nics””: [
    {
    “”nicIdentifier””: “”$VMNICID””,
    “”failoverTest””: {
    “”vcd””: {
    “”isResetMacAddress””: “”true””,
    “”isPrimary””: “”true””,
    “”isConnected””: “”true””,
    “”ipMode””: “”IPPool””,
    “”ipAddress””: “”””,
    “”recoveryOrgVdcNetworkIdentifier””: “”$VMNICFailoverTestNetworkID””
    },
    },
    “”failover””: {
    “”vcd””: {
    “”isResetMacAddress””: “”true””,
    “”isPrimary””: “”true””,
    “”isConnected””: “”true””,
    “”ipMode””: “”IPPool””,
    “”ipAddress””: “”””,
    “”recoveryOrgVdcNetworkIdentifier””: “”$VMNICFailoverNetworkID””
    }

    }
    }
    ] }”
    ################################################
    # Creating URL and sending PUT command to API
    ################################################
    $EditVMNICURL = $BaseURL+”vpgSettings/”+$VPGSettingsID+”/vms/”+$VMID
    “Editing NIC settings with PUT to: $EditVMNICURL”
    Try
    {
    Invoke-RestMethod -Method PUT -Uri $EditVMNICURL -Body $VMNICJSON -ContentType $Type -Headers $ZertoSessionHeader
    }
    Catch
    {
    $_.Exception.ToString()
    $error[0] | Format-List -Force
    }
    # Waiting for API processing
    sleep 3

    }
    # End of for each VMNIC above
    #
    # End of for each VM below
    }
    # End of for each VM above
    ################################################
    # Committing VPG settings
    ################################################
    $CommitVPGSettingURL = $BaseURL+”vpgSettings/”+”$VPGSettingsID”+”/commit”
    “Committing VPG Settings with POST to URL: $CommitVPGSettingURL”
    Try
    {
    Invoke-RestMethod -Method POST -Uri $CommitVPGSettingURL -Headers $ZertoSessionHeader -ContentType $Type -TimeoutSec 100
    $VPGEditOutcome = “PASSED”
    }
    Catch
    {
    $VPGEditOutcome = “FAILED”
    $_.Exception.ToString()
    $error[0] | Format-List -Force
    }
    “VPGEditOutcome: $VPGEditOutcome”
    # Sleeping before processing next VPG
    “Waiting 5 seconds before processing next VPG”
    sleep 5
    # End of check for valid VPG settings ID below
    }

    }
    # End of per VPG actions above
    ################################################
    # IF not authenticated with Zerto no point continuing
    ################################################
    }
    ELSE
    {
    “Failed to login to Zerto API or couldn’t load CSV, check server, credentials and the CSV config from the export script.”
    }

     

     

     

     

     

     

     

    ################################################
    # End of script
    ################################################

     

     

    Hi Suvarna , Thanks for sharing the script. Do you have a shorter version where we can just call the script from the VPG settings as post-script with variables if needed and all the script should do it change the IP after the VMs failover to target site.

    A few of our windows 2012 R2 system fails over where  VMware tools pushes the failover IP as configured in VPG settings. The OS goes for a reboot and comes online with the changed IP, however, it goes for another round of reboot rejecting the IP change and comes back online with original onpremise IP.

     

    The idea is to use powershell script to change the IP from the OS layer – example: using netsh command.

You must be logged in to create new topics. Click here to login