Skip to content

Commit 6638b97

Browse files
committed
[pwsh profile] Refactor newProfile.ps1
- Bugfix Test-Path. Closes #2. - Prevent dot-sourcing. - Use `$ErrorActionPreference = 'Stop'` unless explicit `-ErrorAction` is set. - Simplify Get-ProfileHelp.
1 parent b42bcf9 commit 6638b97

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

PowerShell/Profile/newProfile.ps1

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,18 @@ param(
2929
[switch]$Force
3030
)
3131

32+
try {
33+
$null = Get-Variable 'foobar' -Scope 1 -ErrorAction Stop
34+
}
35+
catch [System.ArgumentOutOfRangeException] {
36+
throw 'Dot-sourcing this script is not allowed.'
37+
}
38+
catch {}
39+
3240
Set-StrictMode -Version 3.0
41+
if (-not $PSBoundParameters.ContainsKey('ErrorAction')) {
42+
$ErrorActionPreference = 'Stop'
43+
}
3344

3445
# Get content from source files
3546
function Get-SourceContent {
@@ -59,16 +70,18 @@ function Get-SourceContent {
5970
[Alias('gprh')]
6071
[OutputType([psobject])]
6172
param()
62-
$Aliases = @('%ALIASES%')
63-
$Functions = @('%FUNCTIONS%')
64-
$Aliases | Get-Command -ListImported | Select-Object @{n = 'Command'; e = 'DisplayName' }, @{n = 'Parameters'; e = '''''' }
65-
$Functions | Get-Command -ListImported | Select-Object @{n = 'Command'; e = 'Name' }, @{n = 'Parameters'; e = { $_.ParameterSets -join [System.Environment]::NewLine } }
73+
@('%ALIASES%') |
74+
Get-Command -ListImported |
75+
Select-Object @{n = 'Command'; e = 'DisplayName' }, @{n = 'Parameters'; e = '''''' }
76+
@('%FUNCTIONS%') |
77+
Get-Command -ListImported |
78+
Select-Object @{n = 'Command'; e = 'Name' }, @{n = 'Parameters'; e = { $_.ParameterSets -join [System.Environment]::NewLine } }
6679
}
6780
}.ToString()
6881

6982
[object[]]$contentAllHosts = Get-SourceContent -Path "$PSScriptRoot/Common"
7083

71-
if (Test-Path 'env:VBOX_MSI_INSTALL_PATH' -PathType Container) {
84+
if (Test-Path 'env:VBOX_MSI_INSTALL_PATH') {
7285
$contentAllHosts += Get-SourceContent -Path "$PSScriptRoot/VirtualBoxHost"
7386
}
7487

0 commit comments

Comments
 (0)