Hyper-V:制作一个脚本,提取计算机名称的后6位数字,将其转换为十六进制,然后插入为VM的MAC地址(后8位)

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

我当前正在使用Hyper-V,我想将静态MAC地址更改为计算机名称的最后6位数字。 MAC地址以十六进制表示。计算机名称是由另一个脚本生成的随机字母和随机数字的组合。

到目前为止,我有这个:

$lastSixChar = $env:COMPUTERNAME.Substring($env:COMPUTERNAME.Length - 6)
$convertHex = $test | Format-Hex

这只是获取计算机的名称并将其转换为十六进制。我不确定这是否是拉动十六进制的最明智的方法,但至少它会打印正确的数字。我真的不知道该如何使用我提取的信息并将其放入Hyper-V中MAC的后8位。

什么都可以!谢谢

编辑:找到了:Set-VMNetworkAdapter -VMName SRV01 -StaticMacAddress“ 00112233445566”

powershell hyper-v mac-address
1个回答
0
投票

您发布的代码似乎没有使用十六进制字符串。所以下面做。 [grin]因为您没有发布如何制作第一个两个MAC地址块,并且还没有回复关于该主题的评论,所以我省略了。

我没有任何虚拟机资料,因此我将把您添加到问题中的内容留作问题那部分的答案。

# fake reading in a text file
#    in real life, use Get-Content
#    or use one of the AD or VM cmdlets and filter out all but the system name values
$SystemNameList = @'
pc1234567890
pc2a3s4d5f6g
pc3z4x5c6v7b
'@ -split [System.Environment]::NewLine

$Results = foreach ($SNL_Item in $SystemNameList)
    {
    $HexList = @()
    # the "-replace" strips out all but the last 6 chars in the string
    foreach ($SB_Item in ($SNL_Item  -replace '.+(.{6})$', '$1').ToCharArray())
        {
        # this uses the string format operator to convert the int to hex
        $HexList += '{0:x0}' -f [int]$SB_Item
        }

    $HexList -join '-'
    }

$Results

输出...

35-36-37-38-39-30
34-64-35-66-36-67
35-63-36-76-37-62
© www.soinside.com 2019 - 2024. All rights reserved.