-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathGet-ExecutableForFile.ps1
91 lines (80 loc) · 2.94 KB
/
Get-ExecutableForFile.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
function Get-ExecutableForFile {
<#
.SYNOPSIS
Given a file, determine what program is associated with file.
.DESCRIPTION
Given a file, determine what program is associated with file.
.PARAMETER Path
The path to a file
.PARAMETER IncludeInput
Include the specified file as part of the output
.EXAMPLE
Get-ExecutableForFile -Path .\sample.docx
C:\Program Files (x86)\Microsoft Office\Root\Office16\WINWORD.EXE
.NOTES
Inspired by http://community.idera.com/powershell/powertips/b/tips/posts/finding-executable-for-file
Added logic to resolve relative path to file
Added logic to test if item is a file and not a folder
Added -IncludeInput as an option.
#>
[CmdletBinding(ConfirmImpact = 'None')]
param
(
[Parameter(Mandatory, HelpMessage = 'Please enter a filename', Position = 0)]
[string] $Path,
[switch] $IncludeInput
)
begin {
Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]"
}
process {
$Source = @'
using System;
using System.Text;
using System.Runtime.InteropServices;
public class PFWin32API
{
[DllImport("shell32.dll", EntryPoint="FindExecutable")]
public static extern long FindExecutableA(string lpFile, string lpDirectory, StringBuilder lpResult);
public static string FindExecutable(string pv_strFilename)
{
StringBuilder objResultBuffer = new StringBuilder(1024);
long lngResult = 0;
lngResult = FindExecutableA(pv_strFilename, string.Empty, objResultBuffer);
if(lngResult >= 32)
{
return objResultBuffer.ToString();
}
return string.Format("Error: ({0})", lngResult);
}
}
'@
if (-not (Test-Path -Path $Path)) {
Write-Error -Message "File specified [$Path] does not exist."
} else {
[array] $ResolveFile = Get-Item -Path $Path
if ($ResolveFile.count -gt 1) {
Write-Error -Message "ERROR: File specification [$Path] resolves to more than 1 file."
} else {
#$ResolveFile = $ResolveFile[0]
if ($ResolveFile[0] -is [System.IO.FileInfo]) {
Add-Type -TypeDefinition $Source -ErrorAction SilentlyContinue
$Result = [PFWin32API]::FindExecutable($ResolveFile)
if ($IncludeInput) {
New-Object -TypeName psobject -Property ([ordered] @{
Path = $ResolveFile[0].FullName
Executable = $Result
})
} else {
$Result
}
} else {
Write-Error -Message "ERROR: File specification [$Path] is a folder and not a file."
}
}
}
}
end {
Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]"
}
}