-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.ps1
145 lines (126 loc) · 5 KB
/
functions.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
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
140
141
142
143
144
145
function test-doIHaveRole(){
# Determine if permission is held by the *current* user.
# Default switch/role is 'administrator'
# Forces user to specify a 'role' -
# Enumerate possibile roles with: [Security.Principal.WindowsBuiltInRole].GetEnumValues()
param (
[Security.Principal.WindowsBuiltInRole]$role = [Security.Principal.WindowsBuiltInRole]::Administrator
)
$user = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
return $user.IsInRole($role)
}
function get-CurrentDir(){
# Shows casting an object into a string
return [string]$(Get-Location)
}
function get-ScriptDir(){
# Shows where script is, not where it's running
# Useful for finding other files alongside the script
return [string]$($PSScriptRoot)
}
function get-CurrentUserHomeDir(){
# Shows returning a system environment variable
return [string]$($env:USERPROFILE)
}
function get-ScriptName(){
# Return Script Name (Without Path)
# $MyInvocation.ScriptName
return [string]$(Split-Path $MyInvocation.PSCommandPath -Leaf)
}
function get-TempDir(){
# Get system defined temporary writeable path
# Guaranteed writable
return [string]$([System.IO.Path]::GetTempPath())
}
function get-TempFile(){
# Get system defined temporary writeable file
# Specify extension if necessary, otherwise will be .tmp
param (
[Parameter(Mandatory=$False)]
[string]$Extension,
[Parameter(Mandatory=$False)]
[string]$FileName
)
If ($PSBoundParameters.ContainsKey('Extension')){
$OutputPath = [string]$([System.IO.Path]::GetTempFileName())
If ($Extension -notmatch "^\.") {$Extension = ".$($Extension)"}
$OutputPath = $OutputPath -replace "\.tmp$", $Extension
} elseif ($PSBoundParameters.ContainsKey('FileName')){
$OutputPath = Join-Path -Path $([System.IO.Path]::GetTempPath()) -ChildPath $FileName
} else {
$OutputPath = [string]$([System.IO.Path]::GetTempFileName())
}
return [string]$($OutputPath)
}
function Get-FileVersion(){
# avoiding interface and SxS issues, returns the versioninfo from a file.
param (
[Parameter(Mandatory=$True)]
[string]$FilePath
)
$oInfo = $(get-item -Path $FilePath).VersionInfo
return "$($oInfo.FileMajorPart).$($oInfo.FileMinorPart).$($oInfo.FileBuildPart).$($oInfo.FilePrivatePart)"
}
function write-Log(){
# Write log to file, make function pipeline capable as an example.
# Use example to show validateset for input parameters (shortcut defining own enum)
# Default to %temp%\scriptname.log
# Demonstrate long method of function definitions
param (
[Parameter(
Mandatory=$true,
ValueFromPipeline=$true,
Position=0)]
[ValidateNotNullorEmpty()]
[string]$Message,
[Parameter(Mandatory=$false)]
[ValidateSet("EventLog","File")]
[String]$Destination = "File",
[Parameter(Mandatory=$false)]
[ValidateSet("Information","Warning","Error","Debug","Verbose")]
[String]$Level = "Information",
[Parameter(Mandatory=$false)]
[string]$LogFile = [System.IO.Path]::ChangeExtension($(Join-Path -Path $(get-TempDir) -ChildPath $(get-ScriptName)),".log"),
[Parameter(Mandatory=$false)]
[switch]$Visible
)
# Iterate once per call
Begin {
#Convert Level to Event Log compatible value
Switch ($Level) {
"information" { $ELEntryType = "Information" }
"warning" { $ELEntryType = "Warning" }
"error" { $ELEntryType = "Error" }
"debug" { $ELEntryType = "Information" }
"verbose" { $ELEntryType = "Information" }
}
If ($Visible) {
Write-Host "Writing to Logfile $LogFile"
}
}
# Iterate once per item on the pipeline
Process {
$TotalMessage = "$((Get-Date).ToString("yyyy-MM-dd HH:mm:ss")) [$($Level)] $($Message)"
# Write Content to File
Switch ($Destination) {
"File" {
Add-Content -Path $LogFile -Value $TotalMessage
}
"EventLog" {
# If necessary - change later to add intelligence about source etc.
# Combine this later: [System.Diagnostics.EventLogEntryType]$entryType = "Information"
Write-EventLog -LogName "Application" -Source "Application" -EventID 1 -EntryType $ELEntryType -Message $TotalMessage
}
}
If ($Visible) {
$ScreenMessage = "$($TotalMessage)"
Switch ($Level) {
"information" { Write-Host "$ScreenMessage" }
"warning" { Write-Warning "$ScreenMessage" }
"error" { Write-Error "$ScreenMessage" }
"debug" { Write-Debug "$ScreenMessage" -Debug:$true }
"verbose" { Write-Verbose "$ScreenMessage" -Verbose:$true }
}
}
}
}