Monday, April 21, 2025
HomePowershellPowerShell is enjoyable :)Whenever you check out your previous PowerShell scripts…

PowerShell is enjoyable :)Whenever you check out your previous PowerShell scripts…


Typically, I browse my previous scripts in my non-public GitHub repository and suppose… Why did I write it / The place’s the error dealing with / I may enhance this with what I realized after writing that years in the past, and so on. On this weblog put up, I’ll present you just a few examples of simply that and the way I improved 🙂

How I began writing my first few scripts

I feel everybody began with scripts that had variables hard-coded on the high of their scripts and used that contained in the script. The script was meant to get a specific factor completed as soon as… For instance: (Modified Domains to generic ones as a result of it’s a earlier buyer)

$Group='Area Admins"
$Recursive=$true
$Domains="Buyer.native",'subdomain.buyer.native'
$LogFile="C:tempDomainAdminscan.log"

Additionally, I used many Learn-Host prompts to create a script that asks you for a particular server, username, course of, and so on. Retailer the reply in a variable and use it within the script. For instance, under, I first immediate for the primary letters of the desktop or laptop computer and use the Quest cmdlet to search out the AD Laptop Objects to test if the Avast Anti-Virus software program is put in by checking its service:

$standards=Learn-Host 'Please enter the search standards (pct*, or lat*)'
$computer systems=Get-QADComputer -SamAccountName "*$standards*"
foreach ($i in $computer systems) {
$service = Get-WmiObject Win32_Service -Filter "Title="avast! Antivirus"" -ComputerName $i.Title
if ( ! $service ) {Write-Host Avast! Antivirus will not be put in on $i.identify} 
}

And sure, clear-text passwords had been additionally a factor in scripts 🙁 For instance, the half under was to assemble snapshot info from VMware VCenter (Modified server and usernames and the password, too)

Join-VIServer -Server srv325 -Consumer 'domaintask' -Password Passw0rd!
$snapshotsNL = Get-Snapshot -VM * -Server srv325
Join-VIServer -Server srv525 -Consumer 'domaintask' -Password Passw0rd!
$snapshotsUSA = Get-Snapshot -VM * -Server srv525

But in addition, as a result of there have been no cmdlets/modules again then, I referred to as executables in my scripts to execute issues and scraped output from that in my script. For instance, this was part of the script that logged off accounts from servers within the Area (Not Terminal Servers, however member servers that had Admins logged in)

question person /server:$pc | ?{$_ -match "b$Userb"} | ?{$_ -match "ICA-tcp#d*"} | %{logoff $matches[0] /server:$pc}

or this snippet to question Scheduled Duties:

schtasks.exe /Question /FO CSV /s $server.Title /v > $env:TEMPschtemp.csv

Issues that I improved on

I attempt to create scripts now which are extra versatile, supply error dealing with (and sure, typically I add extra error dealing with than the precise script :P), are simple to learn (Formatting in VSCode FTW), work in each scenario (not solely on my pc with all modules already put in), and so on.

I wish to share my scripts with the group by posting them on my GitHub web page now, however prior to now, I simply stored them for myself in order that no person may touch upon my scripting expertise. It’s best to by no means try this; you may be taught from others, which implies sharing your scripts and being prepared for suggestions. And sure, that’s scary at first. However don’t fear, and the PowerShell group will not be just like the ‘suggestions’ you get from Stack Overflow 😀

Up to now, I believed PowerShell was for small admin duties, however now I see an increasing number of potentialities for utilizing it for nearly something.

Private finest practices

When you check out my scripts from the final two years on my GitHub web page or within the blogs on this web site, you see a sample in them relating to:

Features

As a substitute of operating issues as a script, I like creating Features that I can add to my PowerShell profile after which make out there in my periods instantly. You should perceive that in the event you run a Operate, nothing occurs till you run the Get-XYZ, Invoke-123, or comparable Operate identify. (I had quite a lot of questions on my weblog posts that the script was executed, however nothing occurred…)

GridView

As a substitute of filtering information from a file output or display screen, I wish to output information to Out-GridView/Out-ConsoleGridView for straightforward filtering and choosing a number of objects in an OutputMode variable.

Output to CSV/XLSX

I began exporting information out of your script to CSV. Nonetheless, I exploit Doug Finke’s Import-Excel module an increasing number of to export to Exel instantly, making it simpler for the individuals who obtain the studies (And never making them import/convert them themselves).

Parameters

I like utilizing parameters to make my scripts extra versatile and simple to run. I have a tendency to make use of them with a default worth as a way to run the script with that or change it from the command line with a extra particular worth if wanted. ParameterSets, ValidateSets, [String[]] as a way to specify a number of values for a Parameter, Obligatory $true or $false, and so on.

PSCustomObject

That’s my go-to for storing outcomes, values, and so on., in scripts. I like constructing them as a pleasant database that I can use within the script for reporting output, and so on.

Strive/Catch

As a substitute of getting crimson error strains in your display screen, I exploit Strive/Catch to Strive an motion, and if it fails, Catch it in an error message on the display screen and cease processing the script. It prevents error output in your display screen and offers you the prospect to supply user-friendly error messages 🙂

Issues I wish to enhance on

With PowerShell, you’re by no means completed studying 🙂 I wish to be taught and enhance on:

  • Creating PowerShell Modules and publishing them on the PSGallery
  • Constructing GUI-based scripts
  • Utilizing Pester
  • DevOps CI/CD stuff
  • Creating extra important scripts and utilizing extra Features inside them

Wrapping up

And that’s my story about how I began writing my first scripts, how I improved, and that there’s nonetheless a lot to be taught. What did you enhance on in comparison with your first scripts? Depart a remark under!

Have a stunning weekend!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments