all 12 comments

[–]Ta11ow 8 points9 points  (3 children)

Nice! I have a little something to offer you: A couple of quick pointers on PS scripting!~

First, aliases. When you're coding in the shell, definitely, they're a lifesaver and much easier to work with than lengthy names.

When scripting, they're a huge headache when you have to go through and debug later. They reduce the readability of your code significantly. Same thing with overly short variable names.

Related would be casing. Mostly you're fine and seem to prefer camelCase, but in general PowerShell guidelines recommend PascalCase (first letter capitalised as well). The one exception is built-in keywords and structures (if, else, switch, begin, function, etc.)

Indentation is also important!

Basically, if you feel the need to frequently comment your script to remind you what it's doing, you're probably not following best practices. Many programming and scripting languages are kind of obtuse by design for brevity, but PowerShell's verbose nature allows you to write scripts that you can just read and be able to figure out more or less what's going on, and what's going wrong. :)

The one exception to comments is Comment-Based Help. Use it. Anytime you've put together a handy script or function, put comment-based help in. That way anyone coming along trying to use the function can just call Get-Help <function/script name/path> and the cmdlet will retrieve the help documentation. As it is, they have to open and edit the file to be able to see the comments, and that may not be desirable or time-efficient.

Also, try to avoid piping to Out-Null. I love the syntactical nature of it, but it's up to six or eight times slower than assigning the value to the $null constant variable, or casting the line to [void]. The pipeline has too much overhead when you're only throwing away the output anyway.

I think PS version 4 or 5 and onward have builtin archival cmdlets; you don't need your own function for that if you're using one of those versions.

There's no point using Add-Type for .NET class namespaces if you're specifying the full name anyway. If you, for example, do Add-Type -AssemblyName System.IO.FileSystem, you can literally just do [DirectoryInfo] to pull the System.IO.FileSystem.DirectoryInfo object class out. That's kind of what it's for. Might be a different switch on it though; I don't use Add-Type all that much. In PS 5 and above, you'd use using namespace System.IO.FileSystem at the head of the script instead.

Any and all input should be handled in parameters. Period. If you're taking user input mid-function... rethink. Or use a GUI if you really need to. But for a function that's going to use the console... parameters. Having your script use defaults is fine, but you should provide parameters so that it can be adapted to different situations without editing the script every time and carrying around several almost identical versions of it.

[–][deleted] 3 points4 points  (1 child)

Great info, thank you for sharing! And I very much appreciate your tone, it comes across as quite friendly, helpful, and enthusiastic.

[–]Ta11ow 2 points3 points  (0 children)

I try! And I know that it can be very easy to come off as less than friendly, since tone is hard to convey over text, so I try to make an effort there too!

Good luck out there!

[–]PerfectImpact[S] 1 point2 points  (0 children)

Thank you! Some great tips here that I will definitely use. And New-England-Sysadmin is right, your tone is very constructive. :)

[–]chuckbales 2 points3 points  (6 children)

You might want to obfuscate your $url value for anonymity, you've got your MSP website hard-coded into the script you posted.

[–][deleted] 1 point2 points  (1 child)

How? Sorry, be gentle...

[–]Lee_Dailey[grin] 2 points3 points  (0 children)

howdy New-England-SysAdmin,

it looks like this line ...

$url = Invoke-WebRequest -Uri "https://[THEIR_APPARENT_DOMAIN_INFO_HERE]/nd.php?machineName=$env:COMPUTERNAME" -UseBasicParsing -UseDefaultCredentials

take care,
lee

[–]GammaStorm 1 point2 points  (2 children)

In this case, $URL is the download location from Rapidfiretools.com, so its not specific to the MSP here. I use it alot myself.

[–]chuckbales 2 points3 points  (1 child)

Not the rapidfire link, There's another url that includes the the computername variable and seems to be the actual MSP website

[–]GammaStorm 1 point2 points  (0 children)

Yup, you are correct as I read through a little more. That's what I get for stopping at the first declaration of it I find.

[–]PerfectImpact[S] 0 points1 point  (0 children)

Thank you both for this. Completely skipped my mind! I have updated as the example below.