Skip to content

PowershellGym Posts

First post – First exercise – Working with Powershell Jobs

Alright, full disclosure time. I launched PowerShell Gym around six months back with every intention to hit you with a weekly post. Fast forward to now, and well, let’s just say the initial post was more of a one-hit wonder. Why the radio silence? I could spin a few excuses, but honestly, there aren’t any good ones. Yet here we are, on this fine Friday night of August 30th, and something just clicked. Why not breathe some life back into this blog? So, that’s exactly what we’re doing.

Welcome (back) to the first post and the first exercise. Even though I haven’t been sharing, believe me, I’ve been knee-deep in PowerShell scripts and projects all this while. Time to get those experiences out of my system and onto this blog. Let’s get this PowerShell party restarted!

Powershell Jobs

Okay, first, let’s dive into what PowerShell jobs are, and then we’ll create an exercise for ourselves. As you might know, PowerShell typically works by doing one job after the other (also called: synchronously). But what if we want something different, something asynchronous? And yeah, I might have mentioned this before, but I want this blog to be as practical as possible, so why the hell would you want something to run asynchronously?

Running tasks asynchronously in PowerShell can improve the efficiency and responsiveness of your scripts. This approach is especially useful when handling time-consuming operations that don’t need to be completed one after the other (=sequentially). For example, if you’re monitoring remote servers using a Telegraf or Nagios agent (for monitoring purposes), and you want to make sure the service is running, you can initiate all requests at once. This reduces the total time spent waiting for tasks to complete one after another, thereby speeding up the process and saving more time for other things!

Wait, why doing this?

Your text captures the essence of why asynchronous tasks can be more efficient compared to using a foreach loop for certain operations. Here’s a slightly refined version of your text to enhance clarity and flow:

You might be thinking, “Why not just use a foreach loop?” Here’s the thing: a foreach loop checks each server one at a time (remember, that’s synchronously). So if one server takes forever to respond, you’re stuck waiting on that one before moving to the next. It’s like standing in a slow-moving line.

Okay, lets get scripting!

we’re about to write a PowerShell script to check if the Telegraf agent is running on multiple remote servers, and we’ll do this asynchronously.

# all your precious servers
$serverNames = @("Server01", "Server02", "Server03")

# the service we want to check
$serviceName = "telegraf"

foreach ($Server in $serverNames) {
    Start-Job -ScriptBlock {
        param($Server, $serviceName)

        # connection to your server (assuming you got the right permissions)
        $session = New-PSSession -ComputerName $Server

        # starting the service on the remote server
        start-Service -Name $serviceName -Session $session

        # Closing the session
        Remove-PSSession -Session $session

    } -ArgumentList $Server, $serviceName
}

# check the status of all jobs
Get-Job | Receive-Job

The Get-Job | Receive-Job line in the script plays a crucial role at the end. After starting asynchronous jobs that check and start the Telegraf service on our multiple precious servers, this command fetches the results of those jobs. Get-Job lists all the background jobs running or completed, and Receive-Job retrieves the output from these jobs. This way, you can see the status of the service on each server, ensuring that each action has been executed and completed successfully. This helps in monitoring and verifying that the Telegraf service is up and running across your server network.

Welcome to PowershellGym.com – a weekly Powershell workout

Who am I?

Hello, all Powershell enthusiasts and curious minds! My name is Nick, and I’m excited to welcome you to PowershellGym.com. Because of my passion for automation and my passion for scripting, I spent a few years navigating the mighty oceans of Powershell. During the day, I am an (Windows) engineer at a big Dutch banking institute, where I use the power of Powershell to optimize programs, automate tasks, and solve (complex) problems. At night I turn into a Powershell explorer, always looking for new challenges and limitations to overcome.

Why I’m Doing This

I’ll be honest: I’m not a Powershell expert. I started PowershellGym.com because I wanted to improve my Powershell skills by practicing every week. My job offers opportunities to use automation, but I often find it challenging to figure out how to advance and enhance my abilities. This blog is my strategy for pushing myself to come up with new exercises each week.

But this endeavor isn’t just about me. It’s also for anyone who wants to learn and share valuable Powershell exercises, or perhaps find some inspiration for exercises here. Maybe you’ve felt a surge of happiness when your script executed flawlessly (like I get), or perhaps you’ve encountered obstacles that seemed overwhelming. Let’s exchange our experiences, learn from one another, and collectively improve.

My goal is to help others going through similar challenges or looking for community insights to share or find inspiration for their own Powershell exercises.

So, if you’re looking to learn new things, find ideas for your projects, or just see what you can do with Powershell, you’re in the right place. Let’s learn and grow together, one simple script at a time.

What Kind of Projects

Powershell is great because it can do so many different things. It’s very powerful, making the possibilities for projects virtually endless. Here are some examples of the exercises I might work on. But every week, I’ll come up with a new idea that is practical and has a clear goal. This way, it’s easier for me to stick to it.

(I might have generated these with ChatGPT, which will be a very helpful tool for my weekly inspiration!)

  1. Automated System Health Checks: Start your day with an automated report on system health, including CPU usage and disk space, directly in your inbox.
  2. Dynamic User Account Management: Use Powershell to streamline the creation, modification, and deletion of user accounts, ensuring security compliance.
  3. Network Monitoring: Implement a Powershell script for monitoring network performance, receiving alerts for potential issues, and auto-troubleshooting.
  4. Powershell-Powered Data Analysis: Analyze and visualize data with Powershell, transforming raw information into actionable insights.
  5. Automating Cloud Services: Learn to automate cloud operations, from VM deployment to cloud storage management, using Powershell scripts.

Thank you for joining me at the starting of this new Powershell journey. I’m excited to see where this path takes us and the amazing automation tasks we’ll tackle together. Stay tuned for the first weekly exercise, and let’s dive into Powershell scripting!

Nick.