Hyper-V Powershell - 将冒号添加到 MAC 地址
Hyper-V Powershell - add colons to MAC address
我有多个 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
我尝试在将集合转换为字符串之前添加 :
$mac=$out.MacAddress
$mac=$mac.Insert(2,":").Insert(5,":").Insert(8,":").Insert(11,":").Insert(14,":")
但是得到:
Exception calling "Insert" with "2" argument(s): "Collection was of a fixed size."
At line:35 char:6
+ $mac=$mac.Insert(2,":").Insert(5,":").Insert(8,":").Insert(11,":").Insert(1 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : NotSupportedException
您不需要 foreach
$mac
只需使用 after(if mac 格式 XX-xx-xx-xx-xx-xx)
if ($mac.count -gt 1) {
$mac = $mac -join " "
}
$mac.Replace('-',':')
你可以这样做(如果 mac 格式为 xxxxxxxxxxxx):$mac=$out.MacAddress|foreach{($_.Insert(2,":").Insert(5,":").Insert(8,":").Insert(11,":").Insert(14,":"))-join " "}
并且可以删除这个:
if ($mac.count -gt 1) { $mac = $mac -join " " } foreach($m in $mac) { $mac=$m.Insert(2,":").Insert(5,":").Insert(8,":").Insert(11,":").Insert(14,":") }
正如 Lee_Dailey 正确指出的那样:$out.MacAddress
包含一个字符串数组。您想遍历每个元素并插入冒号,然后再将它们连接成一个字符串:
$mac = $out.MacAddress
$macWithColons = foreach ($m in $mac)
{
$m.Insert(2, ":").Insert(5, ":").Insert(8, ":").Insert(11, ":").Insert(14, ":")
}
$macWithColons -join " "
简而言之,您可以这样做以将冒号放在 Mac 地址中,并将它们合并为单个字符串,并使用 space 作为分隔符:
$mac = ($out.MacAddress | ForEach-Object {
$_.Insert(2,":").Insert(5,":").Insert(8,":").Insert(11,":").Insert(14,":")
}) -join ' '
Write-Output "$virtm, $ip, $mac"
这个怎么样? "$&" 表示整个匹配。行尾也有负先行,因此冒号不会放在末尾。
$mac = echo 00155D58125F 00155D58125G 00155D58125H
$mac = $mac -replace '..(?!$)','$&:'
$mac
00:15:5D:58:12:5F
00:15:5D:58:12:5G
00:15:5D:58:12:5H
我有多个 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
我尝试在将集合转换为字符串之前添加 :
$mac=$out.MacAddress
$mac=$mac.Insert(2,":").Insert(5,":").Insert(8,":").Insert(11,":").Insert(14,":")
但是得到:
Exception calling "Insert" with "2" argument(s): "Collection was of a fixed size."
At line:35 char:6
+ $mac=$mac.Insert(2,":").Insert(5,":").Insert(8,":").Insert(11,":").Insert(1 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : NotSupportedException
您不需要 foreach
$mac
只需使用 after(if mac 格式 XX-xx-xx-xx-xx-xx)
if ($mac.count -gt 1) {
$mac = $mac -join " "
}
$mac.Replace('-',':')
你可以这样做(如果 mac 格式为 xxxxxxxxxxxx):$mac=$out.MacAddress|foreach{($_.Insert(2,":").Insert(5,":").Insert(8,":").Insert(11,":").Insert(14,":"))-join " "}
并且可以删除这个:
if ($mac.count -gt 1) { $mac = $mac -join " " } foreach($m in $mac) { $mac=$m.Insert(2,":").Insert(5,":").Insert(8,":").Insert(11,":").Insert(14,":") }
正如 Lee_Dailey 正确指出的那样:$out.MacAddress
包含一个字符串数组。您想遍历每个元素并插入冒号,然后再将它们连接成一个字符串:
$mac = $out.MacAddress
$macWithColons = foreach ($m in $mac)
{
$m.Insert(2, ":").Insert(5, ":").Insert(8, ":").Insert(11, ":").Insert(14, ":")
}
$macWithColons -join " "
简而言之,您可以这样做以将冒号放在 Mac 地址中,并将它们合并为单个字符串,并使用 space 作为分隔符:
$mac = ($out.MacAddress | ForEach-Object {
$_.Insert(2,":").Insert(5,":").Insert(8,":").Insert(11,":").Insert(14,":")
}) -join ' '
Write-Output "$virtm, $ip, $mac"
这个怎么样? "$&" 表示整个匹配。行尾也有负先行,因此冒号不会放在末尾。
$mac = echo 00155D58125F 00155D58125G 00155D58125H
$mac = $mac -replace '..(?!$)','$&:'
$mac
00:15:5D:58:12:5F
00:15:5D:58:12:5G
00:15:5D:58:12:5H