header image

Archive for January, 2014

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 […]

under: PowerShell and CIM, PowerShell and WMI, PowerShell V3, PowerShell v4

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"  […]

under: PowerShell and CIM, PowerShell and WMI, PowerShell V3, PowerShell v4

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

under: PowerShell original, Scripting Games 2104

Moving on with examples of using Win32_Process you can find the process owner: function get-procowner { [CmdletBinding()] param ( [string]$computername = $env:COMPUTERNAME ) Get-CimInstance -ClassName Win32_Process -ComputerName $computername | foreach {   $owner = Invoke-CimMethod -InputObject $psitem -MethodName GetOwner   $props = [ordered]@{              Name = $psitem.Name              Domain = $owner.Domain              User = $owner.User […]

under: PowerShell and CIM, PowerShell and WMI, PowerShell V3, PowerShell v4

I saw some example code for using Win32_Process and didn’t like it so decided to create my own versions.  In this case the objective is to display the processor time and memory usage: function get-proctimeandRAM { [CmdletBinding()] param ( [string]$computername = $env:COMPUTERNAME ) Get-CimInstance -ClassName Win32_Process -ComputerName $computername | foreach {   $props = [ordered]@{ […]

under: PowerShell and CIM, PowerShell and WMI, PowerShell V3, PowerShell v4

VM disk info

Posted by: | January 14, 2014 | No Comment |

A question came into the forum about getting information on the virtual disks associated with particular Hyper-V virtual machines. Is a bit of a digging exercise but this gets the results: Get-VM | foreach { $VMname = $psitem.Name Get-VMHardDiskDrive -VMName $VMname  | foreach {    Get-VHD -Path $_.Path |    select @{N=’VMname’; e={$VMName}}, Path, Size, […]

under: Hyper-V, PowerShell original, Windows 2012 R2, Windows Server 2012

Finding a sequence

Posted by: | January 10, 2014 | No Comment |

I saw a challenge to find the occurrences of the sequence 1759 in the larger sequence 11759171759.  It was originally presented as a SQL Server based challenge to be solved with TSQL but we can do this just as easily with PowerShell using Select-String £> $data = ‘11759171759’ £> Select-String -InputObject $data -Pattern ‘1759’ -AllMatches […]

under: PowerShell Basics

Cim cmdlet aliases

Posted by: | January 9, 2014 | No Comment |

I don’t use aliases in scripts/functions and don’t use them much at the command line. One alias I do use reasonably often is gwmi for Get-WmiObject – mainly because I do a lot with WMI.  I’m using the CIM cmdlets much more these days and suddenly realised that I didn’t know if they had aliases. […]

under: PowerShell and CIM, PowerShell v4

Test windows activation

Posted by: | January 8, 2014 | No Comment |

I’m currently upgrading my lab environment to Windows Server 2012 R2 which involves upgrading some machines and rebuilding the others.  One task in any build or upgrade situation is to make sure that Windows is activated. Windows Server 2012 R2 will activate once an Internet connection is established. This can sometimes take a few minutes. […]

under: PowerShell and CIM, PowerShell and WMI, PowerShell v4, Windows 2012 R2

CDXML: Cim jobs

Posted by: | January 5, 2014 | No Comment |

One of the freebies you get when using CDXML is that the cmdlets you create automatically get the –AsJob parameter. I was thinking about jobs in general and realised that I didn’t know how CIM jobs were run. To put this into context: PowerShell jobs run in another PowerShell process that is started as a […]

under: CDXML, PowerShell and CIM, PowerShell V3, PowerShell v4

« Newer Posts