각 VM에 대한 VM 메모에 Hyper-V VM IP 주소 추가

각 VM에 대한 VM 메모에 Hyper-V VM IP 주소 추가

그래서 나는 이것에 대한 코드가 다음과 같다는 것을 이미 알고 있습니다.

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

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

내 IP 주소를 알아요

IP          
--          
127.0.0.1

전체 코드는 다음과 같습니다

$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" 

하지만 foreach 루프를 추가하면

$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"
}

각 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

루프 없이는 작동하지만 루프에서는 작동하지 않는 이유를 이해할 수 없습니다.

편집 : Pimp Juice IT의 제안에 따라 에코 아웃

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}
}

답변1

문자열이 필요한 Name 매개변수에 VM 개체를 전달하고 있습니다. VM 매개변수는 이 목적을 위한 것입니다.

Set-VM -VM $vm

각 VM의 메모에 192개의 IP 주소를 추가한다고 가정하면 다음과 같이 할 것입니다. VM이 멀티홈인 경우 주소를 쉼표로 구분합니다.

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

도움이 되었기를 바랍니다

관련 정보