|
1 | | -#Requires -Modules Admin |
| 1 | +#Requires -Modules Admin, DynamicParams |
2 | 2 |
|
3 | 3 | function Remove-EnvironmentPath { |
4 | 4 | <# |
@@ -51,30 +51,26 @@ function Remove-EnvironmentPath { |
51 | 51 | ) |
52 | 52 |
|
53 | 53 | DynamicParam { |
54 | | - $runtimeDefinedParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary |
55 | | - $attributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] |
56 | | - |
57 | | - $parameterName = 'Path' |
58 | | - $parameterAttribute = New-Object System.Management.Automation.ParameterAttribute |
59 | | - $parameterAttribute.Mandatory = $true |
60 | | - $parameterAttribute.Position = 1 |
61 | | - $parameterAttribute.HelpMessage = 'Name of the font to uninstall.' |
62 | | - $parameterAttribute.ValueFromPipeline = $true |
63 | | - $parameterAttribute.ValueFromPipelineByPropertyName = $true |
64 | | - $attributeCollection.Add($parameterAttribute) |
65 | | - |
66 | | - $parameterValidateSet = Get-EnvironmentPath -Scope $Scope -AsArray |
67 | | - $validateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($parameterValidateSet) |
68 | | - $attributeCollection.Add($validateSetAttribute) |
69 | | - |
70 | | - # Adding a parameter alias |
71 | | - $parameterAlias = 'FullName' |
72 | | - $aliasAttribute = New-Object System.Management.Automation.AliasAttribute -ArgumentList $parameterAlias |
73 | | - $attributeCollection.Add($aliasAttribute) |
74 | | - |
75 | | - $runtimeDefinedParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($parameterName, [string[]], $attributeCollection) |
76 | | - $runtimeDefinedParameterDictionary.Add($parameterName, $runtimeDefinedParameter) |
77 | | - return $runtimeDefinedParameterDictionary |
| 54 | + $DynamicParamDictionary = New-DynamicParamDictionary |
| 55 | + |
| 56 | + $dynPath = @{ |
| 57 | + Name = 'Path' |
| 58 | + Alias = 'FullName' |
| 59 | + Type = [string[]] |
| 60 | + Mandatory = $true |
| 61 | + HelpMessage = 'Name of the font to uninstall.' |
| 62 | + ValueFromPipeline = $true |
| 63 | + ValueFromPipelineByPropertyName = $true |
| 64 | + ValidateSet = if ([string]::IsNullOrEmpty($Scope)) { |
| 65 | + Get-EnvironmentPath -Scope 'CurrentUser' -AsArray |
| 66 | + } else { |
| 67 | + Get-EnvironmentPath -Scope $Scope -AsArray |
| 68 | + } |
| 69 | + DynamicParamDictionary = $DynamicParamDictionary |
| 70 | + } |
| 71 | + New-DynamicParam @dynPath |
| 72 | + |
| 73 | + return $DynamicParamDictionary |
78 | 74 | } |
79 | 75 |
|
80 | 76 | begin { |
@@ -104,12 +100,11 @@ Please run the command again with elevated rights (Run as Administrator) or prov |
104 | 100 | } |
105 | 101 |
|
106 | 102 | end { |
107 | | - if ([System.Environment]::OSVersion.Platform -eq 'Win32NT') { |
108 | | - $pathSeparator = ';' |
109 | | - } else { |
110 | | - $pathSeparator = ':' |
111 | | - } |
| 103 | + $pathSeparator = ';' |
112 | 104 | $environmentPath = $environmentPath -join $pathSeparator |
| 105 | + $environmentPath = $environmentPath.Trim($pathSeparator) |
| 106 | + $environmentPath = $environmentPath + $pathSeparator |
| 107 | + |
113 | 108 | if ($PSCmdlet.ShouldProcess($environmentPath, 'Remove')) { |
114 | 109 | [System.Environment]::SetEnvironmentVariable('PATH', $environmentPath, [System.EnvironmentVariableTarget]::$target) |
115 | 110 | } |
|
0 commit comments