PowerShell : Scan bulk list of servers for "software"

If you wish to find if software is installed a large list of servers then this script can save the day, it will scan all the servers in the defined "txt" file (with the name of the servers inside) and check if the software specified is installed in this particular example we are looking for Java installations:

# Define the list of server names
$servers = Get-Content -Path "<path_to_list.txt>"

# Loop through each server
foreach ($server in $servers) {
    Write-Host "Fetching Java version details for $server"

# Define the script block to be executed on the remote server
    $scriptBlock = {
        try {
            $javaInstances = Get-WmiObject -Class Win32_Product | Where-Object { $_.Name -like 'Java*'} | Select-Object -Property Name, Version
           if ($javaInstances) {
                $javaInstances | Format-Table -AutoSize
            } else {
                Write-Host "No Java installations found on $($env:COMPUTERNAME)."
            }
        } catch {
            Write-Host "Error occurred while fetching Java details on $($env:COMPUTERNAME): $_"
        }
    }

# Invoke the script block on the remote server and display the Java version details in the console
    Invoke-Command -ComputerName $server -ScriptBlock $scriptBlock
}

The output will look like this if Java is found:

Fetching Java version details for Exposed.Server

Java 8 Update 25           8.0.250
Java 8 Update 151 (64-bit) 8.0.1510.12
Java Auto Updater          2.8.151.12

Fetching Java version details for Dangerous.Server

Java 8 Update 25           8.0.250
Java 8 Update 151 (64-bit) 8.0.1510.12
Java Auto Updater          2.8.151.12

If no Java is found it will look like this:

No Java installations found on no.java

If you wish to change what you are looking for amend this be sure in change the variable name and the software name in bold below:


  $javaInstances = Get-WmiObject -Class Win32_Product | Where-Object { $_.Name -like 'Java*'} | Select-Object -Property Name, Version
           if ($javaInstances) {
                $javaInstances | Format-Table -AutoSize
Previous Post Next Post

نموذج الاتصال