Loop for Hostnames

Hi

I’m new :innocent:

I’ve found this pretty usefully PowerShell script for checking users in local Admin group.

invoke-command {
$members = net localgroup Administrators | where {$_ -AND $_ -notmatch “command completed successfully”} | select -skip 4
New-Object PSObject -Property @{
Computername = $env:COMPUTERNAME
Group = “Administrators”
Members=$members
}
} -computer Host-001,Host-002,Host-003 -HideComputerName | Select * -ExcludeProperty RunspaceID | Export-CSV c:\Install\local_admins.csv -NoTypeInformation

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.

Any help?

Thanks :slight_smile:

Hi, welcome to the forum. What exactly would you like help with?

Hi Itlnau,

You would need to put your invoke command inside a loop for each of the hostnames you have soo…

$hostnames = @(1..100)
foreach($h in $hostnames){

  Invoke-command {
  ...... } -computer "Host-$h"

}

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”.

Thats exactly the case. Any suggestions how to solve this?

As described:

  • Above script with loop from 001 to 100
  • and skip not reachable hosts

Thx :slight_smile:

You’ll need to explain the problem you’re having. Is it generating a list of hosts with 001-100? Is it writing an error?

I’m not sure where it gets unclear, but:

  1. PowerShell Script for
  2. checking all PC’s within a rang of Hostname001 to Hostname100
  3. whom is within the local Administrators group
  4. the output is a CSV list with all hostnames (hostname & groups and user within local administrators)
  5. if the client is offline or the access is prohibited note an error in CSV file und move to next one.

Hope this helps :slight_smile:

There are some simple methods you could use to solve these. For testing to see if a host is online, you can use the command Test-Connection

Just to make this clear, this forum is not about us creating a script for you, we are here to help with problems in your own scripts.

As Krzydoug mentioned, what problems are you having? What have you tried to generate the hostnames?

1 Like

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 :slight_smile:

Using WMI:

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.

Thank you for the response!

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.

Thank you

Is there not a better way to get a list of hosts? Cant you grab them from AD?

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"

}