-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-ScriptFileInfo.ps1
More file actions
139 lines (110 loc) · 3.97 KB
/
Get-ScriptFileInfo.ps1
File metadata and controls
139 lines (110 loc) · 3.97 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<#PSScriptInfo
.VERSION 0.0
.GUID 23cd18fc-467d-4a15-a0b6-0418c51a9b3c
.AUTHOR David Walker, Sitecore Dave, Radical Dave
.COMPANYNAME David Walker, Sitecore Dave, Radical Dave
.COPYRIGHT David Walker, Sitecore Dave, Radical Dave
.TAGS powershell file io script scriptfileinfo
.LICENSEURI https://github.com/Radical-Dave/Get-ScriptFileInfo/blob/main/LICENSE
.PROJECTURI https://github.com/Radical-Dave/Get-ScriptFileInfo
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
#>
<#
.SYNOPSIS
Gets a script file including metadata with helpers
.DESCRIPTION
Gets a script file including metadata with helpers
.EXAMPLE
PS> .\Get-ScriptFileInfo path
.Link
https://github.com/Radical-Dave/Get-ScriptFileInfo
.OUTPUTS
System.String
#>
#####################################################
# Get-ScriptFileInfoScript
#####################################################
function Get-ScriptFileInfoScript
{
[CmdletBinding()]
Param(
[Parameter(Mandatory=$false)]
[string] $Path
)
process
{
if (!$Path) { $Path = $PSScriptPath}
if (!$Path) { $Path = $MyInvocation.MyCommand.Path}
return Get-Content $Path
}
}
#####################################################
# Get-ScriptFileInfoProperty
#####################################################
function Get-ScriptFileInfoProperty
{
[CmdletBinding()]
Param(
[Parameter(Mandatory=$false)]
[string] $Path,
[string] $Property
)
process
{
if (!$PSScriptScript) { $PSScriptScript = Get-ScriptFileInfoScript $Path }
$startPos = $PSScriptScript.IndexOf(".$Property")
if ($startPos -eq -1) { return '' }
$endPos = $PSScriptScript.IndexOf(".", $startPos + 1)
if ($endPos -ne -1) { return $PSScriptScript.Substring($startPos, $endPos - $startPos).Replace(".$Property", "") }
return ''
}
}
#####################################################
# Get-ScriptFileInfo
#####################################################
function Get-ScriptFileInfo {
Param(
# Path of script
#[ValidateScript({Test-Path $_})]
[Parameter(Mandatory=$false, Position=0)] [string]$path,
# Set - Set Global variables from ScriptFileInfo
[Parameter(Mandatory=$false)] [switch]$Set = $false
)
$ErrorActionPreference = 'Stop'
$PSScriptName = $MyInvocation.MyCommand.Name.Replace(".ps1","")
Write-Verbose "$PSScriptName start -Set:$Set"
Write-Verbose "PSScriptPath:$PSScriptPath"
if (!$path) { if ($PSScriptPath) { $path = $PSScriptPath } }
#if (!$path) { $path = $MyInvocation.MyCommand.Path}
Write-Verbose "path:$path"
$PSScriptScript = Get-Content $path
Write-Verbose "PSScriptScript.Length:$($PSScriptScript.Length)"
if ($Set) { Set-Variable -Name PSScriptScript -Value $PSScriptScript -Scope Global }
if ($PSScriptScript) {
$PSScriptAuthor = "$($PSScriptScript | Where-Object {$_ -like '.AUTHOR*'})"
if ($PSScriptAuthor.IndexOf('.AUTHOR') -gt -1) {$PSScriptAuthor = $PSScriptAuthor.Remove(0, 8) }
if ($Set) { Set-Variable -Name PSScriptAuthor -Value $PSScriptAuthor -Scope Global }
Write-Verbose "PSScriptAuthor:$PSScriptAuthor"
#$PSScriptDesc = Get-ScriptFileInfoProperty $path, 'DESCRIPTION'
$PSScriptDesc = "$($PSScriptScript | Where-Object {$_ -like '.DESCRIPTION*'})"
#$PSScriptDesc = $PSScriptScript | Select-String '.DESCRIPTION'
if ($PSScriptDesc.IndexOf('.DESCRIPTION') -gt -1) {$PSScriptDesc = $PSScriptDesc.Remove(0, 12) }
if ($Set) { Set-Variable -Name PSScriptDesc -Value $PSScriptDesc -Scope Global }
Write-Verbose "PSScriptDesc:$PSScriptDesc"
}
Write-Verbose "$PSScriptName end"
#Version = "2.0"
#CompanyName = "Contoso"
$ScriptFileInfo = @{
Path = $path
Script = $PSScriptScript
Author = $PSScriptAuthor
Description = $PSScriptDesc
}
return $ScriptFileInfo
}
Get-ScriptFileInfo