locked
Looping through a list of servers that require different credentials RRS feed

  • Question

  • I have a list of servers, a list of domain\usernames, and list of passwords.  Each file is individual and matches to each line in each file line by line.  For example, the first line of the servers requires the first line of the usernames which uses the first line of the password file. 

    I want to run a simple gwmi command against each server and have the results posted to a text file on my local PC.  So far this is the code I have:

    $FS =  get-content -Path L:\FileServerShare\powershell\diskSize\FS.txt
    #This variable above holds the list of server IP addresses I want to authenticate to and run the gwmi command
    
    ForEach($server in $FS)
    	{
            $username = Get-Content -Path L:\FileServerShare\Powershell\Remoting\Creds.txt
    
    #The line above is suppose to get a list of usernames associated line by line with each IP address in the FS.txt
    
            $Password = ConvertTo-SecureString -String (Get-Content L:\FileServerShare\powershell\Remoting\Pass.txt) -AsPlainText -Force 
    
    #The Line above is suppose to take my password and store it within this variable as a secure string
    
            $PSpw = new-object -typename System.Management.Automation.PSCredential("$username", $Password)
    
    #I want this line above to create a new PSCredential object defined by the username and password associated with the IP address the loop is working on at the time
    
            gwmi win32_logicaldisk -ComputerName $server -Credential $PSpw | Format-Table PSComputerName,DeviceId, @{n="Size";e={[math]::Round($_.Size/1GB,2)}},@{n="FreeSpace";e={[math]::Round($_.FreeSpace/1GB,2)}} | Out-file -FilePath L:\FileServerShare\powershell\DiskSize\DiskSizes.txt -NoClobber -Append
    
    #I want this line above to cycle through the list of IP address in the FS.txt and authenticate using the credentials that match
    		
    		} 

    Is there any way I can get it to enumerate and work on each entry in each list and get all of the lists to match line by line.  Like get the script to hit the first server IP in the list in FS.txt.  For that server IP use the username on the same line in the Creds.txt file, and generate a PSCredential object only for that username using the password taken from the same line in the Pass.txt.  At that point run the gwmi command using that server IP address and the newly created PSCredential object that was created.

    Any help would be greatly appreciated, thanks.



    • Edited by BerkleyBoy Tuesday, January 23, 2018 5:49 PM
    Tuesday, January 23, 2018 5:48 PM

All replies

  • hmmmmm; to reiterate. You would like to increase the speed of the process by creating stub instances to run per server group. Then organize the output in such a way that all server groups are separated by which CRED was used.

    Have you though about creating a script for each server group independently and have them run at A) staggered times B) in unison on separate machines?

    Tuesday, January 23, 2018 5:55 PM
  • Not really, in the server list I have an IP address of one server on each domain that I'm trying to run the gwmi command on.  I want to run the gwmi individually on each IP using the appropriate PSCredential Object generated from credentials in my creds.txt list and the password associated with that username from the Pass.txt.
    Tuesday, January 23, 2018 6:17 PM
  • Build one script and launch it with the server group and credentials as a job.  Receive all jobs and sort by job name and server.   That is the easiest and fastest way to do this.


    \_(ツ)_/

    Tuesday, January 23, 2018 6:19 PM
  • To get all lists to match use a CSV instead.  Put everything in one CSV.


    \_(ツ)_/

    Tuesday, January 23, 2018 6:21 PM