-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScript-Information.ps1
81 lines (77 loc) · 2.98 KB
/
Script-Information.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
function Get-ScriptInformartion {
[CmdletBinding()]
Param()
switch ( $ExecutionContext.Host.Name ) {
"ConsoleHost" {
Write-Verbose "Runbook is executed from PowerShell Console"
if ( [boolean]$MyInvocation.ScriptName ) {
if ( ( $MyInvocation.ScriptName ).EndsWith( ".psm1" ) ) {
$MyScriptPath = $Script:MyInvocation.ScriptName
}
else {
$MyScriptPath = $MyInvocation.ScriptName
}
}
elseif ( [boolean]$MyInvocation.MyCommand ) {
if ( ( $MyInvocation.MyCommand.Source ).EndsWith( ".psm1" ) ) {
$MyScriptPath = [System.IO.FileInfo]$Script:MyInvocation.MyCommand.Source
}
else {
$MyScriptPath = [System.IO.FileInfo]$MyInvocation.MyCommand.Source
}
}
}
"Visual Studio Code Host" {
Write-Verbose 'Runbook is executed from Visual Studio Code'
If ( [boolean]( $psEditor.GetEditorContext().CurrentFile.Path ) ) {
Write-Verbose "c"
$MyScriptPath = [System.IO.FileInfo]$psEditor.GetEditorContext().CurrentFile.Path
# $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath(".\")
}
else {
if ( ( [System.IO.FileInfo]$MyInvocation.ScriptName ).Extension -eq '.psm1' ) {
Write-Verbose "d1"
$PSCallStack = Get-PSCallStack
$MyScriptPath =[System.IO.FileInfo] @( $PSCallStack | Where-Object { $_.ScriptName -match '.ps1'} )[0].ScriptName
}
else {
Write-Verbose "d2"
$MyScriptPath = [System.IO.FileInfo]$MyInvocation.scriptname
}
}
}
"Windows PowerShell ISE Host" {
Write-Verbose 'Runbook is executed from ISE'
<#
if ( [boolean]$MyInvocation.ScriptName ) {
Write-Verbose " ScriptName"
}
elseif ( $MyInvocation.MyCommand ) {
Write-Verbose " MyCommand"
if ( ( $MyInvocation.MyCommand.CommandType -eq 'Function' ) -or ( $MyInvocation.MyCommand.Source ).EndsWith( ".psm1" ) ) {
Write-Verbose " Function"
$MyScriptPath = $Script:MyInvocation.MyCommand
}
else {
Write-Verbose " direct"
$MyScriptPath = [System.IO.FileInfo]( $MyInvocation.MyCommand.Source )
}
}
else {
#>
Write-Verbose " CurrentFile"
$MyScriptPath = [System.IO.FileInfo]( $psISE.CurrentFile.FullPath )
# }
}
}
$MyScriptPath
}
Remove-Variable -Name a
$a = Get-ScriptInformartion -Verbose
$a
<#
$a.
$a.CommandType
[System.IO.FileInfo]( $a.GetType()
$a
#>