Change IP Address with Invoke-vmscript

This topic contains 3 replies, has 1 voice, and was last updated by Profile photo of P0w3rb0y P0w3rb0y 2 months, 3 weeks ago.

  • Author
    Posts
  • #54146
    Profile photo of P0w3rb0y
    P0w3rb0y
    Participant

    Hi guys,
    I need to bulk change IP addresses on new Nic that I add on a VM. But I dont want to screw up the existing first NIC's IP settings.
    So I find what NIC is using DHCP (the new one) and apply the new IP from a csv input file.
    Because the script text in invoke-vmscript doesnt allow variables I had to use strings and replace.
    It runs successfully but doesnt change the IP address on the VM. If I run the code locally on the VM it works.
    If I output the $script I see that its divided in 2 lines instead of one, how do I fix it? Please help

    PS C:\> $script
    "NEW-NetIPAddress -interfacealias "Ethernet
    " -IPaddress "10.80.4.57" -PrefixLength 23"

    CODE:

    $baseScript = @'
    "NEW-NetIPAddress -interfacealias "($NIC)" -IPaddress "($IP)" -PrefixLength 23"
    '@
    
    foreach ($obj in $x) {
            $VM = Get-VM "$($obj.vmname)"
            $IP = "$($obj.backupIP)"
        
            $script1 = "(Get-netIpInterface -dhcp enabled | Select-Object interfacealias).interfacealias"
            $Local:_Output = Invoke-VMScript -ScriptText $script1 -VM $VM -GuestCredential $cred
            $script = $baseScript.Replace('($NIC)',$_Output.ScriptOutput).Replace('($IP)',$IP)
            
    $params = @{
    VM = $VM
    ScriptText = $script
    GuestCredential = $Cred
    ScriptType = “PowerShell”
    Confirm = $false
    }
    Invoke-VMScript @params
    }
    
  • #54148
    Profile photo of P0w3rb0y
    P0w3rb0y
    Participant

    So I managed to fix the split:

    PS C:\> $script
    "NEW-NetIPAddress -interfacealias "Ethernet" -IPaddress "10.80.4.57" -PrefixLength 23"

    with this new code:

    $baseScript = @'
    "NEW-NetIPAddress -interfacealias "($NIC)" -IPaddress "($IP)" -PrefixLength 23"
    '@
    
    foreach ($obj in $x) {
            $VM = Get-VM "$($obj.vmname)"
            $IP = "$($obj.backupIP)"
        
            $script1 = "(Get-netIpInterface -dhcp enabled | Select-Object interfacealias).interfacealias"
            $Local:_Output = Invoke-VMScript -ScriptText $script1 -VM $VM -GuestCredential $cred
            $Intalias = echo "$Local:_Output".trim()
            $script = $baseScript.Replace('($NIC)',$Intalias).Replace('($IP)',$IP)
            
    $params = @{
    VM = $VM
    ScriptText = $script
    GuestCredential = $Cred
    ScriptType = “PowerShell”
    Confirm = $false
    }
    Invoke-VMScript @params
    }
    

    But now I get this error:

    + 
    • This reply was modified 2 months, 3 weeks ago by Profile photo of P0w3rb0y P0w3rb0y. Reason: fix error output
    • This reply was modified 2 months, 3 weeks ago by Profile photo of P0w3rb0y P0w3rb0y.
  • #54152
    Profile photo of P0w3rb0y
    P0w3rb0y
    Participant

    This is the error :
    NEW-NetIPAddress -interfacealias "Ethernet" -IPaddress "10.80.4.57"
    -PrefixL ...
    +
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Unexpected token 'Ethernet" -IPaddress "10.80.4.57" -PrefixLength 23"' in
    expression or statement.
    + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken

    • This reply was modified 2 months, 3 weeks ago by Profile photo of P0w3rb0y P0w3rb0y.
  • #54165
    Profile photo of P0w3rb0y
    P0w3rb0y
    Participant

    Hi guys
    I fixed it myself. You can't have "quotes" inside strings. So I passed the quotes down to: ScriptText = "$script"
    Happy days

    $baseScript = @'
    New-NetIPAddress -InterfaceAlias "($NIC)" -IPaddress "($IP)" -PrefixLength 23
    '@
    
    foreach ($obj in $x) {
            $VM = Get-VM $obj.vmname
            $IP = $obj.backupIP
        
            $script1 = "(Get-netIpInterface -dhcp enabled | Select-Object interfacealias).interfacealias"
            $Local:_Output = Invoke-VMScript -ScriptText $script1 -VM $VM -GuestCredential $cred
            $Intalias = echo "$Local:_Output".trim()
            $script = $baseScript.Replace('($NIC)',$Intalias).Replace('($IP)',$IP)
            
    $params = @{
    VM = $VM
    ScriptText = "$script"
    GuestCredential = $Cred
    ScriptType = “PowerShell”
    Confirm = $false
    }
    Invoke-VMScript @params
    }
    

You must be logged in to reply to this topic.