Creating a process

I’ve looked at creating a process before – using [WMIClass].
 
With CTP3 we have a few more options
 
Start-Process -Filepath notepad.exe
 
Invoke-WMIMethod -Class Win32_process -Name Create -ArgumentList notepad.exe
 
Set-WMIinstance -Class Win32_process -Arguments @{Path=c:windowssystem32notepad.exe}
 
The first two just happen.  The third option will throw an exception because it can’t find the instance and then create one.
 
Three options – which to use.  In this case probably start-process but the invoke-wmimethod is useful.
About these ads
This entry was posted in PowerShell and WMI. Bookmark the permalink.

2 Responses to Creating a process

  1. Martin says:

    4th option (I prefer) is to do it in .NET way:Function Global:S4M\Start-Process ($FileName = "", $Arguments = "", [switch]$StringOutput) { $exitCode = 1 # assume failure as long as the reverse comes true if ($FileName.Length) { $process = New-Object system.Diagnostics.Process $si = New-Object System.Diagnostics.ProcessStartInfo $si.FileName = $FileName if ($Arguments.Length) {$si.Arguments = $Arguments} $si.UseShellExecute = $false $si.RedirectStandardOutput = $true $process.StartInfo = $si $process.Start() | Out-Null $ProcessOutput = $process.standardoutput.ReadToEnd() $process.WaitForExit() $exitCode = $process.Get_ExitCode() } If ($StringOutput) { Return $ProcessOutput } Else { return $exitCode }}

  2. Richard says:

    It works but its a lot more code. I think many PowerShell users are admins and they will go for the cmdlet approach because to their eyes it is simpler.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s