Using PSComputerName in Workflow gives no results

This topic contains 2 replies, has 2 voices, and was last updated by  Neville D’Souza 3 months ago.

  • Author
    Posts
  • #94158

    Neville D’Souza
    Participant

    Hi All,

    I have a script that goes to each server checks if the server has rebooted in last 24 hours and if it has it checks which windows updates were installed in the last 24 hours and which automatic services are in stopped state after reboot.

    $Machines = Get-Content C:\Scripts\ServersList.csv
    $LastBootDate = @() 
    $TodayDate = @() 
    $Difference = @() 
    
    foreach ($Machine in $Machines)
    
        {
            $LastBootDate = Get-WmiObject –Class Win32_OperatingSystem -ComputerName $Machine | Select-Object csname, @{LABEL='LastBootUpTime';Expression={$_.ConverttoDateTime($_.lastbootuptime)}}
            $TodayDate = Get-Date
            $Difference = New-TimeSpan –Start $LastBootDate.LastBootUpTime.Date –End $TodayDate.Date 
    
           if ($Difference.Hours -lt '24' ) { 
                                              Get-WmiObject -Class "win32_quickfixengineering" -ComputerName $Machine | Where-Object {($_.InstalledOn) -ge (Get-Date).AddDays(-1).Date} |
                                              Select-Object -Property "csname","Description", "HotfixID", @{Name="InstalledOn"; Expression={([DateTime]($_.InstalledOn)).ToLocalTime()}}
    
                                              Get-Service -ComputerName $Machine | where { $_.status -ne 'Running' -and $_.Starttype -eq 'Automatic' }  | 
                                              Select-Object MachineName,Name,DisplayName,Status,StartType | ft
                                            }
           
           else { "$Machine did not reboot yesterday"} 
        } 
    

    The above scripts runs well but is very time consuming since it goes to each server one by one and gives the result on the screen. I heard of workflows which allow you to run the same script in parallel. So I have tried the below script which does not give any result

    I added the workflow statements and -parallel and -throttlelimit. Finally changed ComputerName to PSComputerName, but not result after running it.

    
    Workflow FindPatchedServers
    { 
    $Machines = Get-Content C:\Scripts\ServersList.csv
    $LastBootDate = @() 
    $TodayDate = @() 
    $Difference = @() 
    
    foreach -Parallel -ThrottleLimit 50 ($Machine in $Machines)
    
        {
            $Workflow:LastBootDate = Get-WmiObject –Class Win32_OperatingSystem -PSComputerName $Machine | Select-Object csname, @{LABEL='LastBootUpTime';Expression={$_.ConverttoDateTime($_.lastbootuptime)}}
            $Workflow:TodayDate = Get-Date
            $Workflow:Difference = New-TimeSpan –Start $LastBootDate.LastBootUpTime.Date –End $TodayDate.Date 
    
           if ($Difference.Hours -lt '24' ) { 
                                              Get-WmiObject -Class "win32_quickfixengineering" -PSComputerName $Machine | Where-Object {($_.InstalledOn) -ge (Get-Date).AddDays(-1).Date} |
                                              Select-Object -Property "csname","Description", "HotfixID", @{Name="InstalledOn"; Expression={([DateTime]($_.InstalledOn)).ToLocalTime()}}
    
                                              Get-Service -PSComputerName $Machine | where { $_.status -ne 'Running' -and $_.Starttype -eq 'Automatic' }  | 
                                              Select-Object MachineName,Name,DisplayName,Status,StartType 
                                            }
           
           else { "$Machine did not reboot yesterday"} 
        } 
    }
    
    

    Can anyone help me with this please?

  • #94260

    iain Barnetson
    Participant

    I ran your workflow against two severs here and it returned the information requested but also an error. I'm pretty sure the error was caused by the difference in Powershell versions on the machine I ran it from and those it queried, but I don't know for certain.

    I highlighted the entire workflow, ran it, then ran the command: FindPatchedServers

    Errors:

    
    Microsoft.PowerShell.Utility\Write-Error : A parameter cannot be found that matches parameter name 'InformationAction'.
    At FindPatchedServers:22 char:22
    + 
        + CategoryInfo          : NotSpecified: (:) [Write-Error], RemoteException
        + FullyQualifiedErrorId : System.Management.Automation.RemoteException,Microsoft.PowerShell.Commands.WriteErrorCommand
        + PSComputerName        : [localhost]
     
    Microsoft.PowerShell.Utility\Write-Error : A parameter cannot be found that matches parameter name 'InformationAction'.
    At FindPatchedServers:22 char:22
    + 
        + CategoryInfo          : NotSpecified: (:) [Write-Error], RemoteException
        + FullyQualifiedErrorId : System.Management.Automation.RemoteException,Microsoft.PowerShell.Commands.WriteErrorCommand
        + PSComputerName        : [localhost]
     

    Results:

    
    CSName                : server1
    Description           : Update
    HotFixID              : KB4040972
    InstalledOn           : 9/27/2017 7:00:00 PM
    PSComputerName        : localhost
    PSSourceJobInstanceId : 14fd53cd-0c5b-4443-abc3-2f6443b37249
    
    CSName                : server1
    Description           : Update
    HotFixID              : KB4040981
    InstalledOn           : 9/27/2017 7:00:00 PM
    PSComputerName        : localhost
    PSSourceJobInstanceId : 14fd53cd-0c5b-4443-abc3-2f6443b37249
    
    CSName                : server2
    Description           : Security Update
    HotFixID              : KB4038792
    InstalledOn           : 9/27/2017 7:00:00 PM
    PSComputerName        : localhost
    PSSourceJobInstanceId : 14fd53cd-0c5b-4443-abc3-2f6443b37249
    
    CSName                : server2
    Description           : Security Update
    HotFixID              : KB2894852
    InstalledOn           : 1/25/2016 6:00:00 PM
    PSComputerName        : localhost
    PSSourceJobInstanceId : 14fd53cd-0c5b-4443-abc3-2f6443b37249
    
    CSName                : server2
    Description           : Security Update
    HotFixID              : KB2894856
    InstalledOn           : 9/8/2014 7:00:00 PM
    PSComputerName        : localhost
    PSSourceJobInstanceId : 14fd53cd-0c5b-4443-abc3-2f6443b37249
    
  • #94317

    Neville D’Souza
    Participant

    Yeah unfortunately it gives wrong results. Instead of showing updates installed in the last 24 hours it shows all updates. Not sure how to fix that. It runs properly without workflow.

You must be logged in to reply to this topic.