PowerShell for Windows Admins


January 20, 2014  1:00 PM

Win32_Process examples–test command line

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

To see the command lines that have been used when processes are started is simple one liner:

Get-CimInstance -ClassName Win32_Process | select Name, CommandLine

If you want to investigate specific processes use the –Filter parameter to restrict the processes

January 20, 2014  12:54 PM

Win32_Examples–start application in hidden window

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

This one is interesting as I’d tried doing this a while back and failed.  Starting a process with Win32_Process is straightforward but controlling the process – such as starting in a hidden window wasn’t working. This is how you do it:

function start-hiddenproc {

[CmdletBinding()]

param ( [string]$processname = ‘notepad.exe’ )

$startclass = Get-CimClass -ClassName Win32_ProcessStartup

$startinfo = New-CimInstance -CimClass $startclass -Property @{ShowWindow = 0} -ClientOnly

$class = Get-CimClass -ClassName Win32_Process

Invoke-CimMethod -CimClass $class -MethodName Create -Arguments @{Commandline = $processname; ProcessStartupInformation = [CimInstance]$startinfo}

}

The function takes a path to the process executable as a parameter – would be a good place for a validation script parameter to test the path to the executable.

Get the Win32_ProcessStartup class and use it with New-CimInstance to create the start up information. The New-CimInstance parameter –ClientOnly can be aliased to –Local.  I always prefer to use the master parameter name rather than aliases – makes it easier for people to look things up in the documentation.

Get the Win32_Process class and use it with Invoke-CimMethod to invoke the Create method with the arguments passed as shown


January 18, 2014  6:24 AM

Winter Scripting Games 2014–event 1

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

The event instructions for event 1 are available for download.

Entries will be accepted starting tomorrow.

Event will close 26 January 00:00:00 UTC


January 17, 2014  1:25 PM

Win32_Process examples–running scripts

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Back in the day when all we had was VBScript you could run scripts through the command line (cscript) or you would get a more graphical interface (wscript).  One of the examples at http://msdn.microsoft.com/en-us/library/aa394599(v=vs.85).aspx shows how to detect running scripts.

I don’t imagine much call for that technique but if you need it – here it is:

Get-CimInstance -ClassName Win32_Process -Filter “Name = ‘cscript.exe’ OR Name = ‘wscript.exe’” | Format-Table Name, Commandline

You could use a variation to test the command line input to other processes if you need to


January 17, 2014  11:45 AM

Winter Scripting Games 2014–tips

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Event 1 is available for download in just over 6 hours

In the mean time head over to powershell.org and look at the fantastic set of tips that the coaches are putting out


January 17, 2014  8:38 AM

Kindle app for Windows 8

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I’ve written unfavourably on the Kindle app in the past but I stumbled on an piece of functionality in the app that makes me take a lot of my comments back – search.

If you are in the Kindle app and bring up the charms you can get into search. This means you can search for books within your cloud store – this is especially useful fro me when I have hundreds of books most of which aren’t downloaded onto my Windows device at any one time.


January 17, 2014  3:36 AM

Win32_Process examples

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

In case you were wondering where the examples came that inspired the code in this series from its here – http://msdn.microsoft.com/en-us/library/aa394599(v=vs.85).aspx

I’m providing PowerShell examples

In some cases it would be easier to use the *Process cmdlets but I want to demonstrate how to use the WMI class


January 16, 2014  1:58 PM

Win32_Process examples–terminate process

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Terminating a running process is simply a case of calling the Terminate method:

function remove-proc{

[CmdletBinding()]

param (

[string]$computername = $env:COMPUTERNAME,

[string]$processname

)

Get-CimInstance -ClassName Win32_Process -ComputerName $computername -Filter “Name = ‘$processname’” |

Invoke-CimMethod -MethodName Terminate

}

This will terminate all instances of a given process.  If you want to be more selective then change the function to use the process id rather than the process name


January 16, 2014  1:41 PM

Win32_Process examples–set priority

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Changing the priority of a process can give a processing boost to an application – but its not always a safe option.

You can modify the process like this:

function set-procpriority {
[CmdletBinding()]
param (
[string]$computername = $env:COMPUTERNAME,
[string]$processname,

[ValidateSet("Idle", "BelowNormal", "Normal", "AboveNormal", "HighPriority", "RealTime")]
[string]$priority
)

switch ($priority){
“Idle”         {[uint32]$priorityin =    64; break}
“BelowNormal”  {[uint32]$priorityin = 16384; break}
“Normal”       {[uint32]$priorityin =    32; break}
“AboveNormal”  {[uint32]$priorityin = 32768; break}
“HighPriority” {[uint32]$priorityin =   128; break}
“RealTime”     {[uint32]$priorityin =   256; break}
}

Get-CimInstance -ClassName Win32_Process -ComputerName $computername -Filter “Name = ‘$processname’” |
Invoke-CimMethod -MethodName SetPriority -Arguments @{Priority = $priorityin}
}

The advanced takes three parameters – computername (defaults to local machine), a process name and the priority that process should have.

A switch statement converts the priority name to an unsigned integer value

The process objects are retrieved by Get-CimInstance and Invoke-CimMethod is used to call the SetPriority method.

You can use it like this:

£> notepad

£> get-process notepad | Format-List Name, PriorityClass

Name          : notepad
PriorityClass : Normal

£> set-procpriority -processname ‘notepad.exe’ -priority HighPriority

ReturnValue PSComputerName
———– ————–
0 RSSURFACEPRO2

£> get-process notepad | Format-List Name, PriorityClass

Name          : notepad
PriorityClass : High

The function will modify the priority of all instances of the process. If you want to modify just one instance then you need to remove the process name parameter and add a process id parameter


January 15, 2014  4:31 PM

Winter Scripting Games 2014 –Event 1

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

The first event in the 2014 Winter Scripting Games kicks off in just over 2 days time.

The practice event is finished and we’re testing the judging system

Good luck to all participants


Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: