Quantcast
Channel: Uncategorized – GotSpeechGuy
Viewing all articles
Browse latest Browse all 9

How Every PowerShell Script Should Start Out

$
0
0

I recently downloaded a rather large PowerShell script from the Internet and  tried to run it. I’m sure the author was quite proud of his script and his programming abilities but the script failed with multiple errors when I tried to run it. The author made a fatal assumption that just because it ran in his environment that it would work for me and everyone else. He probably thought that anyone who tried to run it would first open the Lync Server Management Shell but my preferred environment is the PowerShell ISE and that is what I was using to run the script.

The problem arose when the script tried to use one of the Lync cmdlets but the Lync module was not loaded. A PowerShell Script should always include these lines (and any other needed modules) as the first lines in the script. That way the script won’t be referencing modules that haven’t ben loaded into the environment.

if ((Get-Module ActiveDirectory) -eq $null){Import-Module ActiveDirectory}
if ((Get-Module Lync) -eq $null){Import-Module Lync}

it’s so easy to avoid the execution problems that I had by simply testing to see if  a required modules is loaded and if it isn’t then load it. Its what sets a good developer apart from a mediocre developer.



Viewing all articles
Browse latest Browse all 9

Latest Images

Trending Articles





Latest Images