Wednesday, 15 August 2018

powershell command line options starter kit

I don’t know how I’ve been able to do this until now, but I’ve avoided powershell until last week.

I might quickly become addicted to it though, after my brief introduction.

I have a requirement to upload files into sharepoint from a UBE, simple enough.

I got some amazing fusion5 people to cut me a script, and it’s great for 90% of scenarios, but I need to do a couple of mods for 100% of scenarios.

My modifications revolve around the ability to process parameters, $? $# etc – I miss ksh.

So, here is some tips for me in 3 months time when I need to do this again.

First and foremost, your Param function needs to be 1st line of the script!


Param (
     [string]$filetoupload,
     [string]$username = $( Read-Host "Input username, please" ),
     [string]$password = $( Read-Host "Input password, please" ),
     [switch]$force = $false,
     [switch]$run = $false
)

This terseness is AWESOME!

When calling this function with the following, it assigns all of the variables

PS C:\fusion5> .\uploadContentsToSP_BM_Test_singlefile.ps1 -filetoupload c:\shannon.pd -username shannon.moir@fusion5.com.au -password hello

So, if I did a:

write-host $username

in the script, it’d tell me the username.  The other nice thing is that if you do not specify the parameter and there is a $read-Host directive, it’s going to prompt only for the items that have not been entered.  So cool!

Also, you’ll see that I’ve used a mix of [switch] and [string] parameters.  string of course is a variable, but switch is cool – it’s binary

if your code, you can then use:


if ($force) {
write-host "file to upoload: " $filetoupload
}

So therefore, if the script is called with –force then it’ll execute the write-host function – easy


Param (

    [Parameter(Mandatory=$true)]
    [string]$filetoupload,
     [string]$username = $( Read-Host "Input username, please" ),
     [string]$password = $( Read-Host "Input password, please" ),
     [switch]$force = $false,
     [switch]$run = $false
)

Calling this:

PS C:\fusion5> .\uploadContentsToSP_BM_Test_singlefile.ps1 -filetouplad c:\shannon.pd -username shannon.moir@fusion5.com.au -password hello
C:\fusion5\uploadContentsToSP_BM_Test_singleFile.ps1 : A parameter cannot be found that matches parameter name
'filetouplad'.
At line:1 char:45
+ .\uploadContentsToSP_BM_Test_singlefile.ps1 -filetouplad c:\shannon.p ...
+                                             ~~~~~~~~~~~~
     + CategoryInfo          : InvalidArgument: (:) [uploadContentsT..._singleFile.ps1], ParameterBindingException
     + FullyQualifiedErrorId : NamedParameterNotFound,uploadContentsToSP_BM_Test_singleFile.ps1

So if you do not specify the mandatory parameter (see that I have a spelling mistake) we get the error that the parameter has not been specified

It could be more graceful if you manage it (less big red writing)

The directive only affects the following parameter, not all of them.

No comments:

Extending JDE to generative AI