• This topic has 6 replies, 1 voice, and was last updated July 6, 2020 by Suresh R.

VM NIC Re-IP & Port Groups

  • Hi All,

    I am in process of Bulk Editing VM NIC Settings Including Re-IP & Port Groups settings, i found “White Paper – Automating Zerto Virtual Replication with PowerShell and REST APIs” PDF. below is the script for your reference,

    But i am getting error message , can someone help me on this please

    ==================================

    System.Net.WebException: The remote server returned an error: (400) Bad Request.
    at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.GetResponse(WebRequest request)
    at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.ProcessRecord()

    Exception : System.Net.WebException: The remote server returned an error: (400) Bad Request.
    at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.GetResponse(WebRequest request)
    at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.ProcessRecord()
    TargetObject : System.Net.HttpWebRequest
    CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
    ErrorDetails : Request Error
    The server encountered an error processing the request. Please see the service help page for constructing valid requests to the service.
    InvocationInfo : System.Management.Automation.InvocationInfo
    ScriptStackTrace : at <ScriptBlock>, C:XXX\import.ps1: line 185
    PipelineIterationInfo : {}
    PSMessageDetails :

    System.Net.WebException: The remote server returned an error: (400) Bad Request.
    at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.GetResponse(WebRequest request)
    at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.ProcessRecord()

    ==================================

    script

    ################################################
    # Configure the variables below
    ################################################
    $CSVImportFile = “C:\Users\xxx\1ZVRVMNICS.csv”
    $ZertoServer = “10.xx.xx.xx”
    $ZertoPort = “9669”
    $ZertoUser = “user@local.com”
    $ZertoPassword = “XXX”
    ########################################################################################################################
    # Nothing to configure below this line – Starting the main function of the script
    ########################################################################################################################
    ################################################
    # Importing CSV and building list of VPGs
    ################################################
    $CSVImport = Import-Csv $CSVImportFile
    $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 + “:”+$ZertoPort+”/v1/”
    # Authenticating with Zerto APIs
    $xZertoSessionURL = $baseURL + “session/add”
    $authInfo = (“{0}:{1}” -f $ZertoUser,$ZertoPassword)
    $authInfo = [System.Text.Encoding]::UTF8.GetBytes($authInfo)
    $authInfo = [System.Convert]::ToBase64String($authInfo)
    $headers = @{Authorization=(“Basic {0}” -f $authInfo)}
    $sessionBody = ‘{“AuthenticationMethod”: “1”}’
    $TypeJSON = “application/json”
    $TypeXML = “application/xml”
    Try
    {
    $xZertoSessionResponse = Invoke-WebRequest -Uri $xZertoSessionURL -Headers $headers -Method POST -Body $sessionBody -ContentType $TypeJSON
    }
    Catch {
    Write-Host $_.Exception.ToString()
    $error[0] | Format-List -Force
    }
    # Extracting x-zerto-session from the response, and adding it to the actual API
    $xZertoSession = $xZertoSessionResponse.headers.get_item(“x-zerto-session”)
    $zertoSessionHeader = @{“x-zerto-session”=$xZertoSession; “Accept”=$TypeJSON }
    $CreateVPGURL = $baseURL+”vpgSettings”
    ################################################
    # Starting for each VPG action
    ################################################
    foreach ($VPG in $VPGsToConfigure)
    {
    $VPGName = $VPG
    # Getting VPG Identifier
    $VPGidentifier = $CSVImport | Where-Object {$_.VPGName -eq $VPGName} | select -ExpandProperty VPGidentifier -Unique
    # Getting list of VMs to reconfigure
    $VMsToConfigure = $CSVImport | Where-Object {$_.VPGName -eq $VPGName} | select -ExpandProperty VMName -Unique
    # Creating edit VPG JSON
    $JSON =
    “{
    “”VpgIdentifier””:””$VPGidentifier””
    }”
    ################################################
    # Posting the VPG JSON Request to the API
    ################################################
    Try
    {
    $VPGSettingsIdentifier = Invoke-RestMethod -Method Post -Uri $CreateVPGURL -Body $JSON -ContentType $TypeJSON -Headers $zertoSessionHeader
    $ValidVPGSettingsIdentifier = $true
    }
    Catch {
    $ValidVPGSettingsIdentifier = $false
    }
    ################################################
    # Skipping if unable to obtain valid VPG setting identifier
    ################################################
    if ($ValidVPGSettingsIdentifier -eq $true)
    {
    ################################################
    # Getting ZVR IDs for the VPG
    ################################################
    $VPGSettingsURL = $baseURL+”vpgSettings/”+$VPGSettingsIdentifier
    $VPGSettings = Invoke-RestMethod -Uri $VPGSettingsURL -Headers $zertoSessionHeader -ContentType $TypeJSON
    # Getting recovery site ID (needed anyway for network settings)
    $VPGRecoverySiteIdentifier = $VPGSettings.Basic.RecoverySiteIdentifier
    # Getting network info
    $VINetworksURL = $baseURL+”virtualizationsites/$VPGRecoverySiteIdentifier/networks”
    $VINetworksCMD = Invoke-RestMethod -Uri $VINetworksURL -TimeoutSec 100 -Headers $zertoSessionHeader -ContentType $TypeJSON
    ################################################
    # Starting per VM actions
    ################################################
    foreach ($VM in $VMsToConfigure)
    {
    $VMName = $VM
    # Getting VM settings from the CSV
    $VMSettings = $CSVImport | Where-Object {$_.VPGName -eq $VPGName -and $_.VMName -eq $VMName} | select
    $VMIdentifier = $CSVImport | Where-Object {$_.VPGName -eq $VPGName -and $_.VMName -eq $VMName} | select -ExpandProperty VMIdentifier -Unique
    $VMNICIdentifiers = $VMSettings.VMNICIdentifier
    #####################
    # Starting per VM NIC actions
    #####################
    foreach ($VMNIC in $VMNICIdentifiers)
    {
    $VMNICIdentifier = $VMNIC
    # Getting VM NIC settings
    $VMNICSettings = $VMSettings | Where-Object {$_.VMNICIdentifier -eq $VMNICIdentifier} | select *
    $VMNICFailoverNetworkName = $VMNICSettings.VMNICFailoverNetworkName
    $VMNICFailoverDNSSuffix = $VMNICSettings.VMNICFailoverDNSSuffix
    $VMNICFailoverShouldReplaceMacAddress = $VMNICSettings.VMNICFailoverShouldReplaceMacAddress
    $VMNICFailoverGateway = $VMNICSettings.VMNICFailoverGateway
    $VMNICFailoverDHCP = $VMNICSettings.VMNICFailoverDHCP
    $VMNICFailoverPrimaryDns = $VMNICSettings.VMNICFailoverPrimaryDns
    $VMNICFailoverSecondaryDns = $VMNICSettings.VMNICFailoverSecondaryDns
    $VMNICFailoverStaticIp = $VMNICSettings.VMNICFailoverStaticIp
    $VMNICFailoverSubnetMask = $VMNICSettings.VMNICFailoverSubnetMask
    $VMNICFailoverTestNetworkName = $VMNICSettings.VMNICFailoverTestNetworkName
    $VMNICFailoverTestDNSSuffix = $VMNICSettings.VMNICFailoverTestDNSSuffix
    $VMNICFailoverTestShouldReplaceMacAddress = $VMNICSettings.VMNICFailoverTestShouldReplaceMacAddress
    $VMNICFailoverTestGateway = $VMNICSettings.VMNICFailoverTestGateway
    $VMNICFailoverTestDHCP = $VMNICSettings.VMNICFailoverTestDHCP
    $VMNICFailoverTestPrimaryDns = $VMNICSettings.VMNICFailoverTestPrimaryDns
    $VMNICFailoverTestSecondaryDns = $VMNICSettings.VMNICFailoverTestSecondaryDns
    $VMNICFailoverTestStaticIp = $VMNICSettings.VMNICFailoverTestStaticIp
    $VMNICFailoverTestSubnetMask = $VMNICSettings.VMNICFailoverTestSubnetMask
    # Setting answers to lower case for API to process
    $VMNICFailoverShouldReplaceMacAddress = $VMNICFailoverShouldReplaceMacAddress.ToLower()
    $VMNICFailoverDHCP = $VMNICFailoverDHCP.ToLower()
    $VMNICFailoverTestShouldReplaceMacAddress = $VMNICFailoverTestShouldReplaceMacAddress.ToLower()
    $VMNICFailoverTestDHCP = $VMNICFailoverTestDHCP.ToLower()
    # Translating network names to ZVR Network Identifiers
    $VMNICFailoverNetworkIdentifier = $VINetworksCMD | where-object {$_.VirtualizationNetworkName -eq $VMNICFailoverNetworkName} | select -ExpandProperty NetworkIdentifier
    $VMNICFailoverTestNetworkIdentifier = $VINetworksCMD | where-object {$_.VirtualizationNetworkName -eq $VMNICFailoverTestNetworkName} | select -ExpandProperty NetworkIdentifier
    #####################
    # Building VMNIC JSON
    #####################
    $VMNICJSON =
    ” {
    “”Failover””:{
    “”Hypervisor””:{
    “”DnsSuffix””:””$VMNICFailoverDNSSuffix””,
    “”IpConfig””:{
    “”Gateway””:””$VMNICFailoverGateway””,
    “”IsDhcp””:$VMNICFailoverDHCP,
    “”PrimaryDns””:””$VMNICFailoverPrimaryDns””,
    “”SecondaryDns””:””$VMNICFailoverSecondaryDns””,
    “”StaticIp””:””$VMNICFailoverStaticIp””,
    “”SubnetMask””:””$VMNICFailoverSubnetMask””
    },
    “”NetworkIdentifier””:””$VMNICFailoverNetworkIdentifier””,
    “”ShouldReplaceMacAddress””:$VMNICFailoverShouldReplaceMacAddress
    }
    },
    “”FailoverTest””:{
    “”Hypervisor””:{
    “”DnsSuffix””:””$VMNICFailoverTestDNSSuffix””,
    “”IpConfig””:{
    “”Gateway””:””$VMNICFailoverTestGateway””,
    “”IsDhcp””:$VMNICFailoverTestDHCP,
    “”PrimaryDns””:””$VMNICFailoverTestPrimaryDns””,
    “”SecondaryDns””:””$VMNICFailoverTestSecondaryDns””,
    “”StaticIp””:””$VMNICFailoverTestStaticIp””,
    “”SubnetMask””:””$VMNICFailoverTestSubnetMask””
    },
    “”NetworkIdentifier””:””$VMNICFailoverTestNetworkIdentifier””,
    “”ShouldReplaceMACAddress””:$VMNICFailoverTestShouldReplaceMacAddress
    }
    },
    “”NicIdentifier””:””$VMNICIdentifier””
    }”
    #####################
    # Creating URL and sending PUT command to API
    #####################
    $EditVMNICURL = $baseURL+”vpgSettings/”+$VPGSettingsIdentifier+”/vms/”+$VMIdentifier+”/nics/”+$VMNICIdentifier
    Try
    {
    $EditVMNIC = Invoke-RestMethod -Method PUT -Uri $EditVMNICURL -Body $VMNICJSON -Headers $zertoSessionHeader -ContentType $TypeJSON -TimeoutSec 100
    }
    Catch {
    Write-Host $_.Exception.ToString()
    $error[0] | Format-List -Force
    }
    # Waiting for API processing
    sleep 3
    # End of for each VMNIC below
    }
    # End of for each VMNIC above
    #
    # End of for each VM below
    }
    # End of for each VM above
    #####################
    # Committing VPG settings
    #####################
    $CommitVPGSettingURL = $baseURL+”vpgSettings/”+”$VPGSettingsIdentifier”+”/commit”
    write-host “CommitVPGSettingURL:$CommitVPGSettingURL”
    Try
    {
    Invoke-RestMethod -Method Post -Uri $CommitVPGSettingURL -Headers $zertoSessionHeader -ContentType $TypeJSON -TimeoutSec 100
    $VPGEditOutcome = “PASSED”
    }
    Catch {
    $VPGEditOutcome = “FAILED”
    Write-Host $_.Exception.ToString()
    $error[0] | Format-List -Force
    }
    write-host “VPG:$VPGName VPGEditOutcome=$VPGEditOutcome”
    # Sleeping before processing next VPG
    write-host “Waiting 5 seconds before processing next VPG”
    sleep 5
    # End of check for valid VPG settings ID below
    }
    # End of check for valid VPG settings ID above
    #
    # End of per VPG actions below
    }
    # End of per VPG actions above

     

     

    Hi Usrekso,

    The error is quite generic. Any chance you can post some more details (like CSV file used and maybe the actual PS1 file you used).

    Did the export piece work or did you not use that one?

    Thanks,

    Gijsbert

    Hi Gijsbert,

     

    posted my reply couple of times but it is not getting updated, can i have you email so that i can sent the details

    Thanks

    Hi Usrekso,

    Please use gijsbert -at- zerto -dot- com

    Hi Gijsbert,

     

    I sent email kindly check and help me.

     

    Thank you in advance.

    Is this related to a 6.0 upgrade?  I am having the same issue.

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