Help With some for each authoring

This topic contains 5 replies, has 3 voices, and was last updated by Profile photo of Mark Prior Mark Prior 1 year, 3 months ago.

  • Author
    Posts
  • #33109
    Profile photo of Mark Prior
    Mark Prior
    Participant

    Hi All

    i have the following script which works and displays very nicley when ran, but is extremely messy etc... im trying to clean this up, lets start with this

    $date = "09/10/2015"
    
    Write-host
    Write-host
    Write-host "EURXMBS05 Updates"
    Get-HotFix -ComputerName eurxmbs05 |? installedon -eq $date | Select-Object description,hotfixid,installedby,
    @{l="InstalledOn";e={
    [DateTime]::Parse($_.psbase.properties["installedon"].value,
    $([System.Globalization.CultureInfo]::GetCultureInfo("en-US")))}}
    $xmbs05 = Get-HotFix -ComputerName eurxmbs05 |? installedon -eq $date |measure | Select-Object count
    Write-Host
    Write-host "Update Count"
    Write-host $xmbs05
    Write-host
    
    Write-host "EURXMBS06 Updates"
    Get-HotFix -ComputerName eurxmbs06 |? installedon -eq $date | Select-Object description,hotfixid,installedby,
    @{l="InstalledOn";e={
    [DateTime]::Parse($_.psbase.properties["installedon"].value,
    $([System.Globalization.CultureInfo]::GetCultureInfo("en-US")))}}
    $xmbs06 = Get-HotFix -ComputerName eurxmbs06 |? installedon -eq $date |measure | Select-Object count
    Write-Host
    Write-host "Update Count"
    Write-host $xmbs06
    Write-host
    
    Write-host "EURXHUB01 Updates"
    Get-HotFix -ComputerName eurxhub01 |? installedon -eq $date | Select-Object description,hotfixid,installedby,
    @{l="InstalledOn";e={
    [DateTime]::Parse($_.psbase.properties["installedon"].value,
    $([System.Globalization.CultureInfo]::GetCultureInfo("en-US")))}}
    $hub01 = Get-HotFix -ComputerName eurxhub01 |? installedon -eq $date |measure | Select-Object count
    Write-Host
    Write-host "Update Count"
    Write-host $hub01
    Write-host
    
    Write-host "EURXHUB02 Updates"
    Get-HotFix -ComputerName eurxhub02 |? installedon -eq $date | Select-Object description,hotfixid,installedby,
    @{l="InstalledOn";e={
    [DateTime]::Parse($_.psbase.properties["installedon"].value,
    $([System.Globalization.CultureInfo]::GetCultureInfo("en-US")))}}
    $hub02 = Get-HotFix -ComputerName eurxhub02 |? installedon -eq $date |measure | Select-Object count
    Write-Host
    Write-host "Update Count"
    Write-host $hub02
    Write-host
    
    Write-host "EURXHUB03 Updates"
    Get-HotFix -ComputerName eurxhub03 |? installedon -eq $date | Select-Object description,hotfixid,installedby,
    @{l="InstalledOn";e={
    [DateTime]::Parse($_.psbase.properties["installedon"].value,
    $([System.Globalization.CultureInfo]::GetCultureInfo("en-US")))}}
    $hub03 = Get-HotFix -ComputerName eurxhub03 |? installedon -eq $date | measure | Select-Object count
    Write-Host
    Write-host "Update Count"
    Write-host $hub03
    Write-host
    
    Write-host "EURXHTS01 Updates"
    Get-HotFix -ComputerName eurxhTS01 |? installedon -eq $date | Select-Object description,hotfixid,installedby,
    @{l="InstalledOn";e={
    [DateTime]::Parse($_.psbase.properties["installedon"].value,
    $([System.Globalization.CultureInfo]::GetCultureInfo("en-US")))}}
    $HTS01 = Get-HotFix -ComputerName eurxhTS01 |? installedon -eq $date |measure | Select-Object count
    Write-Host
    Write-host "Update Count"
    Write-host $hts01
    Write-host
    
    Write-host "EURXHTS02 Updates"
    Get-HotFix -ComputerName eurxhTS02 |? installedon -eq $date | Select-Object description,hotfixid,installedby,
    @{l="InstalledOn";e={
    [DateTime]::Parse($_.psbase.properties["installedon"].value,
    $([System.Globalization.CultureInfo]::GetCultureInfo("en-US")))}}
    $hts02 = Get-HotFix -ComputerName eurxhTS02 |? installedon -eq $date |measure | Select-Object count
    Write-Host
    Write-host "Update Count"
    Write-host $hts02
    Write-host
    
    Write-host "EURXHTS03 Updates"
    Get-HotFix -ComputerName eurxhTS03 |? installedon -eq $date | Select-Object description,hotfixid,installedby,
    @{l="InstalledOn";e={
    [DateTime]::Parse($_.psbase.properties["installedon"].value,
    $([System.Globalization.CultureInfo]::GetCultureInfo("en-US")))}}
    $HTS03 = Get-HotFix -ComputerName eurxhTS03 |? installedon -eq $date |measure | Select-Object count
    Write-Host
    Write-host "Update Count"
    Write-host $hts03
    Write-host
    
    Write-host "EURXCASL1 Updates"
    Get-HotFix -ComputerName eurxCASl1 |? installedon -eq $date | Select-Object description,hotfixid,installedby,
    @{l="InstalledOn";e={
    [DateTime]::Parse($_.psbase.properties["installedon"].value,
    $([System.Globalization.CultureInfo]::GetCultureInfo("en-US")))}}
    $Casl1 = Get-HotFix -ComputerName eurxCASl1 |? installedon -eq $date |measure | Select-Object count
    Write-Host
    Write-host "Update Count"
    Write-host $CASl1
    Write-host
    
    Write-host "EURXCASL2 Updates"
    Get-HotFix -ComputerName eurxCASl2 |? installedon -eq $date | Select-Object description,hotfixid,installedby,
    @{l="InstalledOn";e={
    [DateTime]::Parse($_.psbase.properties["installedon"].value,
    $([System.Globalization.CultureInfo]::GetCultureInfo("en-US")))}}
    $Casl2 = Get-HotFix -ComputerName eurxCASl2 |? installedon -eq $date |measure | Select-Object count
    Write-Host
    Write-host "Update Count"
    Write-host $CASl2
    Write-host
    

    i am trying the below but not getting the results i want

    #Get Machines by role
    $cas = Get-QADComputer -LdapFilter '(Name=eurxca*)' | select name
    $hub = Get-QADComputer -LdapFilter '(Name=eurxhu*)' | select name
    $hts = Get-QADComputer -LdapFilter '(Name=eurxht*)' | select name
    $Active05 = Get-QADComputer -LdapFilter '(Name=eurxmbs05)' | select name
    $Active06 = Get-QADComputer -LdapFilter '(Name=eurxmbs05)' | select name
    $pub = Get-QADComputer -LdapFilter '(Name=eurxpub*)' | select name
    
    # Combine the Above
    $Machines = $cas,$hub,$hts,$Active05,$Active06,$pub
    
    #UK Date Prefix 
    $date = "18/12/2015"
    
    $machines | ForEach-Object {
    Get-HotFix  |? installedon -eq $date | Select-Object description,hotfixid,installedby,
    @{l="InstalledOn";e={
    [DateTime]::Parse($_.psbase.properties["installedon"].value,
    $([System.Globalization.CultureInfo]::GetCultureInfo("en-US")))}}
    Get-HotFix |? installedon -eq $date |measure | Select-Object count
    }
    

    the output i would like to be in the below format is a list of the updates etc... and a count at the bottom, i also had problems adding the count in a single command hence running it twice

  • #33111
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    A couple of immediate points. When you're getting the computer name you should use select -expandproperty name. That will give you just the name. You're currently getting an object. Also if you already know the names why bother using get-QADComputer ?

    Get-Hotfix – will be looking at the local computer as you don't tell it to use the names on the pipeline.

    Don't use ? as an alias for Where-Object in scripts.

    your loop should look something like this

    $machines | ForEach-Object {
    
        $hf = Get-HotFix -ComputerName $psitem  |Where-Object installedon -eq $date 
        
        $hf | Select-Object description,hotfixid,installedby,
        @{l='InstalledOn'; 
            e={[DateTime]::Parse($_.psbase.properties['installedon'].value,
        $([System.Globalization.CultureInfo]::GetCultureInfo('en-US')))}}
        
        $hf |Measure-Object | Select-Object count
    }
    
  • #33113
    Profile photo of Dan Potter
    Dan Potter
    Participant

    That's a whole kennel of puppies:-)

  • #33114
    Profile photo of Mark Prior
    Mark Prior
    Participant

    Thanks richard, i know the names but was just trying to allow the script to cope with the addition of any new cas or MBX servers

    the loop looks great, however it does not display the name of the server that the updates have applied to, any idea how i can retrive that ?

    really appreciate the help thanks

  • #33115
    Profile photo of Dan Potter
    Dan Potter
    Participant

    add source to the select statement.

    note the columns(properties) of get-hotfix |select -First 1

    check out the other properties

    get-hotfix |select -First 1 |gm

  • #33150
    Profile photo of Mark Prior
    Mark Prior
    Participant

    many thanks both

You must be logged in to reply to this topic.