Forum Discussion
Write-Host AND output results to text file.
- May 03, 2022
It does a ping/icmp test to the computers listed in the computers.txt file, if the firewall is active.. It won't return the request marking the computer inaccessible. You could try to use Test-Path "\\$($computer)\c$" instead of test-connection -computername.. (Run this as an account who has local admin priviledges on the remote machine, Domain Admin of Global Admin/Device Administator? Also, shouldn't invoke-command -computername (get-content -Path .\computers.txt) be invoke-command -computername $computer ?
charlie4872 Something like this:
$computers = get-content .\computers.txt
foreach ($computer in $computers){
if (Test-Connection -computername $computer -count 1 quiet -erroraction silentlycontinue){
Invoke-Command -ComputerName (Get-Content -Path .\computers.txt) -ArgumentList (Get-Content -Path .\users.txt) -ScriptBlock { $args | Remove-LocalGroupMember -Group "Administrators" -ErrorAction:SilentlyContinue;}}
else {"$computer Offline" | Out-File -Path c:\temp\log.txt -Append}
}
The Write-Host removes the ability to output it to a text-file using out-file
- charlie4872May 03, 2022Brass ContributorHello Harm thanks for the response! The script runs but in the output file it is showing every computer as "Offline" even though some of them are actually online.
- May 03, 2022
It does a ping/icmp test to the computers listed in the computers.txt file, if the firewall is active.. It won't return the request marking the computer inaccessible. You could try to use Test-Path "\\$($computer)\c$" instead of test-connection -computername.. (Run this as an account who has local admin priviledges on the remote machine, Domain Admin of Global Admin/Device Administator? Also, shouldn't invoke-command -computername (get-content -Path .\computers.txt) be invoke-command -computername $computer ?
- LainRobertsonMay 04, 2022Silver Contributor
Harm_Veenstra wrote:... Also, shouldn't invoke-command -computername (get-content -Path .\computers.txt) be invoke-command -computername $computer ?
As a quick explainer: no, this is fine.
-Computer takes a string array, not a string, as an input, meaning this is perfectly fine if the input file contains one computer name per line. Of course, implicit conversion means a string is fine, too (i.e. an explicit type cast isn't required.)
Cheers,
Lain