Hyper-V Powershell-将破折号添加到MAC地址

问题描述 投票:0回答:1

我的虚拟机具有多个IP / MAC地址

我正在使用此代码来获取多个IP / MAC地址:

$vms=Get-VM | Where { $_.State –eq ‘Running’ } | Select-Object -ExpandProperty Name 


 foreach($vm in $vms) {

    $out=Get-VMNetworkAdapter -vmname $vm | select VMName, MacAddress, IPAddresses

    $virtm=$out.VMName

    $ip=$out.IPAddresses



    if ($ip.Count -gt 1){

        foreach($i in $ip.Count) {
         if ($ip -match ':'){

         $ip = $ip | ?{$_ -notmatch ':'}

  }
}         
      $ip = $ip -join " "
      $virtm = ($virtm -split '\n')[0]
}
     else {
     $ip=$out.IPAddresses
       }

    $mac=$out.MacAddress

     if ($mac.count -gt 1) {

    $mac = $mac -join " "
        }

   foreach($m in $mac) {
     $mac=$m.Insert(2,":").Insert(5,":").Insert(8,":").Insert(11,":").Insert(14,":")

    }

     Write-Output "$virtm, $ip, $mac"

此代码可以正常工作,希望它只能将列添加到第一个MAC地址中

当前输出:

OAP80, 192.168.87.45 192.168.1.45, 00:15:5D:58:12:5E 00155D58125F

我想将列添加到特定VM的所有其他MAC地址中

所需的输出

OAP80, 192.168.87.45 192.168.1.45, 00:15:5D:58:12:5E 00:15:5D:58:12:5F
powershell hyper-v
1个回答
0
投票

foreach您不需要$mac只需在

之后使用
if ($mac.count -gt 1) {

    $mac = $mac -join " "
        }
$mac.Replace('-',':')
© www.soinside.com 2019 - 2024. All rights reserved.