Powershell GET-ADComputer 列表和 foreach Get WMI_Networkconf
Powershell GET-ADComputer list and foreach Get WMI_Networkconf
我只是想将所有计算机从 AD 中移除,并想显示 IP、DNS 名称、Mac-地址和描述。
我让 WMI 部分在我的本地计算机上工作,但是当我用 AD 计算机列表尝试它时,它会抛出我无法自己解决的错误。
我知道我们的内部网络允许 WMI 读取,因为我们也使用 WMI 监控。
Powershell 代码:
$computerlist = Get-ADComputer -Filter * | select Name
foreach($c in $computerlist)
{
try
{
gwmi -class "Win32_NetworkAdapterConfiguration" -ComputerName $c |
select DNSHostName, MACAddress, IPAddress, Description |
where IPAddress -NotLike "" |
where Description -NotLike "VMware*"
}
catch
{
Write-Warning "$c is unreachable!"
}
}
我认为问题出在 For
循环上。传递名称 $c
并不是将名称作为实际的计算机名称传递,而是作为自定义的 PS 对象传递。如果你执行 Write-Host
:
就可以看到这个
$computerlist = Get-ADComputer -Filter * | select Name
foreach($c in $computerlist)
{
Write-Host $c
}
@{Name=C1234}
您只需要引用 属性 例如$c.Name
所以你的代码看起来像:
$computerlist = Get-ADComputer -Filter * | select Name
foreach($c in $computerlist)
{
try
{
gwmi -class "Win32_NetworkAdapterConfiguration" -ComputerName $c.Name `
|select DNSHostName, MACAddress, IPAddress, Description `
|where IPAddress -NotLike "" `
|where Description -NotLike "VMware*" `
}
catch
{
Write-Warning "$c is unreachable!"
}
}
试试这个并报告回来...
# Use a basic WMI connection check before checking the Network Adapter details
(Get-ADComputer -Filter *).Name |
foreach {
If((gwmi -ComputerName $PSItem -Class win32_process))
{
try
{
gwmi -class 'Win32_NetworkAdapterConfiguration' -ComputerName $PSItem |
select DNSHostName, MACAddress, IPAddress, Description |
where IPAddress -NotLike "" |
where Description -NotLike '*VMware*'
}
catch
{
Write-Warning "When processing $PSItem, some other error occurred!"
}
}
Else
{
Write-Warning -Message "WMI connection failed for host $PSItem, thus not reachable"
}
}
OP 更新
至于……
Thx for the suggestion but i still get the same error message
(HRESULT: 0x800706BA)
正如其他人所说,RPC 不是特定于 PowerShell 的东西,它是服务/资源级别的东西。
See this Q&A
我只是想将所有计算机从 AD 中移除,并想显示 IP、DNS 名称、Mac-地址和描述。
我让 WMI 部分在我的本地计算机上工作,但是当我用 AD 计算机列表尝试它时,它会抛出我无法自己解决的错误。
我知道我们的内部网络允许 WMI 读取,因为我们也使用 WMI 监控。
Powershell 代码:
$computerlist = Get-ADComputer -Filter * | select Name
foreach($c in $computerlist)
{
try
{
gwmi -class "Win32_NetworkAdapterConfiguration" -ComputerName $c |
select DNSHostName, MACAddress, IPAddress, Description |
where IPAddress -NotLike "" |
where Description -NotLike "VMware*"
}
catch
{
Write-Warning "$c is unreachable!"
}
}
我认为问题出在 For
循环上。传递名称 $c
并不是将名称作为实际的计算机名称传递,而是作为自定义的 PS 对象传递。如果你执行 Write-Host
:
$computerlist = Get-ADComputer -Filter * | select Name
foreach($c in $computerlist)
{
Write-Host $c
}
@{Name=C1234}
您只需要引用 属性 例如$c.Name
所以你的代码看起来像:
$computerlist = Get-ADComputer -Filter * | select Name
foreach($c in $computerlist)
{
try
{
gwmi -class "Win32_NetworkAdapterConfiguration" -ComputerName $c.Name `
|select DNSHostName, MACAddress, IPAddress, Description `
|where IPAddress -NotLike "" `
|where Description -NotLike "VMware*" `
}
catch
{
Write-Warning "$c is unreachable!"
}
}
试试这个并报告回来...
# Use a basic WMI connection check before checking the Network Adapter details
(Get-ADComputer -Filter *).Name |
foreach {
If((gwmi -ComputerName $PSItem -Class win32_process))
{
try
{
gwmi -class 'Win32_NetworkAdapterConfiguration' -ComputerName $PSItem |
select DNSHostName, MACAddress, IPAddress, Description |
where IPAddress -NotLike "" |
where Description -NotLike '*VMware*'
}
catch
{
Write-Warning "When processing $PSItem, some other error occurred!"
}
}
Else
{
Write-Warning -Message "WMI connection failed for host $PSItem, thus not reachable"
}
}
OP 更新
至于……
Thx for the suggestion but i still get the same error message (HRESULT: 0x800706BA)
正如其他人所说,RPC 不是特定于 PowerShell 的东西,它是服务/资源级别的东西。
See this Q&A