PowerCLI

 View Only
  • 1.  The filename, directory name, or volume label syntax is incorrect.

    Posted Oct 29, 2022 04:50 PM

    trying to change 2012 windows DNS settings with following

     
    $DC = read-host "Enter Datacenter"
     
    $GuestCred = $Host.UI.PromptForCredential("Please enter admin credentials", "Enter Guest credentials", "", "")
    $PrimaryDNS = Read-Host "Primary DNS: "
    $SecondaryDNS = Read-Host "Secondary DNS: "
     
    get-datacenter $DC | get-vm | ? {$_.NAme -notlike "*dc*" -and $_.powerstate -eq "PoweredOn"} | % {Invoke-VMScript -VM $_ -GuestCredential $GuestCred -ScriptType "bat" -ScriptText "netsh interface ip set dns ""Ethernet0"" static $PrimaryDNS && netsh interface ip add dns ""Ethernet0"" $SecondaryDNS" }
     
    but keep getting following error
     

    ScriptOutput
    ---------------------------------------------------------------------------------------------------------------------------------------------------------
    | The filename, directory name, or volume label syntax is incorrect.


  • 2.  RE: The filename, directory name, or volume label syntax is incorrect.

    Posted Oct 29, 2022 04:52 PM

    i was able to use 

     

    invoke-vmscript -VM $VM -scripttext {ipconfig /all}

     

    this worked just fine. Also I checke the name of the ethernet name and it is correct "



  • 3.  RE: The filename, directory name, or volume label syntax is incorrect.

    Posted Oct 29, 2022 05:16 PM

    I think the reason was some VM has ethernet interface labelled different

     

    some as Ethernet0

    some as Ethernet

    some as PROD

     

    any idea?



  • 4.  RE: The filename, directory name, or volume label syntax is incorrect.

    Posted Oct 29, 2022 05:17 PM

    Then you would need to find the name of the interface first, and substitute that in the $code1 variable



  • 5.  RE: The filename, directory name, or volume label syntax is incorrect.

    Posted Oct 30, 2022 11:50 PM

    is there way to get the variable from invoke-vmscript.?

     

    $code0 = @"
    $nic = (Get-NetAdapter | ? {$_.Status -eq "up"}).name
    "@
     
     
     


  • 6.  RE: The filename, directory name, or volume label syntax is incorrect.

    Posted Oct 31, 2022 09:16 AM

    Try like this

     

    $code0 = @"
    (Get-NetAdapter | ? {$_.Status -eq "up"}).name
    "@
    $nic = (Invoke-VMScript -VM $_ -GuestCredential $GuestCred -ScriptType "PowerShell" -ScriptText $code0).ScriptOutput

     



  • 7.  RE: The filename, directory name, or volume label syntax is incorrect.

    Posted Oct 31, 2022 11:46 PM

    kept getting 

     

    ScriptOutput
    ---------------------------------------------------------------------------------------------------------------------------------------------------------
    | & was unexpected at this time.
    |
    ----------------------------------------



  • 8.  RE: The filename, directory name, or volume label syntax is incorrect.

    Posted Nov 01, 2022 07:40 AM

    I didn't notice you had "bat" as the ScriptType, I corrected it to PowerShell.
    Try again



  • 9.  RE: The filename, directory name, or volume label syntax is incorrect.

    Posted Oct 29, 2022 05:16 PM

    The following works for me

     

    $DC = Read-Host "Enter Datacenter"
    
    $GuestCred = $Host.UI.PromptForCredential("Please enter admin credentials", "Enter Guest credentials", "", "")
    $PrimaryDNS = Read-Host "Primary DNS: "
    $SecondaryDNS = Read-Host "Secondary DNS: "
    $code1 = @"
    netsh interface ip set dns Ethernet0 static $PrimaryDNS && netsh interface ip add dns Ethernet0 $SecondaryDNS
    "@
    $code2 = @'
    ipconfig /all
    '@
    Get-Datacenter $DC | Get-VM |
    Where-Object { $_.Name -notlike "*dc*" -and $_.powerstate -eq "PoweredOn" } |
    ForEach-Object {
      Invoke-VMScript -VM $_ -GuestCredential $GuestCred -ScriptType "bat" -ScriptText $code1
      # Verify setting
      Invoke-VMScript -VM $_ -GuestCredential $GuestCred -ScriptType "bat" -ScriptText $code2
    }