forked from bcdady/MyScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMicrosoft.PowerShell_profile.ps1
executable file
·202 lines (178 loc) · 9.05 KB
/
Microsoft.PowerShell_profile.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!pwsh
#Requires -Version 3
#========================================
# NAME : Microsoft.PowerShell_profile.ps1
# LANGUAGE : Microsoft PowerShell
# PowerShell $Profile
# Created by New-Profile function of ProfilePal module
# For more information, see https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles
#========================================
[CmdletBinding()]
param ()
# Uncomment the following 2 lines for testing profile scripts with Verbose output
#'$VerbosePreference = ''Continue'''
#$VerbosePreference = 'Continue'
Write-Verbose -Message 'Detect -Verbose $VerbosePreference'
switch ($VerbosePreference) {
Stop { $IsVerbose = $True }
Inquire { $IsVerbose = $True }
Continue { $IsVerbose = $True }
SilentlyContinue { $IsVerbose = $False }
Default { if ('Verbose' -in $PSBoundParameters.Keys) {$IsVerbose = $True} else {$IsVerbose = $False} }
}
Write-Verbose -Message ('$VerbosePreference = ''{0}'' : $IsVerbose = ''{1}''' -f $VerbosePreference, $IsVerbose)
#Region MyScriptInfo
Write-Verbose -Message ('[{0}] Populating $MyScriptInfo' -f $MyInvocation.MyCommand.Name)
$MyCommandName = $MyInvocation.MyCommand.Name
$MyCommandPath = $MyInvocation.MyCommand.Path
$MyCommandType = $MyInvocation.MyCommand.CommandType
$MyCommandModule = $MyInvocation.MyCommand.Module
$MyModuleName = $MyInvocation.MyCommand.ModuleName
$MyCommandParameters = $MyInvocation.MyCommand.Parameters
$MyParameterSets = $MyInvocation.MyCommand.ParameterSets
$MyRemotingCapability = $MyInvocation.MyCommand.RemotingCapability
$MyVisibility = $MyInvocation.MyCommand.Visibility
if (($null -eq $MyCommandName) -or ($null -eq $MyCommandPath)) {
# We didn't get a successful command / script name or path from $MyInvocation, so check with CallStack
Write-Verbose -Message 'Getting PSCallStack [$CallStack = Get-PSCallStack]'
$CallStack = Get-PSCallStack | Select-Object -First 1
# $CallStack | Select Position, ScriptName, Command | format-list # FunctionName, ScriptLineNumber, Arguments, Location
$myScriptName = $CallStack.ScriptName
$myCommand = $CallStack.Command
Write-Verbose -Message ('$ScriptName: {0}' -f $myScriptName)
Write-Verbose -Message ('$Command: {0}' -f $myCommand)
Write-Verbose -Message 'Assigning previously null MyCommand variables with CallStack values'
$MyCommandPath = $myScriptName
$MyCommandName = $myCommand
}
#'Optimize New-Object invocation, based on Don Jones' recommendation: https://technet.microsoft.com/en-us/magazine/hh750381.aspx
$properties = [ordered]@{
'CommandName' = $MyCommandName
'CommandPath' = $MyCommandPath
'CommandRoot' = Split-Path -Path $MyCommandPath -Parent
'CommandType' = $MyCommandType
'CommandModule' = $MyCommandModule
'ModuleName' = $MyModuleName
'CommandParameters' = $MyCommandParameters.Keys
'ParameterSets' = $MyParameterSets
'RemotingCapability' = $MyRemotingCapability
'Visibility' = $MyVisibility
}
$MyScriptInfo = New-Object -TypeName PSObject -Property $properties -ErrorAction SilentlyContinue
Write-Verbose -Message ('[{0}] $MyScriptInfo populated' -f $MyInvocation.MyCommand.Name)
# Cleanup
foreach ($var in $properties.Keys) {
Remove-Variable -Name ('My{0}' -f $var) -Force -ErrorAction SilentlyContinue
}
Remove-Variable -Name properties
Remove-Variable -Name var
if ($IsVerbose) {
Write-Verbose -Message '$MyScriptInfo:'
$Script:MyScriptInfo
}
#End Region
# capture starting path so we can go back after other things below might move around
$startingPath = $PWD.Path
Write-Output -InputObject ' # Loading PowerShell $Profile: CurrentUserCurrentHost #'
Write-Verbose -Message (' ... from {0} # ' -f $MyScriptInfo.CommandPath)
# Display PowerShell Execution Policy -- on Windows only (as ExecutionPolicy is not supported on non-Windows platforms)
if ($IsWindows) {
Write-Output -InputObject 'Current PS execution policy is:'
Get-ExecutionPolicy -List
}
Push-Location -Path $MyScriptInfo.CommandRoot -PassThru
# https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_prompts
if ($IsVerbose) {Write-Output -InputObject ''}
Write-Verbose -Message 'Defining custom prompt'
function prompt {
# $IsWindows, if not already provided by pwsh $Host, is set in bootstrap.ps1
if ($IsWindows) {
if (-not (Get-Variable -Name IsAdmin -ValueOnly -ErrorAction SilentlyContinue)) {
$IsAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')
if ($IsAdmin) { $AdminPrompt = '[ADMIN]:' } else { $AdminPrompt = '' }
}
} else {
if (-not (Get-Variable -Name IsRoot -ValueOnly -ErrorAction SilentlyContinue)) {
$IsRoot = ($ENV:USER -eq 'root')
if ($IsRoot) { $AdminPrompt = '[root]:' } else { $AdminPrompt = '' }
}
$Env:COMPUTERNAME = (hostname)
}
if (Get-Variable -Name PSDebugContext -ValueOnly -ErrorAction SilentlyContinue) { $DebugPrompt = '[DEBUG]:' } else { $DebugPrompt = '' }
if (Get-Variable -Name PSConsoleFile -ValueOnly -ErrorAction SilentlyContinue) { $PSCPrompt = "[PSConsoleFile: $PSConsoleFile]" } else { $PSCPrompt = '' }
if ($NestedPromptLevel -ge 1) { $PromptLevel = 'PS .\> >' } else { $PromptLevel = 'PS .\>' }
return "[{0} @ {1}]`n{2}{3}{4}{5}" -f $Env:COMPUTERNAME, $PWD.Path, $AdminPrompt, $PSCPrompt, $DebugPrompt, $PromptLevel
}
# Invoke Bootstrap.ps1, from the same root path as this $PROFILE script
if (Get-Variable -Name 'myPS*' -ValueOnly -ErrorAction SilentlyContinue) {
# PowerShell path variables have been initialized via a recent invocation of bootstrap.ps1
Write-Output -InputObject ''
Write-Output -InputObject 'My PowerShell paths:'
Get-Variable -Name 'myPS*' | Format-Table -AutoSize
} else {
# initialize variables, via bootstrap.ps1
Write-Verbose -Message ('(Test-Path -Path ./bootstrap.ps1): {0}' -f (Test-Path -Path ./Bootstrap.ps1))
if (Test-Path -Path ./Bootstrap.ps1) {
#Write-Verbose -Message '. ./Bootstrap.ps1'
. ./Bootstrap.ps1
}
if (Get-Variable -Name 'myPS*' -ValueOnly -ErrorAction SilentlyContinue) {
# PowerShell path variables have been initialized via a recent invocation of bootstrap.ps1
Write-Output -InputObject ''
Write-Output -InputObject 'My PowerShell paths:'
Get-Variable -Name 'myPS*' | Format-Table -AutoSize
} else {
Write-Warning -Message './Bootstrap.ps1 may have encountered errors.'
}
#End Region
<# Yes! This even works in XenApp!
& Invoke-Expression (New-Object Net.WebClient).DownloadString('http://bit.ly/e0Mw9w')
# start-sleep -Seconds 3
#>
if (Get-Command -Name Set-ConsoleTitle -ErrorAction SilentlyContinue) {
# Call Set-ConsoleTitle, from ProfilePal module
if ($IsVerbose) {Write-Output -InputObject ''}
Write-Verbose -Message ' # Set-ConsoleTitle >'
Set-ConsoleTitle
Write-Verbose -Message ' # < Set-ConsoleTitle'
if ($IsVerbose) {Write-Output -InputObject ''}
}
# Display execution policy, for convenience, on Windows only (as ExecutionPolicy is not supported on non-Windows platforms)
if ($IsWindows) {
Write-Output -InputObject 'PowerShell Execution Policy: '
Get-ExecutionPolicy -List | Format-Table -AutoSize
}
# Loading ProfilePal Module, and only if successful, call Set-ConsoleTitle to customize the ConsoleHost window title
Import-Module -Name ProfilePal
if ($?) {
# Call Set-ConsoleTitle function from ProfilePal module
Set-ConsoleTitle
}
Write-Verbose -Message ('$HostOS = ''{0}''' -f $HostOS)
# Detect host OS and then jump to the OS specific profile sub-script
if ($IsLinux) {
$Private:SubProfile = (Join-Path -Path (split-path -Path $MyScriptInfo.CommandPath) -ChildPath 'Microsoft.PowerShell_profile-Linux.ps1')
}
if ($IsMacOS) {
$Private:SubProfile = (Join-Path -Path (split-path -Path $MyScriptInfo.CommandPath) -ChildPath 'Microsoft.PowerShell_profile-macOS.ps1')
}
if ($IsWindows) {
$Private:SubProfile = (Join-Path -Path (split-path -Path $MyScriptInfo.CommandPath) -ChildPath 'Microsoft.PowerShell_profile-Windows.ps1')
}
if ($IsVerbose) {Write-Output -InputObject ''}
Write-Verbose -Message ('$SubProfile = ''{0}''' -f $Private:SubProfile)
# Load/invoke OS specific profile sub-script
if (Test-Path -Path $SubProfile) {
# dot-source it
. $SubProfile
} else {
throw ('Failed to locate OS specific profile sub-script: {0}' -f $SubProfile)
}
Remove-Variable -Name SubProfile -Force
if ($IsVerbose) {Write-Output -InputObject ''}
Write-Output -InputObject ' # End of PowerShell $Profile CurrentUserCurrentHost #'
# For intra-profile/bootstrap script flow Testing
if ($IsVerbose) {
Write-Output -InputObject 'Start-Sleep -Seconds 3'
Start-Sleep -Seconds 3
}