none
Powershell workflow - output hashtable with parameter variable RRS feed

  • Question

  • Hi guys,

    I need to get list of applied patches for last couple of months for long list of servers cca 1000.

    In order to get it I done faster I'm using workflow foreach -paralell...

    Script from  Michael Goulart through function hotfixreport - I've modified in workflow version:

    
    
    workflow Hotfixreport 
    
        { 
                param ($computers)
                   ForEach -parallel -throttlelimit 30 ($computer in $computers) 
                               {  
                                 $computer
                                  try  
                                    { 
                                        $computer
                                        get-hotfix -PScomputername $computer  | where-object {$_.InstalledOn -ge (Get-Date).AddMonths(-3)} | Sort-Object -Property InstalledOn -Descending | Select-Object @{Label="Server";Expression={$computer}},
                                                                                                                                                                                                           @{Label="CSName";Expression={$_.CSname}},
                                                                                                                                                                                                           @{Label="hotfixID";Expression={$_.hotfixid}},
                                                                                                                                                                                                           installedby,                     
                                                                                                                                                                                                           installedon
                                     } 
    
                                catch  
                                    { 
                                        $WORKFLOW:wftest+=  "$computer`n"                           
                                    }  
                               } 
    
                 $wftest | out-file  "C:\hotfix\failed_computer.csv"
        } 
    
    
    $computers = Get-Content "C:\hotfix\computers.txt"
    $output_csv="C:\hotfix\Hotfix output "+(get-date -format "MM-dd-yyyy")+".csv"
    $owners= import-csv -Delimiter "`t" -path "C:\hotfix\Owner_list.csv"
    $hotfix= hotfixreport -computers $computers 
    $hotfix  |ft -AutoSize  

    It does work - in paralell, result is the same as from Michael Goulart script - but my issue is that varaiable $computer uses in workflow:

    @{Label="Server";Expression={$computer}}


    Is always displayed in final result as empty string, which is not (you get it listed if you write it in worfklow).

    $_.CSname is not good  enought- as I need to have FQDN name from the list in varaible $computer, as its used for further actions.

    I've tried:

    $workflow:...

    $using:...

    $_.PSComputername - and all other stuff.

    But esult is always - empty parameter  server in outputed hashtable at the end of workflow.

    If I put the same tihgn without foreach -paralell, it works and parameter is not empty string in final hashtable.

    Can anybody point me in direction how to get it done though workflow paralell?



    • Edited by dok_hook Wednesday, June 12, 2019 8:48 AM
    Tuesday, June 11, 2019 11:25 AM

Answers

  • https://devblogs.microsoft.com/scripting/the-basics-of-windows-powershell-workflow-when-to-use-it-and-why/

    Overall "Worlflow" is an industry concept that addresses a task based system that can define activities and routing. It has been around in WIndows since at least 1998.  Many companies leverage the Windows WF engine.  Other companies supply custom engines and services.

    Workflow has an industry organization and a magazine.

    See: https://workflowotg.com/

    MS supplies a toolkit that is part of Windows and is used by many Windows products including Exchange, SharePoint, Office 365 and Azure.

    PowerShell surfaces a simplified method of access to the WF service and creates the XAML based structure that the WF engine uses to run and manage a set of activities.

    The following book does a very good job of introducing and teaching WF in PowerShell.  It is also one of the best of the books on all PS components.

    https://www.manning.com/books/windows-powershell-in-action-third-edition

    Bruce Payette is the PowerShell language designer and a Microsoft engineer. The book is co-written by Richard Siddaway a system architect and consultant who works on the PowerShell team and has written extensively on PowerShell.


    \_(ツ)_/

    • Marked as answer by dok_hook Thursday, June 13, 2019 9:00 AM
    Wednesday, June 12, 2019 9:51 AM

All replies

  • The computer name is always added to the results.  Don't add it in the workflow.

     hotfixreport -computers $computers  | Select PsComputerName


    \_(ツ)_/

    Tuesday, June 11, 2019 11:51 AM
  • This output is giving always value: localhost

    What I want to get is each and every name from the list of $computers that is run through foreach -paralell ( .. in $computers)


    Tuesday, June 11, 2019 12:30 PM
  • Please post scripts correctly.  Edit you original post and fix so it is readable and in the code posting tool provided.


    \_(ツ)_/

    Tuesday, June 11, 2019 3:06 PM
  • Start simple until you understand how this works then add a pieces at a time and try ro understand what you are doing wrong:

    workflow Hotfixreport{
        param(
            [string[]]$computers
        )
        
        ForEach -parallel ($computer in $computers) {
            try{
                get-hotfix -PScomputername $computer 
            } 
            catch{
                #$WORKFLOW:wftest += "$computer`n"
            }
        }
        #$wftest | out-file  "C:\hotfix\failed_computer.csv"
    }
    Hotfixreport $computer | Select-Object PscomputerName, HotFixId, installedon

    Clean, neat, properly formatted code are the first things to address.  Only use a command or syntax element once you fully understand what it does.

    Note that "Workflow" is NOT PowerShell code.  It uses PS syntax but the WF engine converts it to WF code.


    \_(ツ)_/

    Tuesday, June 11, 2019 3:23 PM
  • Jrv,

    You helpped, when I put it this way it worked.

    Sorry for understending you right away and for not putting code in proper format. I've edited initial post so it looks better.

    Agree with the fact that I don't understand how WF works, that's why I do have problems. Went through basic course and red this article: https://docs.microsoft.com/en-us/system-center/sma/overview-powershell-workflows?view=sc-sma-2019 guess not as good student as I sould be.

    Spent couple of days googleing, try-and-fail with all sort of things that I red, and could think of. 

    You really saved me.

    If I don't get my hands dirty - will never learn :-). 

    In case you know some better resources for workflow reading - please point me.

    Thanks again!

    Wednesday, June 12, 2019 9:24 AM
  • https://devblogs.microsoft.com/scripting/the-basics-of-windows-powershell-workflow-when-to-use-it-and-why/

    Overall "Worlflow" is an industry concept that addresses a task based system that can define activities and routing. It has been around in WIndows since at least 1998.  Many companies leverage the Windows WF engine.  Other companies supply custom engines and services.

    Workflow has an industry organization and a magazine.

    See: https://workflowotg.com/

    MS supplies a toolkit that is part of Windows and is used by many Windows products including Exchange, SharePoint, Office 365 and Azure.

    PowerShell surfaces a simplified method of access to the WF service and creates the XAML based structure that the WF engine uses to run and manage a set of activities.

    The following book does a very good job of introducing and teaching WF in PowerShell.  It is also one of the best of the books on all PS components.

    https://www.manning.com/books/windows-powershell-in-action-third-edition

    Bruce Payette is the PowerShell language designer and a Microsoft engineer. The book is co-written by Richard Siddaway a system architect and consultant who works on the PowerShell team and has written extensively on PowerShell.


    \_(ツ)_/

    • Marked as answer by dok_hook Thursday, June 13, 2019 9:00 AM
    Wednesday, June 12, 2019 9:51 AM