Author Posts

September 14, 2016 at 11:04 am

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
}

September 14, 2016 at 11:45 am

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 years ago by  P0w3rb0y. Reason: fix error output
  • This reply was modified 2 years ago by  P0w3rb0y.

September 14, 2016 at 11:54 am

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 years ago by  P0w3rb0y.

September 14, 2016 at 2:11 pm

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
}