查询所有计算机上的 NetAdapter Wi-Fi 侵略性
Query NetAdapter Wi-Fi aggressiveness on all computers
$Machines = Get-ADComputer -Filter * -SearchBase 'OU=Laptops,OU=Win10Modern,OU=LN,OU=Workstations,DC=cooley,DC=com' | Select-Object Name
$result = foreach ($Machine in $Machines) { if (Test-Connection -ComputerName $Machine -Count 1 -Quiet) { Invoke-Command -ComputerName $Machine -ScriptBlock { Get-NetAdapterAdvancedProperty -Name "Wi-Fi*" -DisplayName "Roaming Aggressiveness" | Select-Object Name,DisplayName, DisplayValue,PSComputerName } } else { Write-Warning "Computer '$Machine' does not respond" } }
在屏幕上输出
$result
Whosebug 不允许我编辑您的原始问题以格式化文本以便于阅读。
如果您的代码是正确的,您不能将 foreach
的结果放入 $result
.
这样的变量中
我建议您将每个变量的结果输出到一个变量中,然后发布这些变量 - 或者 - 将它们写入您可以阅读的文件。
$Machines = Get-ADComputer -Filter * -SearchBase 'OU=Laptops,OU=Win10Modern,OU=LN,OU=Workstations,DC=cooley,DC=com' | Select-Object Name
# generate collection to store data for each outcome
$ComputerNotRespond = @()
$ComputerResponse = @()
foreach ($Machine in $Machines)
{
if (Test-Connection -ComputerName $Machine -Count 1 -Quiet)
{
Invoke-Command -ComputerName $Machine -ScriptBlock {
$Data = Get-NetAdapterAdvancedProperty -Name "Wi-Fi*" -DisplayName "Roaming Aggressiveness" | Select-Object Name,DisplayName,DisplayValue,PSComputerName
}
$ComputerResponse += $Data
}
else
{
$ComputerNotRespond += "Computer ${Machine} does not respond"
}
}
现在您可以将 $ComputerNotRespond 的结果发布到文本文件中:
$ComputerNotRespond | Out-File C:\tmp\computer_not_respond.txt -Append
您还可以将结果转换成 CSV 格式以便稍后阅读:
$ComputerResponse | Export-Csv -Path C:\tmp\computer_response.csv -NoTypeInformation
如评论所述,您正在变量 $Machines
中收集 对象 ,而您期望的只是字符串。
尝试
$Machines = Get-ADComputer -Filter * -SearchBase 'OU=Laptops,OU=Win10Modern,OU=LN,OU=Workstations,DC=cooley,DC=com' | Select-Object -ExpandProperty Name
# or shorter:
# $Machines = (Get-ADComputer -Filter * -SearchBase 'OU=Laptops,OU=Win10Modern,OU=LN,OU=Workstations,DC=cooley,DC=com').Name
$result = foreach ($Machine in $Machines) {
if (Test-Connection -ComputerName $Machine -Count 1 -Quiet) {
Invoke-Command -ComputerName $Machine -ScriptBlock {
Get-NetAdapterAdvancedProperty -Name "Wi-Fi*" -DisplayName "Roaming Aggressiveness" |
Select-Object Name, DisplayName, DisplayValue, PSComputerName
}
}
else {
Write-Warning "Computer '$Machine' does not respond"
}
}
# output on screen
$result | Format-Table -AutoSize
# output to CSV
$result | Export-Csv -Path 'X:\Somewhere\WiFiAgressiveness.csv' -NoTypeInformation
$Machines = Get-ADComputer -Filter * -SearchBase 'OU=Laptops,OU=Win10Modern,OU=LN,OU=Workstations,DC=cooley,DC=com' | Select-Object Name
$result = foreach ($Machine in $Machines) { if (Test-Connection -ComputerName $Machine -Count 1 -Quiet) { Invoke-Command -ComputerName $Machine -ScriptBlock { Get-NetAdapterAdvancedProperty -Name "Wi-Fi*" -DisplayName "Roaming Aggressiveness" | Select-Object Name,DisplayName, DisplayValue,PSComputerName } } else { Write-Warning "Computer '$Machine' does not respond" } }
在屏幕上输出
$result
Whosebug 不允许我编辑您的原始问题以格式化文本以便于阅读。
如果您的代码是正确的,您不能将 foreach
的结果放入 $result
.
我建议您将每个变量的结果输出到一个变量中,然后发布这些变量 - 或者 - 将它们写入您可以阅读的文件。
$Machines = Get-ADComputer -Filter * -SearchBase 'OU=Laptops,OU=Win10Modern,OU=LN,OU=Workstations,DC=cooley,DC=com' | Select-Object Name
# generate collection to store data for each outcome
$ComputerNotRespond = @()
$ComputerResponse = @()
foreach ($Machine in $Machines)
{
if (Test-Connection -ComputerName $Machine -Count 1 -Quiet)
{
Invoke-Command -ComputerName $Machine -ScriptBlock {
$Data = Get-NetAdapterAdvancedProperty -Name "Wi-Fi*" -DisplayName "Roaming Aggressiveness" | Select-Object Name,DisplayName,DisplayValue,PSComputerName
}
$ComputerResponse += $Data
}
else
{
$ComputerNotRespond += "Computer ${Machine} does not respond"
}
}
现在您可以将 $ComputerNotRespond 的结果发布到文本文件中:
$ComputerNotRespond | Out-File C:\tmp\computer_not_respond.txt -Append
您还可以将结果转换成 CSV 格式以便稍后阅读:
$ComputerResponse | Export-Csv -Path C:\tmp\computer_response.csv -NoTypeInformation
如评论所述,您正在变量 $Machines
中收集 对象 ,而您期望的只是字符串。
尝试
$Machines = Get-ADComputer -Filter * -SearchBase 'OU=Laptops,OU=Win10Modern,OU=LN,OU=Workstations,DC=cooley,DC=com' | Select-Object -ExpandProperty Name
# or shorter:
# $Machines = (Get-ADComputer -Filter * -SearchBase 'OU=Laptops,OU=Win10Modern,OU=LN,OU=Workstations,DC=cooley,DC=com').Name
$result = foreach ($Machine in $Machines) {
if (Test-Connection -ComputerName $Machine -Count 1 -Quiet) {
Invoke-Command -ComputerName $Machine -ScriptBlock {
Get-NetAdapterAdvancedProperty -Name "Wi-Fi*" -DisplayName "Roaming Aggressiveness" |
Select-Object Name, DisplayName, DisplayValue, PSComputerName
}
}
else {
Write-Warning "Computer '$Machine' does not respond"
}
}
# output on screen
$result | Format-Table -AutoSize
# output to CSV
$result | Export-Csv -Path 'X:\Somewhere\WiFiAgressiveness.csv' -NoTypeInformation