Converting a script Normal Powershell 2 PSWorkflow to Enhance parralelisms

Hello Everyone,

Hope you are doing great, I am just a beginner at Powershell please help me out with the below requirements:

Aim is to convert a normal time consuming powershell script to PS Workflow using parallel techniques.

The PS Script basically reboots a list of servers based on checking 3 important conditions,as following :

#imports a list of servers
$CIReport=Import-Csv D:\servers.csv

do

Foreach ($Computer in $CIReport)   
{
#Checks below conditions
#condition 1
#--------------------------------------------------
$CBSRebootPend = $null      
# Making registry connection to the local/remote computer
$RegCon = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]"LocalMachine",$Computer.CI)  # Query WUAU from the registry
$RegWUAU = $RegCon.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\")
$RegWUAURebootReq = $RegWUAU.GetSubKeyNames()
$WUAURebootReq = $RegWUAURebootReq -contains "RebootRequired"      
# Query PendingFileRenameOperations from the registry
$RegSubKeySM = $RegCon.OpenSubKey("SYSTEM\CurrentControlSet\Control\Session Manager\")
$RegValuePFRO = $RegSubKeySM.GetValue("PendingFileRenameOperations",$null)      
# Closing registry connection
$RegCon.Close()
#--------------------------------------------------

#condition 2
#--------------------------------------------------
# Determine SCCM 2012 Client Reboot Pending Status
# To avoid nested 'if' statements and unneeded WMI calls to determine if the CCM_ClientUtilities class exist, setting EA = 0
$CCMClientSDK = $null
$CCMSplat = @{
NameSpace='ROOT\ccm\ClientSDK'
Class='CCM_ClientUtilities'
Name='DetermineIfRebootPending'
ComputerName=$Computer.CI
ErrorAction='SilentlyContinue'
}
$CCMClientSDK = Invoke-WmiMethod @CCMSplat
If ($CCMClientSDK)
{
If ($CCMClientSDK.ReturnValue -ne 0)
{
Write-Warning "Error: DetermineIfRebootPending returned error code $($CCMClientSDK.ReturnValue)"
}#End If ($CCMClientSDK -and $CCMClientSDK.ReturnValue -ne 0)
If ($CCMClientSDK.IsHardRebootPending -or $CCMClientSDK.RebootPending)
{
$SCCM = $true
}#End If ($CCMClientSDK.IsHardRebootPending -or $CCMClientSDK.RebootPending)
}#End If ($CCMClientSDK)
Else
{
$SCCM = $null
}                         
#-------------------------------------------------------
if($WUAURebootReq -eq "True" -and $SCCM -eq "True")
{
Restart-Computer -ComputerName $Computer -Force
}
else
{
#repeat the loop
}

while("End of servers in the list")

Basically this does the job correctly but it takes huge time ,Kindly help me out transforming this with parallelisms using PSWorkflow model.

I would appreciate any suggestions on it..
Thanks in advance

April 28th, 2015 4:06pm

I would suggest starting by reading some of the many articles on workflows then attempting to convert your script.  Post back when you have a clear question.

Free Windows Admin Tool Kit Click here and download it now
April 28th, 2015 5:15pm

You should also note that few here will pay attention to such a badly formatted script.  It is hard to read which makes it a pain to figure out.

Start by learning basic script formatting styles and techniques.

April 28th, 2015 5:17pm

Note also that your script as posted cannot possibly work.

Here is an example of a scrip that is formatted.  I passed it through an auto-formatter:

#imports a list of servers
$CIReport = Import-Csv D:\servers.csv

Foreach ($Computer in $CIReport) {
    #Checks below conditions
    #condition 1
    #--------------------------------------------------
    $CBSRebootPend = $null
    # Making registry connection to the local/remote computer
    $RegCon = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]"LocalMachine", $Computer.CI)  # Query WUAU from the registry
    $RegWUAU = $RegCon.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\")
    $RegWUAURebootReq = $RegWUAU.GetSubKeyNames()
    $WUAURebootReq = $RegWUAURebootReq -contains "RebootRequired"
    # Query PendingFileRenameOperations from the registry
    $RegSubKeySM = $RegCon.OpenSubKey("SYSTEM\CurrentControlSet\Control\Session Manager\")
    $RegValuePFRO = $RegSubKeySM.GetValue("PendingFileRenameOperations", $null)
    # Closing registry connection
    $RegCon.Close()
    #--------------------------------------------------
    
    #condition 2
    #--------------------------------------------------
    # Determine SCCM 2012 Client Reboot Pending Status
    # To avoid nested 'if' statements and unneeded WMI calls to determine if the CCM_ClientUtilities class exist, setting EA = 0
    $CCMClientSDK = $null
    $CCMSplat = @{
        NameSpace = 'ROOT\ccm\ClientSDK'
        Class = 'CCM_ClientUtilities'
        Name = 'DetermineIfRebootPending'
        ComputerName = $Computer.CI
        ErrorAction = 'SilentlyContinue'
    }
    $CCMClientSDK = Invoke-WmiMethod @CCMSplat
    If ($CCMClientSDK) {
        If ($CCMClientSDK.ReturnValue -ne 0) {
            Write-Warning "Error: DetermineIfRebootPending returned error code $($CCMClientSDK.ReturnValue)"
        }#End If ($CCMClientSDK -and $CCMClientSDK.ReturnValue -ne 0)
        If ($CCMClientSDK.IsHardRebootPending -or $CCMClientSDK.RebootPending) {
            $SCCM = $true
        }#End If ($CCMClientSDK.IsHardRebootPending -or $CCMClientSDK.RebootPending)
    }#End If ($CCMClientSDK)
    Else {
        $SCCM = $null
    }
    #-------------------------------------------------------
    if ($WUAURebootReq -eq "True" -and $SCCM -eq "True") {
        Restart-Computer -ComputerName $Computer -Force
    } else {
        #repeat the loop
    }
}    

    

It is not good but it is better.  THe syntax errors prevent the formatter from working well.

Free Windows Admin Tool Kit Click here and download it now
April 28th, 2015 5:21pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics