Now i’d like to add a loop for Hostname 001 to 100. Hosts which don’t respond should generate a error in the output file whitout stopping the execution.
If you are in a domain environment, I would suggest getting the list of computers from AD. Once you have the list, you will need to test connectivity in your loop as their are almost always stale entries.
Another suggestion. If you don’t want to use/mess with remote powershell, you can still use ADSI or WMI to grab the local administrators group info. If you do go with Invoke-Command, I would add Test-WSMan to your loop for each host before executing. In large domain environments, you will surely run into systems with issues.
Also, use try catch blocks to keep your script moving along when issues are found.
One might also suggest you do a quick google search
invoke-command uses PS remoting and WinRM for connection, therefore to test connection to remote host you would do it like this:
follow up on alex-innes sample code…
$hostnames = @(1..100)
foreach($h in $hostnames){
$Cred = Get-Credential -Message "Credentials are required to access Host-$h"
$TestResult = Test-WSMan -Credential $Cred -ComputerName "Host-$h" -Authentication Negotiate -Port 5985 |
Select-Object ProductVendor, ProductVersion | Format-List
# Show test status for debugging
$TestResult
if ($TestResult)
{
Invoke-command {
# remote command
} -computer "Host-$h" -Credential $Cred
}
This should work if remote host is HTTP enabled which is default, for HTTPS remoting you would add -UseSsl and -Port 5986 to Test-WSMan command and you would also require SSL certificate installed in trusted root of the remote host.
It doesn’t solve the counting problem described by @alex-innes →
Your only issue with that if your hosts are labelled “Host-001” you will need to do some string manipulation to have the number come through as “001”.
Furthermore, there is usually no problem with remote execution, because WinRM works just fine.
My second problem with your skript example is that in the output CSV file gets always overwriten by the last output. This means the CSV file contains only the last host Host-100.
For your hostname problem, I agree with tonyd there must be a better way to get these.
You could write some complicated code to insert the “00” if there is only a single length integer and a “0” for a double length integer, or you could create a CSV with 001…100 and then pull from that.
For your overwriting issue, I would suggest you read the documentation for the command. Export-csv as there is a parameter that will append to a file rather than overwrite.
Agree that Get-ADComputer with some filters is a good way to gather a list to action on.
Aside from that, to make sure the host numbers are strings you could cast them as [string] inside the loop?
I could not figure out how do cast inline so made separate variable but there’s probably a better method.
$hostnames = @(1..100)
foreach($h in $hostnames){
$hostStr = [string]$h
... (Creds and testing) ...
Invoke-Command -Computer "Host-$hostStr"
}