Need help with simpley powershell code

Welcome Forums General PowerShell Q&A Need help with simpley powershell code

Viewing 6 reply threads
  • Author
    Posts
    • #264413
      Participant
      Topics: 1
      Replies: 0
      Points: 11
      Rank: Member

      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:

      Any ideas or help?

      Thank you

    • #264446
      Participant
      Topics: 2
      Replies: 382
      Points: 523
      Helping Hand
      Rank: Major Contributor

      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.

       

    • #264596
      Participant
      Topics: 0
      Replies: 3
      Points: 13
      Rank: Member

      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.

    • #264602
      Participant
      Topics: 2
      Replies: 382
      Points: 523
      Helping Hand
      Rank: Major Contributor

      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.

    • #264617
      Participant
      Topics: 0
      Replies: 3
      Points: 13
      Rank: Member

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

    • #264623
      Participant
      Topics: 0
      Replies: 3
      Points: 13
      Rank: Member

      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

    • #264668
      Participant
      Topics: 2
      Replies: 382
      Points: 523
      Helping Hand
      Rank: Major Contributor

      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’
Viewing 6 reply threads
  • You must be logged in to reply to this topic.