pwshguy (mdowst)

Father, author, blogger, enthusiast of all things PowerShell and automation. http://linktr.ee/mdowst

  • 14 Posts
  • 2 Comments
Joined 1 year ago
cake
Cake day: June 8th, 2023

help-circle


  • Just looking at it from the point of view of making the script more portable and easier for someone else to run, there are a few things I would address.

    The first is the Write-Host commands all over the script. I would recommend converting those to Write-Verbose. Here is a great explanation when to use Write-Host vs other outputs.

    There are also numerous Write-Output commands in the script. Anything sent to the Write-Output will be returned to the calling console. If you need to take additional actions based on the results of this script, this could cause issues. You can run into problems with the New-Item commands in there too, as they will produce output. You might consider saving them to a variable or piping to Out-Null.

    Also, there is no need to call exit and set an exit code in the way you are. If you want to write and error but have the script continue you can use, Write-Error. If you want the processing to terminate then use throw. Doing it this way will allow PowerShell’s built-in error handling to take care of the exit codes. It will also give you greater flexibility with using Error Action Preferences and using try/catch statements.

    Finally, you have a path hardcoded for the workingDir. I would suggest making this a parameter or using an environment variable as this will make it more portable. Also, when creating the log variable, you will want to use the Join-Path cmdlet instead of just joining strings.