Created
November 8, 2023 17:35
-
-
Save MyITGuy/369d48dc5984dbebf2a7095aa8bddb09 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#region Get-CMInstallPath | |
function Get-CMInstallPath { | |
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 | |
} | |
} | |
$ServicePathName = Get-CimInstance -ClassName Win32_Service -Namespace root/CIMV2 -Filter "Name='CcmExec'" | Select-Object -ExpandProperty PathName | |
$ServicePathName | Split-CommandLine | Split-Path -Parent | |
} | |
#endregion Get-CMInstallPath |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment