Need help with simpley powershell code

Hi @all

I don’t know very much powershell. I have a simple question:

I have e environment with 40 virtual Windows Servers (2012R2, 2016 and 2019). Now I woll use a Powershellscript on one Server (Domaincontroller) who will put all installed updates on all servers. Limited to a period. An I will use a file (.txt or .csv) to set the servernames who I will check with this script.

Example: I will set the following in the script and then i can see all installed updates from a period only for this servers:

Server1.domain.com
Server2.domain.com
Server3.domain.com

At the moment I have the following code, but I cannot set a period or use a file to restrict the scanned servers:

Invoke-Command -ComputerName (Get-ADComputer -Filter *).Name {Get-HotFix} -ErrorAction SilentlyContinue | Select-Object PSComputername, HotfixID, InstalledOn | Out-GridView

Any ideas or help?

Thank you

You really should not be using a domain controller for this. It should be done from an admin workstation/server.

You can use Get-Content to read the text file and then create a loop to get the hotfix information for each computer. You can pipe the output of Get-HotFix to Where-Object to filter the dates.

$computers = Get-Content c:\temp\computers.txt

foreach ($computer in $computers) {

    Get-HotFix -ComputerName $computer | 
        Where-Object {$_.InstalledOn -gt (Get-Date '01/07/2020') -and $_.InstalledOn -lt (Get-Date '19/10/2020')} | 
            Select-Object PSComputername, HotfixID, InstalledOn | Out-GridView

}

 

Hi @Matt Bloomfield

Thanks for your help. It works. But I have a littel change/question. I will output the result in a .txt file. I modified the code with “output-file”:

$computers = Get-Content c:\temp\computers.txt

foreach ($computer in $computers) {

Get-HotFix -ComputerName $computer |
Where-Object {$.InstalledOn -gt (Get-Date ‘06/07/2020’) -and $.InstalledOn -lt (Get-Date ‘20/10/2020’)} |
Select-Object PSComputername, Description, HotfixID, InstalledBy, InstalledOn | Out-File -FilePath C:\Temp\ServerupdatesNew.txt

}

The problem is, at the end the .txt-file is empty. But when I add only one server in the comptuers.txt, then it works. Is there a limit? Or do I need a other code?

Thank you.

You are overwriting the text file each time the loop runs. Look at the help for Out-File and you should be able to work out how to append data to a file rather than overwriting it each time.

Yeah, great. I add the “-append” an now it works. Thank you very much :slight_smile:

But one more question. What I need to change, when I will an other Date-Format? At the moment it is the following: MM:DD:YYYY

I will change the input and output to DD:MM:YYYY

Check the help for Get-Date, you can use the -Format parameter to get the desired format.

Get-Date '20/10/2020' -Format 'MM:dd:yyyy'