-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDemo.ps1
78 lines (76 loc) · 2.22 KB
/
Demo.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
Set-StrictMode -Version Latest
dotnet build (Join-Path -Path source -ChildPath Alias) -o Demo
Set-Location -Path Demo
@'
Set-StrictMode -Version Latest
$ErrorActionPreference=[System.Management.Automation.ActionPreference]::Stop
function Set-Alias {
[CmdletBinding()]
param(
# Alias file name
[Parameter(Mandatory = $true)]
[string]
$Alias,
# Command file name or path
[Parameter(Mandatory = $true)]
[string]
$Command,
# Command arguments
[Parameter(Mandatory = $true, ValueFromRemainingArguments = $true)]
[string[]]
$Arguments
)
#region
$Target = Join-Path -Path $PSScriptRoot -ChildPath Alias.exe
$LinkPath = Join-Path -Path $PSScriptRoot -ChildPath $Alias
#endregion
#region
& $Target set $Alias $Command @Arguments
if ($?) {
try {
[void](New-Item -Verbose:$VerbosePreference -ErrorAction $ErrorActionPreference -Path "$LinkPath.exe" -ItemType HardLink -Value $Target)
Write-Verbose -Message ('{0} created' -f $LinkPath)
} catch {
Write-Verbose -Message ('{0} not created' -f $LinkPath)
}
}
#endregion
}
[string[]]$Arguments = $args
Set-Alias @Arguments
'@ | Set-Content -Path alias-set.ps1
@'
Set-StrictMode -Version Latest
$ErrorActionPreference=[System.Management.Automation.ActionPreference]::Stop
function Remove-Alias {
[CmdletBinding()]
param(
# Alias file name
[Parameter(Mandatory = $true)]
[string]
$Alias
)
#region
$Target = Join-Path -Path $PSScriptRoot -ChildPath Alias.exe
$LinkPath = Join-Path -Path $PSScriptRoot -ChildPath $Alias
#endregion
#region
& $Target unset $Alias
if ($?) {
try {
[void](Remove-Item -Verbose:$VerbosePreference -ErrorAction $ErrorActionPreference -Path "$LinkPath.exe")
Write-Verbose -Message ('{0} removed' -f $LinkPath)
} catch {
Write-Verbose -Message ('{0} not removed' -f $LinkPath)
}
}
#endregion
}
[string[]]$Arguments = $args
Remove-Alias @Arguments
'@ | Set-Content -Path alias-unset.ps1
$Alias = Resolve-Path -Path Alias.exe
& $Alias set alias-set powershell -- -NoProfile -NoLogo -File (Resolve-Path -Path alias-set.ps1)
$AliasSet = New-Item -ItemType HardLink -Path alias-set.exe -Value Alias.exe
& $AliasSet alias-unset powershell -- -NoProfile -NoLogo -File (Resolve-Path -Path alias-unset.ps1)
& $AliasSet mklink cmd /c mklink