Automation

 View Only
  • 1.  Invoke-VMScript to set ip and dns

    Posted Jan 03, 2023 06:10 PM

    I'm trying to run this:

    $VM = "VM1"
    $IPType = 'IPv4'
    $IP = '10.10.10.55'
    $MaskBits = 24
    $Gateway = '10.10.10.1'
    $DNSPrimary = '10.10.10.1'
    $DNSSecondary = '10.10.10.2'

    $adapterscript = "$GetAdapter = Get-NetAdapter;
                                 $SetAdapter = $GetAdapter | New-NetIPAddress -AddressFamily $IPType -IPAddress $IP -PrefixLength $MaskBits -DefaultGateway $Gateway;
                                 $GetAdapter | Set-DnsClientServerAddress -ServerAddresses $DNSPrimary"

    Write-Verbose -Message "Getting ready to change IP on VM $VM." -Verbose

    Invoke-VMScript -ScriptText $adapterscript -ScriptType Powershell -VM $VM -GuestCredential $LocalCredential

    but getting error:

    ScriptOutput
    ---------------------------------------------------------------------------------------------------------------------------------------------------------------
    | = : The term '=' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
    | spelling of the name, or if a path was included, verify that the path is correct and try again.
    | At line:1 char:5
    | + & { = Get-NetAdapter;
    | + ~
    | + CategoryInfo : ObjectNotFound: (=:String) [], CommandNotFoundException
    | + FullyQualifiedErrorId : CommandNotFoundException
    |

    I have tried to wrap the @adapterscript in singe ' and double " but same issue.



  • 2.  RE: Invoke-VMScript to set ip and dns
    Best Answer

    Posted Jan 03, 2023 06:37 PM

    You will need to use a here-string, escape the variables you don't want to be substituted, and then call ExpandString.

    Something like this for example