Home > Blockchain >  How do I make a conditional statement for Switch statement
How do I make a conditional statement for Switch statement

Time:09-15

I need to make a conditional statement so that:

'.txt' { Invoke-CommandAndLog @((Get-ResourcePath "UtilityModules\LGPO.exe"), - " /un", $policy.FullName, " /q")}

It is based on both .txt and the filename of "xyz.txt". I am struggling to get this to work, though. Code below without additional condition of filename = xyz.txt

function Import-GpoSettings{
<#
.SYNOPSIS
    Imports group policy files. 
.DESCRIPTION
    Applies DISA/ESS specific group policies.
.PARAMETER
    Resource path to find pol files.
.PARAMETER
    MatchString finds pol files that includes this string in the file name.
#>
    Param (
        [string] $Resource,
        [string] $MatchString
    )
    
    $policies = Get-ChildItem (Get-ResourcePath $Resource) | Where-Object Extension -in '.txt', '.inf', '.csv' | Where-Object Name -match $matchString

    ForEach ($policy in $policies) {
        [string] $currentPolicy = $policy.BaseName
        Write-HostAndLog "`t Applying standard $currentPolicy policy settings.`n"
        switch ($policy.Extension) {
            '.txt' { Invoke-CommandAndLog @((Get-ResourcePath "UtilityModules\LGPO.exe"), - " /un", $policy.FullName, " /q")}
            '.txt' { Invoke-CommandAndLog @((Get-ResourcePath "UtilityModules\LGPO.exe"), " /t", $policy.FullName, " /q")}
            '.inf' { Invoke-CommandAndLog @((Get-ResourcePath "UtilityModules\LGPO.exe"), " /s", $policy.FullName, " /q")}
            '.csv' { Invoke-CommandAndLog @((Get-resourcepath "UtilityModules\LGPO.exe"), " /a", $policy.FullName, " /q")}
            Default { }
        }

    }

}#endFunction

CodePudding user response:

If you want your switch statement condition to match two or more conditions, you can use the syntax described in the about_Switch

It's also worth noting that the switch can do the looping for you, so technically you don't need foreach in this case. You can also add the condition for matching extensions and testing if the file name matches $matchString in the switch itself, so this can also be removed:

Where-Object Extension -in '.txt', '.inf', '.csv' | Where-Object Name -match $matchString

Lastly, you will notice the use of continue on each of the action scriptblocks, this is because a switch evaluates all conditions before going to the next element unless we add control flows.

switch(Get-ChildItem (Get-ResourcePath $Resource)) {
    # if the filename doesn't match, just got to the next item,
    # below conditions will not be evaluated
    { $_.Name -notmatch $matchString } { continue }

    # this conditions is always exectued
    { $true } {
        Write-HostAndLog ("`t Applying standard {0} policy settings.`n" -f $_.BaseName)
    }

    # this is the condition that evaluates the question being asked,
    # extension is `.txt` and file name is `xyz.txt`
    { $_.Extension -eq '.txt' -and $_.Name -eq 'xyz.txt' } {
        # => code here for this condition!
        continue
    }

    {$_.Extension -eq '.txt' } {
        Invoke-CommandAndLog @((Get-ResourcePath "UtilityModules\LGPO.exe"), - " /un", $_.FullName, " /q")
        continue
    }

    { $_.Extension -eq '.inf' } {
        Invoke-CommandAndLog @((Get-ResourcePath "UtilityModules\LGPO.exe"), " /s", $_.FullName, " /q")
        continue
    }
    
    { $_.Extension -eq '.csv' } {
        Invoke-CommandAndLog @((Get-resourcepath "UtilityModules\LGPO.exe"), " /a", $_.FullName, " /q")
    }
}
  • Related