• This topic has 1 reply, 1 voice, and was last updated May 25, 2018 by John M.

Bulk Edit VRA – Update Default Gateway

  • I’m working on a script that will edit the default gateway of VRAs.  I’m getting the following error while running the script:

    PS C:\Windows\system32> TerminatingError(Invoke-RestMethod): "Request Error
    The server encountered an error processing the request. Please see the service help page for constructing valid requests to the service."
    
    
    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>, <No file>: line 102
    PipelineIterationInfo : {}
    PSMessageDetails      :

    The following is found in the logfile.csv

    Exception NullReferenceException (Object reference not set to an instance of an object.), cannot be serialized to preserve Stack, skipping. System.NullReferenceException: Object reference not set to an instance of an object.
    
       at Zerto.Zvm.Services.Vra.VraInfo.DynamicVraAddressTracker.SetVraBandwidthGroup(HostIdentifier host, String bwGroup) in E:\blv_u2\dev\system\Zvm\Services\Vra\Zerto.Zvm.Services.Vra.VraInfo\DynamicVraAddressTracker.cs:line 301
    
       at Zerto.Zvm.Vra.Management.VraManagement.HostTracker.ChangeVraBandwidthGroup(HostIdentifier host, String bwGroup) in E:\blv_u2\dev\system\Zvm\Vra\Zerto.Zvm.Vra.Management\VraManagement\HostTracker.cs:line 983
    

    And finally, here is my script (excluding the connection and session creation info, which I know is working because sessions are connecting correctly and variables are being populated with correct data.

    #Get VRAs
    $GetVRAsURL = $BaseURL + "vras"
    $vraList = Invoke-RestMethod -Uri $GetVRAsURL -TimeoutSec 100 -Headers $zertoSessionHeader -ContentType $TypeJSON
    
    #Parse $vraList
    foreach($vra in $vraList){
    
        $vraIdentifier = $vra.VraIdentifier
        $vraName = $vra.VraName
        $defaultGW = $vra.VraNetworkDataApi.DefaultGateway
        $vraIP = $vra.VraNetworkDataApi.VraIPAddress
        
    
        #Build VRA Edit URL
        $editVRAsURL = $baseURL + "vras/" + $vraIdentifier 
    
        if($defaultGW -eq $newVRADefaultGateway){
            #Skip update if GW is already correct
            Write-host "Skipping $vraName"
            Write-host "Current GW = $defaultGW"
            Write-host "Default gateway already $defaultGW"
        }else {
            #Change GW to $newVRADefaultGateway
            Write-host "Changing $vraName default gateway from $defaultGW to $newVRADefaultGateway"
    
            #Building JSON body to send to API
            $JSON = 
                "{
                ""VraNetworkDataApi"": {
                    ""DefaultGateway"": ""$newVRADefaultGateway"",
                    ""SubnetMask"": ""255.255.255.224"",
                    ""VraIPAddress"": ""$vraIP"",
                    ""VraIPConfigurationTypeApi"": ""Static""
                    }
                }"
            
            
            Write-host "Executing $JSON"
    
            #try/catch API command
            try {
                Invoke-RestMethod -Method PUT -Uri $editVRAsURL -Body $JSON -Headers $zertoSessionHeader -ContentType $TypeJSON -TimeoutSec 60
            }
            catch {
                Write-Host $_.Exception.ToString()
                $error[0] | Format-List -Force   
            }#end try/catch
    
            Write-host "Waiting 30 seconds before starting next VRA Edit"
            sleep 30 
        }#end if/else
    }#end foreach
    
    # Stopping logging
    Stop-Transcript

    Anyone have any ideas what could possibly be causing the Null exception in the PUT statement?

    I found the solution.  Apparently all the attributes listed on VRA:PUT documentation as optional, are in fact, required.

The forum ‘Scripting and APIs’ is closed to new topics and replies.