forked from beatcracker/Powershell-Misc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Split-CommandLine.ps1
108 lines (87 loc) · 3.31 KB
/
Split-CommandLine.ps1
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<#
.Synopsis
Parse command-line arguments using Win32 API CommandLineToArgvW function.
.Link
http://edgylogic.com/blog/powershell-and-external-commands-done-right/
.Description
This is the Cmdlet version of the code from the article http://edgylogic.com/blog/powershell-and-external-commands-done-right.
It can parse command-line arguments using Win32 API function CommandLineToArgvW .
.Parameter CommandLine
This parameter is optional.
A string representing the command-line to parse. If not specified, the command-line of the current PowerShell host is used.
.Example
Split-CommandLine
Description
-----------
Get the command-line of the current PowerShell host, parse it and return arguments.
.Example
Split-CommandLine -CommandLine '"c:\windows\notepad.exe" test.txt'
Description
-----------
Parse user-specified command-line and return arguments.
.Example
'"c:\windows\notepad.exe" test.txt', '%SystemRoot%\system32\svchost.exe -k LocalServiceNetworkRestricted' | Split-CommandLine
Description
-----------
Parse user-specified command-line from pipeline input and return arguments.
.Example
Get-WmiObject Win32_Process -Filter "Name='notepad.exe'" | Split-CommandLine
Description
-----------
Parse user-specified command-line from property name of the pipeline object and return arguments.
#>
function Split-CommandLine
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$false, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=0)]
[ValidateNotNullOrEmpty()]
[string]$CommandLine
)
Begin
{
$Kernel32Definition = @'
[DllImport("kernel32")]
public static extern IntPtr GetCommandLineW();
[DllImport("kernel32")]
public static extern IntPtr LocalFree(IntPtr hMem);
'@
$Kernel32 = Add-Type -MemberDefinition $Kernel32Definition -Name 'Kernel32' -Namespace 'Win32' -PassThru
$Shell32Definition = @'
[DllImport("shell32.dll", SetLastError = true)]
public static extern IntPtr CommandLineToArgvW(
[MarshalAs(UnmanagedType.LPWStr)] string lpCmdLine,
out int pNumArgs);
'@
$Shell32 = Add-Type -MemberDefinition $Shell32Definition -Name 'Shell32' -Namespace 'Win32' -PassThru
if(!$CommandLine)
{
$CommandLine = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($Kernel32::GetCommandLineW())
}
}
Process
{
$ParsedArgCount = 0
$ParsedArgsPtr = $Shell32::CommandLineToArgvW($CommandLine, [ref]$ParsedArgCount)
Try
{
$ParsedArgs = @();
0..$ParsedArgCount | ForEach-Object {
$ParsedArgs += [System.Runtime.InteropServices.Marshal]::PtrToStringUni(
[System.Runtime.InteropServices.Marshal]::ReadIntPtr($ParsedArgsPtr, $_ * [IntPtr]::Size)
)
}
}
Finally
{
$Kernel32::LocalFree($ParsedArgsPtr) | Out-Null
}
$ret = @()
# -lt to skip the last item, which is a NULL ptr
for ($i = 0; $i -lt $ParsedArgCount; $i += 1) {
$ret += $ParsedArgs[$i]
}
return $ret
}
}