2
\$\begingroup\$

I am still learning PowerShell and I would like your opinion on my folder deployment PowerShell function. It basically check if a defined folder exists and creates it if it does not and writes it actions to log and console.

<#

.SYNOPSIS

Creates folder if it does not already exists.

.DESCRIPTION

This function check if defined transfer folder exists and if not it creates it on remote computer.

.PARAMETER Path

Full path of the folder.

.PARAMETER Cancel

If Cancel parameter set to true the folder deployment is canceled. This is used in pipeline when it is important to skip this operation if last operation failed.

.EXAMPLE

Deploy-Folder -Path 'D:\Folder\Folder'

#>

function Deploy-Folder {
    param (
        [Parameter(Position = 0, Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
        [String]
        $Path,

        [Parameter(Position = 1, ValueFromPipeline, ValueFromPipelineByPropertyName)]
        [boolean]
        $Cancel = $false
    )

    begin {
        Import-Module '.\Write-Log.psm1'
    }

    process {   
        if(-not $Cancel) {
            if((Test-Path $Path) -eq $true) {
                $Message = "Successfully accessed " + $Path + " folder"
                $OperationResult  = 'Success'
            }
            else {
                try {
                    New-Item -Path $Path -ItemType "Directory"
                }
                catch {
                    $Message = "Failed to create " + $Path + " folder `n" + $_.Exception
                    $OperationResult  = 'Fail'
                }

                if((Test-Path $Path) -eq $true) {
                    $Message = "Successfully created " + $Path + " folder"
                    $OperationResult  = 'Success'
                }
            }
        }
        else {
            $Message = "Canceled " + $Path + " folder deployment"
            $OperationResult  = 'Success'
        }

        Write-Log -OperationResult $OperationResult -Message $message

        if($OperationResult -ne 'Fail') {
            $Cancel = $false
        }
        else {
            $Cancel = $true
        }
        
        New-Object -TypeName psobject -Property @{
            Cancel = $Cancel
        }
    }
}
\$\endgroup\$

1 Answer 1

4
\$\begingroup\$
  1. The code is very cleanly written, and its easy to see what is going on.
  2. The verb you are using for the command is not officially 'supported'. Every command should stick to the official list of verbs, that can be checked with get-verb. would suggest New-Folder
  3. I don't know why you go through this much code to create a folder when you could append a -force to the back end of the new-item command. I guess if you have a system that required you to log every instance where one would create a folder, this would make sense.
  4. If you have a system where you do commands to an external module, i would suggest you look into the #require tag in powershell, instead of having import-module at the start of your command
  5. you can easily cast to a psobject just by doing [PSCustomObject]@{cancel=$cancel}.
  6. use [CmdletBinding()] at the start of your function to get powershell standard switches (-verbose, -debug, -erroraction etc..). just put it between the function definition line and the param definition line (line 2)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.