0

So I already know that the code for this is

Set-VM -Name "localhost" -Notes "127.0.0.1"

using $ipAddress = get-vm | where { $_.state -eq 'running'} | get-vmnetworkadapter | Select @{Name="IP";Expression={$_.IPAddresses | where {$_ -match "^192\."}}} | Sort VMName

gets my ip address

IP          
--          
127.0.0.1

so the full code is like this

$ipAddress = get-vm | where { $_.state -eq 'running'} | get-vmnetworkadapter | Select @{Name="IP";Expression={$_.IPAddresses | where {$_ -match "^192\."}}} | Sort VMName
Set-VM -Name "server1" -Notes "$ipAddress" 

but when I add the foreach loop

$vms = Get-VM
$ipAddress = get-vm | where { $_.state -eq 'running'} | get-vmnetworkadapter | Select @{Name="IP";Expression={$_.IPAddresses | where {$_ -match "^192\."}}} | Sort VMName

foreach ($vm in $vms) {
    Set-VM -Name "$vm" -Notes "$ipAddress"
}

produces an error like this for each vm

Set-VM : The specified wildcard character pattern is not valid: VirtualMachine (Name = 'server1') [Id = 'de0994fb-c19b-48a3-9389-fd595bd3dc43']
At line:2 char:5
+     Set-VM -Name "$vm" -Notes "$ipAddress"
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Set-VM], VirtualizationException
    + FullyQualifiedErrorId : Unspecified,Microsoft.HyperV.PowerShell.Commands.SetVM

I don't understand why it can work without the loop but not with

EDIT: As per Pimp Juice IT's Suggestion to echo out

VirtualMachine (Name = 'server1') [Id = 'de0994fb-c19b-48a3-9389-fd595bd3dc43'] VirtualMachine (Name = 'server2') [Id = 'ecb80604-1f4a-4493-97cd-14d474baca30'] VirtualMachine (Name = 'server3') [Id = '71a01492-f70e
-420e-9db8-1718ac5d9b41'] VirtualMachine (Name = 'server4') [Id = '500387d7-95e0-44a6-9819-425d4b7af876'] VirtualMachine (Name = 'server5') [Id = '4eef3925-1ae5-4410-8fee-2f99bbfd7ec5'] VirtualMachine (Name = 'serv
er6') [Id = '337f1016-4001-457e-b231-b84173efd695'] = Get-VM
@{IP=192.168.0.39} = get-vm | where { .state -eq 'running'} | get-vmnetworkadapter | Select @{Name=
IP;Expression={.IPAddresses | where { -match ^192\.}}} | Sort VMName
foreach (VirtualMachine (Name = 'server6') [Id = '337f1016-4001-457e-b231-b84173efd695'] in VirtualMachine (Name = 'server1') [Id = 'de0994fb-c19b-48a3-9389-fd595bd3dc43'] VirtualMachine (Name = 'server2') [Id = 'e
cb80604-1f4a-4493-97cd-14d474baca30'] VirtualMachine (Name = 'server3') [Id = '71a01492-f70e-420e-9db8-1718ac5d9b41'] VirtualMachine (Name = 'server4') [Id = '500387d7-95e0-44a6-9819-425d4b7af876'] VirtualMachine (
Name = 'server5') [Id = '4eef3925-1ae5-4410-8fee-2f99bbfd7ec5'] VirtualMachine (Name = 'server6') [Id = '337f1016-4001-457e-b231-b84173efd695']) {
    Set-VM -Name VirtualMachine (Name = 'server6') [Id = '337f1016-4001-457e-b231-b84173efd695'] -Notes @{IP=192.168.0.39}
}
2
  • What do you get when you echo the output instead of executing it? Check to ensure that the output is what you would expect with an echo or something first to help ensure it's not something simple. It may be the looped property you need to reference when looped versus when it's not looped you do not need to reference the property. Start with that if you know what I'm saying to see if that helps. Nov 4, 2019 at 18:07
  • You might also need to put the $ipAddress = logic with the looped logic in case that's the issue. I would test if I could but short on time right now. Nov 4, 2019 at 18:09

1 Answer 1

0

You're passing the VM object to the Name parameter which is expecting a string. The VM parameter is for this purpose.

Set-VM -VM $vm

Assuming you're wanting to add the 192 IP addresses to notes of each VM then I would do it like this. If the VM is multihomed it'll comma separate the addresses.

 $vms = Get-VM | where { $_.state -eq 'running'}
 foreach ($vm in $vms) {
     Set-VM -VM $vm -Notes $($vm.NetworkAdapters.IPAddresses -match "^192\." -join ',')
 }

Hope that helps

1
  • yes thank you! I wasn't aware of -VM
    – bckelley
    Nov 5, 2019 at 2:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .