-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateProcess.ps1
More file actions
40 lines (30 loc) · 1.26 KB
/
CreateProcess.ps1
File metadata and controls
40 lines (30 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
param
(
[string]$serverName,
[Parameter(Mandatory = $true)]
[string]$executionPath,
[string]$commandLine,
[string]$currentDirectory
)
# determine server name is local or remote machine
$server = (".", $serverName)[!$serverName -eq $null]
$connection = new-object System.Management.ConnectionOptions
$connection.EnablePrivileges = $true
$connection.Impersonation = "Impersonate"
$scope = new-object System.Management.ManagementScope("\\$server\root\cimv2", $connection)
$scope.Connect()
$managment = new-object System.Management.ManagementClass(
$scope,
(new-object System.Management.ManagementPath("Win32_Process")),
(new-object System.Management.ObjectGetOptions))
$params = $managment.GetMethodParameters("Create")
$params["CommandLine"] = "$executionPath $commandLine"
$params["CurrentDirectory"] = ((Split-Path -parent $executionPath), $currentDirectory)[!$currentDirectory -eq ""]
$result = $managment.InvokeMethod("Create", $params, $null)
$strResult = $result | Format-List * -Force | Out-String
if($result.ReturnValue -ne 0)
{
Write-Error ("Process didn't create. Return value is {0}.{1}{2}" -f $result.ReturnValue, [System.Environment]::NewLine, $strResult)
exit 1;
}
Write-Output ("Process was created:{0}" -f $strResult)