Public/Process-SpecItem.ps1

function Process-SpecItem {
    <#
    .SYNOPSIS
    This function processes a specified item at the given path, creating the folder if necessary and optionally setting it as hidden.
 
    .DESCRIPTION
    The function checks if the specified path exists. If it doesn't exist and CreatePath is set to FALSE, the function skips the download.
    If the path doesn't exist but CreatePath is set to TRUE or the path exists, the function creates the folder and optionally sets it as hidden.
 
    This function was specifically written for the IRL device script using the Azure Table Storage data source for the parameter values.
 
    .PARAMETER path
    The path of the folder to be processed. This parameter is mandatory.
 
    .PARAMETER CreatePath
    A switch parameter that indicates whether to create the folder if it doesn't exist. Default value is empty (FALSE).
 
    .PARAMETER Hidden
    A switch parameter that, if specified, sets the folder as hidden. Default value is empty (FALSE).
 
    .EXAMPLE
    Process-SpecItem -path "C:\ExampleFolder" -CreatePath $true -Hidden $true
    Creates the folder "C:\ExampleFolder" if it doesn't exist and sets it as hidden.
 
    .OUTPUTS
    Boolean value indicating whether the folder was created successfully.
    $true = Folder creation and / or hidden attribute evaluation will occur
    $false = Folder creation and / or hidden attribute evaluation will not occur
 
    .NOTES
    Author : owen.heaume
    Version : 1.0
    #>


    param (
        [parameter (mandatory = $true)]
        [string]$path,

        [parameter (mandatory = $false)]
        [string]$CreatePath = "",

        [parameter (mandatory = $false)]
        $Hidden
    )

    if (!(Test-Path $path) -and $CreatePath -eq "FALSE") {
        Write-Host "[$path] does not exist, but not set to create so skipping download" -ForegroundColor DarkYellow
        return $false # no need to download
    } else {
        Write-Host "Creating folder [$path]" -ForegroundColor DarkCyan
        $result = New-SpecFolder -Path $path
        switch ($result) {
            0 { Write-Host "OK" -ForegroundColor DarkGreen; $OK = $true }
            1 { Write-Host "An error occurred trying to create the folder" -ForegroundColor DarkRed; $OK = $false }
            2 { Write-Host "The folder already exists" -ForegroundColor DarkGray; $OK = $true }
        }

        if ($OK) {
            # Set folder as hidden if required
            write-host "OK"
            if ($hidden -match "$true") {
                #use -match due to the way aztables sends down the data as a string
                Write-Host "Setting hidden attribute on folder" -ForegroundColor DarkCyan
                $result = Set-SpecHiddenAttribute -Path $path -Action Add
                switch ($result) {
                    0 { Write-Host "Attribute set OK" -ForegroundColor DarkGreen }
                    1 { Write-Host "Attribute was already set to hidden" -ForegroundColor DarkGray }
                }
            }
            return $true # success
        }
    }
}