Hi,
I am trying to do IP configuration for Guest OS. But I am getting strange error message.
script :
$nic1Name = 'LAN'
$nicIP = 'xx'
$nicGW = 'xx'
$dns1 = 'xx'
$dns2 = 'xx'
$code = @'
foreach(`$nic in Get-NetAdapter){
switch(`$nic.MacAddress){
'$($nic1.MacAddress.Replace(':','-'))' {
Rename-NetAdapter -InputObject `$nic -NewName '$nic1Name'
New-NetIpAddress -InterfaceAlias '$nic1Name' -IPAddress '$nicIP' -PrefixLength 24 -DefaultGateway '$nicGW'
Set-DnsClientServerAddress -InterfaceAlias 'LAN' -ServerAddresses $dns1,$dns2
Set-NetIPInterface -InterfaceAlias '$nic1Name' -AutomaticMetric disabled -InterfaceMetric 5
}
Default {
Write-Error 'Unknown MAC address found'
}
}
}
Get-NetAdapter | Select Name,MacAddress
'@
Invoke-VMScript -VM $updateTempName -ScriptText $code -Verbose -GuestUser $updateTempUser -GuestPassword $updateTempPass -ScriptType Powershell
Message :
PS C:\Windows\system32> Invoke-VMScript -VM $updateTempName -ScriptText $code -Verbose -GuestUser $updateTempUser -GuestPassword $updateTempPass -ScriptType Powershell
VERBOSE: Performing the operation "Invoke-VMScript" on target "2016GoldenImage".
VERBOSE: 11/25/2022 8:48:20 PM Invoke-VMScript Finished execution
ScriptOutput
-----------------------------------------------------------------------------------------------------------------------| At line:1 char:12
| + & {foreach(`$nic in Get-NetAdapter){
| + ~
| Missing variable name after foreach.
| At line:1 char:3
| + & {foreach(`$nic in Get-NetAdapter){
| + ~
| Missing closing '}' in statement block or type definition.
| At line:1 char:35
| + & {foreach(`$nic in Get-NetAdapter){
| + ~
| Unexpected token ')' in expression or statement.
| At line:5 char:38
| + '$($nic1.MacAddress.Replace(':','-'))' {
| + ~
| Missing statement block in switch statement clause.
| At line:35 char:1
| + }
| + ~
| Unexpected token '}' in expression or statement.
| + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
| + FullyQualifiedErrorId : MissingVariableNameAfterForeach
|
|
-----------------------------------------------------------------------------------------------------------------------
Since you are using single quotes for the here-string, there will be no automatic substitution.
You will have to call ExpandString.
I have some Dives on the subject, see Here string and variable substitution and Here strings and the ExpandString method
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
like you said I've modified my script. still same issue.
code:
$code = @'
foreach(`$nic in Get-NetAdapter){
switch(`$nic.MacAddress){
'`$(`$nic1.MacAddress.Replace(':','-'))' {
Rename-NetAdapter -InputObject `$nic -NewName '$nic1Name'
New-NetIpAddress -InterfaceAlias '$nic1Name' -IPAddress '$nicIP' -PrefixLength 24 -DefaultGateway '$nicGW'
Set-DnsClientServerAddress -InterfaceAlias 'LAN' -ServerAddresses $dns1,$dns2
Set-NetIPInterface -InterfaceAlias '$nic1Name' -AutomaticMetric disabled -InterfaceMetric 5
}
Default {
Write-Error 'Unknown MAC address found'
}
}
}
Get-NetAdapter | Select Name,MacAddress
'@
$ExecutionContext.InvokeCommand.ExpandString($code)
output:
foreach($nic in Get-NetAdapter){
switch($nic.MacAddress){
'$($nic1.MacAddress.Replace(':','-'))' {
Rename-NetAdapter -InputObject $nic -NewName 'LAN'
New-NetIpAddress -InterfaceAlias 'LAN' -IPAddress 'xxxxxx' -PrefixLength 24 -DefaultGateway 'xxxxxx'
Set-DnsClientServerAddress -InterfaceAlias 'LAN' -ServerAddresses xxxx,xxxxx
Set-NetIPInterface -InterfaceAlias 'LAN' -AutomaticMetric disabled -InterfaceMetric 5
}
Default {
Write-Error 'Unknown MAC address found'
}
}
}
Get-NetAdapter | Select Name,MacAddress
error message:
VERBOSE: Performing the operation "Invoke-VMScript" on target "2016GoldenImage".
VERBOSE: 11/25/2022 9:21:40 PM Invoke-VMScript Finished execution
ScriptOutput
-----------------------------------------------------------------------------------------------------------------------| At line:1 char:12
| + & {foreach(`$nic in Get-NetAdapter){
| + ~
| Missing variable name after foreach.
| At line:1 char:3
| + & {foreach(`$nic in Get-NetAdapter){
| + ~
| Missing closing '}' in statement block or type definition.
| At line:1 char:35
| + & {foreach(`$nic in Get-NetAdapter){
| + ~
| Unexpected token ')' in expression or statement.
| At line:5 char:40
| + '`$(`$nic1.MacAddress.Replace(':','-'))' {
| + ~
| Missing statement block in switch statement clause.
| At line:35 char:1
| + }
| + ~
| Unexpected token '}' in expression or statement.
| + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
| + FullyQualifiedErrorId : MissingVariableNameAfterForeach
|
|
-----------------------------------------------------------------------------------------------------------------------
The error message still shows that back-tick before $nic.
Are you sure you called Invoke-VMScript with the result from ExpandString?
Also, I'm not sure if $nic1 should still be in there.
Isn't that a variable you define in your main script, and hence should be substituted by ExpandString.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
ok I've resolved my issue.
last update:
$nic1Name = 'LAN'
$nicIP = 'xxx'
$nicGW = 'xxx'
$dns1 = 'xxx'
$dns2 = 'xxx'
$nic1 = Get-NetworkAdapter -VM $updateTempName -Name 'Network adapter 1'
$code = @'
foreach(`$nic in Get-NetAdapter){
switch(`$nic.MacAddress){
$($nic1.MacAddress.Replace(':','-'))
{
Rename-NetAdapter -InputObject `$nic -NewName '$nic1Name'
New-NetIpAddress -InterfaceAlias '$nic1Name' -IPAddress '$nicIP' -PrefixLength 24 -DefaultGateway '$nicGW'
Set-DnsClientServerAddress -InterfaceAlias 'LAN' -ServerAddresses $dns1,$dns2
Set-NetIPInterface -InterfaceAlias '$nic1Name' -AutomaticMetric disabled -InterfaceMetric 5
}
Default {
Write-Error 'Unknown MAC address found'
}
}
}
Get-NetAdapter | Select Name,MacAddress
'@
$code1 = $ExecutionContext.InvokeCommand.ExpandString($code)
Invoke-VMScript -VM $updateTempName -ScriptText $code1 -Verbose -GuestUser $updateTempUser -GuestPassword $updateTempPass -ScriptType Powershell