Forum Discussion

sebastianj1981's avatar
sebastianj1981
Copper Contributor
Jun 22, 2024

Script to find out if updates are installed on a number of servers

Hello Team,

 

I found some script which is checking if particular updates have been installed on remote servers. This script might be very helpfull but unfortunatelly it is not working. I'm the beginner and don't know how to correct it. May I kindly ask you to help me correct it?

 

$computers=Get-Content .computers.txt
$Kbs=Get-Content .updates.txt
$ErrorActionPreference=“SilentlyContinue”;

ForEach ($computerin$computers) {
add-content“———————— $computer ——————————————————-“-path .hotfixes.txt
ForEach ($Kbin$Kbs{
$Info=$null
$Info=Get-HotFix-cn$computer-ID$Kb
if ($Info-eq$null{
add-content$Kb not installed”-path .hotfixes.txt
}
else {
$InstalledDate=$Info.InstalledOn
add-content$Kb installed $InstalledDate-path .hotfixes.txt
}
}
}

 

Thank you in advance.

  • DTB's avatar
    DTB
    Iron Contributor

    Hi sebastianj1981,

     

    Thank you for sharing your script. I'll help you correct it so it can check if particular updates have been installed on remote servers. Here’s the corrected version of your script with explanations for each change:

     

     

    # Define the list of computers and updates
    $computers = Get-Content -Path ".\computers.txt"
    $Kbs = Get-Content -Path ".\updates.txt"
    $ErrorActionPreference = "SilentlyContinue"
    
    # Loop through each computer
    ForEach ($computer in $computers) {
        # Add a header for the computer in the output file
        Add-Content -Path ".\hotfixes.txt" -Value "---------------------------- $computer ----------------------------"
    
        # Loop through each update
        ForEach ($Kb in $Kbs) {
            $Info = $null
            # Get the hotfix information
            $Info = Get-HotFix -ComputerName $computer -Id $Kb
            if ($Info -eq $null) {
                # If the hotfix is not installed, add this information to the output file
                Add-Content -Path ".\hotfixes.txt" -Value "$Kb not installed"
            } else {
                # If the hotfix is installed, add the installation date to the output file
                $InstalledDate = $Info.InstalledOn
                Add-Content -Path ".\hotfixes.txt" -Value "$Kb installed on $InstalledDate"
            }
        }
    }

     

     

    Explanation of Changes

    1. File Path Corrections:

      • Ensure you provide the correct paths to the files. I added -Path parameters for Get-Content and Add-Content.
    2. Variable Formatting:

      • Corrected the variable names to ensure consistency and readability.
      • Changed add-content to Add-Content to maintain case consistency.
    3. Loop Syntax:

      • Corrected the loop syntax to ensure variables are separated by spaces.
    4. Get-HotFix Command:

      • Fixed the Get-HotFix command to ensure it correctly uses the -ComputerName and -Id parameters.
    5. Output Formatting:

      • Added clearer formatting to the output to make it more readable.

    Additional Tips

    • Error Handling: While SilentlyContinue suppresses errors, consider logging errors to a file for troubleshooting.
    • Permissions: Ensure you have the necessary permissions to query the remote servers.

     

    Conclusion

    This corrected script should now work as intended, checking for the specified updates on each remote server and writing the results to a file. If you have any further questions or need additional assistance, feel free to ask.

     

    Please click Mark as Best Response & Like if my post helped you to solve your issue.

    This will help others to find the correct solution easily. It also closes the item.

    If the post was useful in other ways, please consider giving it Like.

Resources