Author Posts

April 5, 2016 at 5:27 am

I'm writing a function that looks at a status of a WMI property. The property has 3 values, Success, InProgress and Error. Its part of the SCCM namespace to check on distribution status.

I wanted to add the switch option to Continue check until completed or errored, the $persist switch.
The below works, but doesn't feel best practice. Any thoughts ?

Function Get-DPStatus {

Param
    (
[Parameter(Mandatory, 
    ValueFromPipeline=$true,  
    Position=1,
    ParameterSetName='Define Application')]
    [string[]]$Application, 
    
    [switch]$Persist      
    )

$WMI = @{
NameSpace = 'root\SMS\site_AAC'
ClassName = 'SMS_DPGroupDistributionStatusDetails'
Filter =  "ContentName like '$Application'"
}

$DPStatus = Get-CimInstance @WMI

Write-Output "`n`tChecking the distribution status of $application`n`n"

$Pause = { $T = New-TimeSpan -End (Get-Date).AddHours(4) ; sleep ($T.TotalSeconds) }

Foreach ($DPState in $DPStatus) {
      
   switch ($DPState.MessageState) 
         {
            1 {write-host -NoNewline "$($DPState.DPName): " ; sleep 2 ; write-host -ForegroundColor Green "[ Successful ]" }
            2 { 
                if ($Persist) {
                    do { write-host -ForegroundColor Yellow "Please Wait $($DPState.DPName) in progress..." ; & $Pause } until ($DPState.MessageState -eq 3 -or 2) }
                else { write-host -NoNewline "$($DPState.DPName): " ; sleep 2 ; write-host -ForegroundColor Yellow "[ In Progress ]" } 
              }
            3 {write-host -NoNewline "$($DPState.DPName): " ; sleep 2 ; write-host -ForegroundColor Red "[ Failed ]" }
         } 
    }#End of Foreach

}#End of Function

April 5, 2016 at 6:32 am

I've had a bit of a rewrite, does this look better ?

Function Get-DPStatus {

Param
    (
[Parameter(Mandatory, 
    ValueFromPipeline=$true,  
    Position=1,
    ParameterSetName='Define Application')]
    [string[]]$Application, 
    
    [switch]$Persist      
    )

$WMI = @{
NameSpace = 'root\SMS\site_AAC'
ClassName = 'SMS_DPGroupDistributionStatusDetails'
Filter =  "ContentName like '$Application'"
}

$DPStatus = Get-CimInstance @WMI

Write-Output "`n`tChecking the distribution status of $application`n`n"


Foreach ($DPState in $DPStatus) {
      
   switch ($DPState.MessageState) 
         {
                1 { write-host -NoNewline "$($DPState.DPName): " ; sleep 2 ; write-host -ForegroundColor Green "[ Successful ]" }
                2 { write-host -NoNewline "$($DPState.DPName): " ; sleep 2 ; write-host -ForegroundColor Yellow "[ In Progress ]" }
                3 { write-host -NoNewline "$($DPState.DPName): " ; sleep 2 ; write-host -ForegroundColor Red "[ Failed ]" }
         } 
    }#End of Foreach

if ($Persist) {

#Set Pause
$Pause = { $T = New-TimeSpan -End (Get-Date).AddHours(4) ; sleep ($T.TotalSeconds) }

    Foreach ($DPState in $DPStatus) {
        if ($DPState.MessageState -contains 2) {
            do { write-host -ForegroundColor Yellow "Please Wait $($DPState.DPName) in progress..." ; & $Pause } until ($DPState.MessageState -contains 1 -or 3)  
                If ($DPState.MessageState -eq 1) {
                    write-host -NoNewline "$($DPState.DPName): " ; sleep 2 ; write-host -ForegroundColor Green "[ Successful ]" }
                else { 
                    write-host -NoNewline "$($DPState.DPName): " ; sleep 2 ; write-host -ForegroundColor Red "[ Failed ]" } }          
            }                                
       
   }#End of $persist switch                                                   

}#End of Function

April 5, 2016 at 11:44 am

Option 1 looks fine to me. It's perfectly valid to use a nested if statement.