none
Convert script to workflow RRS feed

  • Question

  • Hi,

    I currently have a script to fix SCCM clients, however it's not very efficient as it takes quite a while to repair multiple devices.

    I figured I could convert it to a workflow and use the foreach -parallel option to run it on multiple machines at the same time. Main reason for using a workflow is that I want to know how long the repair took for each machine and I don't think I can properly keep track of it without one if I don't want to wait a couple of days for a few 100 machines to complete.

    Below is my current script, which will take a while to complete if there's a bunch of computers in my list.

    Function MassRepair() {
        Try {
            $RootPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
            $computers = Get-Content -Path ($RootPath + "\computers.txt")
    
            foreach ($computer in $computers) {
                $getProcess = Get-Process -Name ccmrepair* -ComputerName $computer  
                Write-Host "Starting client repair on $computer." 
                $client = [wmiclass] "\\$computer\root\ccm:sms_client" 
                $repair = $client.RepairClient() 
    
                Write-Host "Wait for the repair to finish (maximum runtime is 10 minutes)." 
                For ($i = 0; $i -le 600; $i++) { 
                    $checkProcess = Get-Process -Name ccmrepair* -ComputerName $computer
                    Start-Sleep 1
                     
                    If ($checkProcess -eq $Null) { 
                        Write-Host "Repair took $i seconds to complete on $computer. Check the logs for more info."  
                        Exit 0
                    } 
                    ElseIf ($i -eq 600) { 
                        Write-Host "The repair is taking too long and will be stopped." 
                        Invoke-Command -ComputerName $computer -ScriptBlock { Get-Process -Name ccmrepair* | Stop-Process -Force } 
                        Exit 1 
                    } 
                } 
            }
        } 
        Catch { 
            Write-Host "Something went wrong."
        }
    }

    I'm not very good with workflows so I'm not really sure if I can pretty much convert it to a workflow or not (I know I have to use some InlineScripts for some cmdlets).

    What would be the easiest way to make this into a workflow? Or is there another way to do this?


    Please remember to mark the post(s), which answered your question.

    Tuesday, April 24, 2018 2:12 PM

Answers

  • Run all repairs first then wait.  They will all run in parallel.

     # start all comuters.
    foreach ($computer in $computers) {
        Write-Host "Starting client repair on $computer." 
        $client = [wmiclass] "\\$computer\root\ccm:sms_client" 
        $repair = $client.RepairClient()
    }
    
    # loop through all computers and wait for completion
    foreach ($computer in $computers) {
        while(Get-Process comrepair* -ComputerName $computer){sleep 5}
        Write-Host "$computer finished"
    }
    


    \_(ツ)_/

    • Marked as answer by Thomas G333 Tuesday, April 24, 2018 2:47 PM
    Tuesday, April 24, 2018 2:31 PM