Running a script on multiple machines

Hey! I recently got into using powershell and am writing a script to automate our quality checks on our imaged devices. The script below works perfectly fine but I am looking to run it against possibly 40 machines at once. I have attempted to use a text file and use the foreach command. I have also attempt to prompt for the number of computers and prompt the user 40 times for computer name(but that seems terrible). Let me know what you guys think your best solution to this problem would be.

#Promting for needed information
$Computer = Read-Host -Prompt 'Input the computer name'
$Computer1 =  "$Computer$"
$GCinfo = Invoke-Command -ComputerName $Computer -ScriptBlock {Get-ComputerInfo}
$BLinfo = Invoke-Command -ComputerName $Computer -ScriptBlock {Get-BitlockerVolume}
$Mcaf1 = Invoke-Command -ComputerName $Computer -ScriptBlock {Get-service McAfeeDLPAgentService}
$Cort2 = Invoke-Command -ComputerName $Computer -ScriptBlock {Get-service cyserver}
$GCinfo = Invoke-Command -ComputerName $Computer -ScriptBlock {Get-ComputerInfo}
$winver = "2009"

"ADgroup" , "ADgroup" , "ADgroup" | Add-ADGroupMember -Members $Computer1
Invoke-GPUpdate -Computer $Computer -Force

#Testing Install
$testWinver = if($GCinfo.WindowsVersion -eq "$winver"){
    write-output "PASSED"
    }
    else {
    Write-Output "FAILED" 
    }

$bitLocker = if($blinfo.ProtectionStatus -eq 'On' -or $BLinfo.VolumeStatus -eq 'EncryptioninProgress'){
    write-output "PASSED"
    }
    else {
    write-output "FAILED"
    }

$mcAfee = if($Mcaf1.Status -eq 'Running'){
    Write-Output "PASSED"
    }
    else {
    Write-Output "FAILED"
    }

$corTex = if($Cort2.Status -eq 'Running'){
    Write-Output "PASSED"
    }
    else {
    Write-Output "FAILED"
    }
Write-Output "Computer Name - "$GCinfo.PSComputerName "" "Serial Number - "$GCinfo.BiosSeralNumber "" $testWinver "" $bitLocker "" $mcAfee "" $corTex ""
}

John,
Welcome to the forum. :wave:t4:

That’s a good idea. :+1:t4:

That’s unnecessary. :point_up_2:t4:

That’s a horrible idea - please don’t do that to nobody. :raised_hand:t4:

You run 5 separate remote commands to the same computer and two them do the same. That’s unnecessary and hard to read and hard to maintain.

A lot easier would it be this way:

$ComputerList = 'Computer1', 'Computer2', 'Computer3'

$ResultList = 
Invoke-Command -ComputerName $ComputerList -ScriptBlock {

    $ComputerInfo = Get-ComputerInfo 
    $BitLockerVolume = Get-BitlockerVolume
    $McAfeeDLPAgentService = Get-service McAfeeDLPAgentService 
    $cyserverService = Get-service cyserver 

    [PSCustomObject]@{
        ComputerName              = $env:COMPUTERNAME
        WindowsVersion            = $ComputerInfo.WindowsVersion
        BitLockerProtectionStatus = $BitLockerVolume.ProtectionStatus
        BitLockerVolumeStatus     = $BitLockerVolume.VolumeStatus
        McAfeeDLPAgentService     = $McAfeeDLPAgentService.Status
        cyserverService           = $cyserverService.Status
    }
}
$ResultList | 
    Export-Csv -Path 'Path where you want to save the CSV file' -NoTypeInformation

That’s just a first suggestion for an attempt and covers just the remote execution part of your code and there’s a lot of room for improvement to be made by you but I hope you get the point. :wink:

The fact that you just turned my 3 page paper into a paragraph makes me want to cry but thats amazing. Thanks for that I wasted alot of time the ‘pass fail ifs’. Im having an issue though where I keep trying to call on a .txt file for the computer list but I continue to get an error that the names arent valid. How does the txt file need to be written to work with the script?

One computername per line.

Weird thats how I have been writing it. I also tried going through a csv with the same issue.

You may share some sample data and the code you used. (both formatted as code please :wink: )

You may share some sample data and the code you used. (both formatted as code please :wink: )

I was just trying to run the code you provided with the csv file in place of the actual names.

$ComputerList = 'C:\Users\applebottomj\Desktop\PowerShell-Scripts\WKSnames.csv'

$ResultList = 
Invoke-Command -ComputerName $ComputerList -ScriptBlock {

    $ComputerInfo = Get-ComputerInfo 
    $BitLockerVolume = Get-BitlockerVolume
    $McAfeeDLPAgentService = Get-service McAfeeDLPAgentService 
    $cyserverService = Get-service cyserver 

    [PSCustomObject]@{
        ComputerName              = $env:COMPUTERNAME
        WindowsVersion            = $ComputerInfo.WindowsVersion
        BitLockerProtectionStatus = $BitLockerVolume.ProtectionStatus
        BitLockerVolumeStatus     = $BitLockerVolume.VolumeStatus
        McAfeeDLPAgentService     = $McAfeeDLPAgentService.Status
        cyserverService           = $cyserverService.Status
    }
}
$ResultList | 
    Export-Csv -Path 'C:\Users\applebottomj\Desktop\QCList\Testing.csv' -NoTypeInformation

With this line …

$ComputerList = 'C:\Users\applebottomj\Desktop\PowerShell-Scripts\WKSnames.csv'

… you assigned the path to the CSV to the variable $ComputerList. What you need is to read the file and assign the ouput to the variable. In case of a CSV file you can use the cmdlet

Assumed your CSV file has headers and the header for the computernames is “ComputerName” you could use something like this:

$ComputerList = Import-CSV -Path 'C:\Users\applebottomj\Desktop\PowerShell-Scripts\WKSnames.csv' 

$ResultList = 
Invoke-Command -ComputerName $ComputerList.ComputerName -ScriptBlock {
.......

With a plain text file with computernames one per line you can use

to read the file and the code should look something like this:

$ComputerList = Get-Content -Path 'C:\Users\applebottomj\Desktop\PowerShell-Scripts\WKSnames.txt' 

$ResultList = 
Invoke-Command -ComputerName $ComputerList -ScriptBlock {
.......

Please notice the difference in the Invoke-Command line …

Please always read the complete help including the examples for the cmdlets you’re about to use to learn how to use them.

Thanks! Ill make sure to go through and read those. Thanks for your help!!