PowerShell

How to Run Jobs or Commands in Parallel using PowerShell

Categories:

Well I don't care much for PowerShell, but sometimes it is a necessary evil. I needed to run some PowerShell scripts in parallel, so I wrote a wrapper to do this. Maybe it will help you also.

Features:

  • You specify how many jobs to run in parallel
  • When one job finishes, another will be started

Note: the Start-Job commandlet takes a few seconds to do its thing.


The repo lives here: https://github.com/northben/PowerShell-Parallel-Job-Runner

Working with IIS logs

Categories:

If you live in the unfortunate world of IIS6, or perhaps you're just masochistic, you're going to use ADSI in PowerShell to work with IIS. A friend of mine showed me a handy way to delete log files older than a certain date using an ADSI query in PowerShell.

Converting FLAC files on Windows, or, "How I use ffmpeg to rule the world"

Categories:

I had some FLAC audio files on my computer that I wanted to convert to a lossy format (AAC, in my case). iTunes won't play FLAC, and VLC wasn't helping me. I ended up calling FFmpeg in a PowerShell script to quickly convert all of the files.

$target = 'J:\FLAC'
$target = $target + "\*.flac"
$files = Get-ChildItem $target

foreach ($file in $files) {
    .\ffmpeg.exe -i $file -acodec libvo_aacenc -b:a 256k -ar 44100 -threads 4 -n $($file.BaseName + ".m4a")
}

How to retrieve file modification time through WMI and PowerShell

Categories:

I needed to get a file modification timestamp through WMI (for an irrelevant reason, I couldn't use the UNC path and PowerShell's get-childitem cmdlet). So, WMI it had to be...

$file = gwmi -computername remoteComputer -query "select * from cim_datafile where name = 'C:\\file.txt' "
$age = (get-date) - $file.convertToDateTime($file.LastModified)
return $age.seconds

Pretty quick and painless!

Note: you could get (m)any other file attributes by slightly altering these lines. Remember: the get-member cmdlet is your friend!

PowerShell Parameters

Categories:

If you're writing a script that takes parameters with the "param" statement/feature, don't declare any variables before param. It will throw some weird errors! You can put variables in the Begin scriptblock instead.

How to copy group membership between Active Directory users

Categories:

Let's say you need to add a new user to all the Active Directory groups of an existing user. It would be painstaking and error-prone to compare group memberships in the AD Users & Computers snap-in. But don't despair: There is an easier way!

First, install the free ActiveRoles Management Shell module for PowerShell from Quest if you don't already have it. Trust me, you'll wonder how you managed Active Directory without it!

Second, run a command like this in PowerShell:

Remove empty lines from a file with PowerShell

Categories:

Here's a quick way to remove blank lines from a file using PowerShell. This will also remove lines that only have spaces.

(gc file.txt) | ? {$_.trim() -ne "" } | set-content file.txt

PowerShell Get-WmiObject with a variable in filter

Categories:

Let's say you want to use PowerShell's Get-WmiObject commandlet to retrieve a list of files older than a certain amount of time. Here's one way to do it.

PowerShell script to find status of service on a remote computer

Categories:

Here's a quick one-liner to retreive the status of a Windows service on a remote computer using Powershell. You don't need PowerShell remoting for this to work because it uses WMI.

This example uses the SNMP service, so just replace that text with the particular service you're looking for. Also replace remotecomputer with the name of the computer you are querying. If you remove '-computername remotecomputer' you will query your local computer.

Subscribe to RSS - PowerShell