VMware Cloud Community
tdubb123
Expert
Expert

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

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.
Reply
0 Kudos
8 Replies
tdubb123
Expert
Expert

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 "

Reply
0 Kudos
tdubb123
Expert
Expert

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

 

some as Ethernet0

some as Ethernet

some as PROD

 

any idea?

Reply
0 Kudos
LucD
Leadership
Leadership

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
}

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
LucD
Leadership
Leadership

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
tdubb123
Expert
Expert

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

 

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

Try like this

 

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

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Tags (1)
Reply
0 Kudos
tdubb123
Expert
Expert

kept getting 

 

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

Reply
0 Kudos
LucD
Leadership
Leadership

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos