$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" } }
output on the screen
$result
CodePudding user response:
StackOverflow isn't letting me edit your original question to format the text so it's easier to read.
If your code is correct, you cannot put the Results of a foreach
into a variable like $result
.
I would suggest you output the outcome of each into a variable, then publish those variables -OR- write them to a file you can read.
$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"
}
}
Now you can pubish the outcome of the $ComputerNotRespond to a text file:
$ComputerNotRespond | Out-File C:\tmp\computer_not_respond.txt -Append
And you can get the results into CSV to read later:
$ComputerResponse | Export-Csv -Path C:\tmp\computer_response.csv -NoTypeInformation
CodePudding user response:
As commented, you are collecting objects in variable $Machines
, where you are expecting just strings.
Try
$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