An Switch inside a Switch ?

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Richard Diphoorn Richard Diphoorn 5 months, 3 weeks ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #37247
    Profile photo of Graham Beer
    Graham Beer
    Participant

    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
    
    
    #37251
    Profile photo of Graham Beer
    Graham Beer
    Participant

    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
    
    #37253
    Profile photo of Richard Diphoorn
    Richard Diphoorn
    Participant

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

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.