Friday, November 9, 2012

PowerShell Tricks

Set Specific time for today's date:
 
In PowerShell the Hour variable within the DateTime class is readonly. So below is a way to create a new datetime that says something like today at 5:30 pm.

[datetime]::ParseExact("17:30","HH:mm",$null)

Create a pause in the script:

Start-Sleep -s 10
#This pauses for 10 seconds

Start-Sleep -m 1000
#This pauses for 1000 milliseconds


Find an instance of program based on its command line argument:

A problem I had recently was how to identify a specific instance of Erlang running. The reason for this was that in all instances the name of the process was erl.exe. The only difference were the starting arguments provided.

The solution is as follows:


$erls = (Get-WmiObject win32_process -Filter "name like '%erl.exe' and CommandLine like '%argument%'" -ErrorAction SilentlyContinue)

This uses WMI to get all instances of processes ending in erl.exe, and whose commandLine includes the word argument.

No comments: