是否有一个简单的Powershell脚本根据PnPDeviceID重命名NetConnectionID?如果PnPDeviceID为“ PCI \ VEN”,则将NIC重命名为“ Internet”

我需要基于许多一致的PnPDeviceID重命名NIC卡。

到目前为止,下面的代码是我能够获得的最接近的代码。但是它仅显示NetConnectionID和PnPDeviceID,而不能使用它来操作NetConnectionID。

$interfaces = Get-Wmiobject Win32_NetworkAdapter
$interfaces | foreach {
$name = $_ | Select-Object -ExpandProperty NetConnectionID
if ($name) {
$id = $_.GetRelated("Win32_PnPEntity") | Select-Object -ExpandProperty DeviceID
Write-Output "$name - $id"
}
}

我希望这是一个可以成功重命名NIC卡的简单脚本。

kubabel 回答:是否有一个简单的Powershell脚本根据PnPDeviceID重命名NetConnectionID?如果PnPDeviceID为“ PCI \ VEN”,则将NIC重命名为“ Internet”

我会使用Get-NetAdapter和Rename-NetAdapter代替Get-WmiObject。

https://docs.microsoft.com/en-us/powershell/module/netadapter/get-netadapter?view=win10-ps

https://docs.microsoft.com/en-us/powershell/module/netadapter/rename-netadapter?view=win10-ps

The Get-NetAdapter cmdlet gets the basic network adapter properties. By default only visible adapters are returned.

The Rename-NetAdapter cmdlet renames a network adapter. Only the name,or interface alias can be changed.

#Create your array of adapters
$netAdapters = Get-NetAdapter 

#loop through all adapters
foreach($adapter in $netAdapters)
{

    #Set the requirements that need to be met in order to change the adapter name
    $PnPIDs2Change = "PCI\VEN_8086&DEV_1539&SUBSYS_15391849&REV_03\7085C2FFFFBA0CFC00","PCI\VEN_8086&DEV_1539&SUBSYS_15391849&REV_03\7085C2FFFFBA0CFC01","PCI\VEN_8086&DEV_1539&SUBSYS_15391849&REV_03\7085C2FFFFBA0CFC02","PCI\VEN_8086&DEV_1539&SUBSYS_15391849&REV_03\7085C2FFFFBA0CFC03","PCI\VEN_8086&DEV_1539&SUBSYS_15391849&REV_03\7085C2FFFFBA0CFC04"

    #Since you have "many consistent PnPDeviceID's" you need to loop through each of those PnPIDs and check them against the adapter in question
    foreach($ID in $PnPIDs2Change)
    {

        #If adapter meets the requirements,change the name 
        if($adapter.PnPDeviceID -eq "$ID")
        {
            #Take note of the old name and ID
            $oldName = $adapter.Name
            $adapterPnPID = $adapter.PnPDeviceID
            #Uncomment the 2 lines below in order to actually rename the adapter(set $newName to what you actually want it set to)
            #$newName = "Public - VLAN 1"
            #Rename-NetAdapter -Name $adapter.Name -NewName $newName
            Write-Output "$oldName - $adapterPnPID"
        }
    }
}
本文链接:https://www.f2er.com/3129882.html

大家都在问