-
Notifications
You must be signed in to change notification settings - Fork 909
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
GH-303 Install-ChocolateyPath Expands Variables in PATH, Overwriting #373
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,11 +10,27 @@ | |
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
function Get-EnvironmentVariable([string] $Name, [System.EnvironmentVariableTarget] $Scope) { | ||
[Environment]::GetEnvironmentVariable($Name, $Scope) | ||
} | ||
|
||
# Some enhancements to think about here. | ||
# $machinePath = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey("SYSTEM\CurrentControlSet\Control\Session Manager\Environment\").GetValue("PATH", "", [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames).ToString(); | ||
# limitations under the License. | ||
|
||
function Get-EnvironmentVariable([string] $Name, [System.EnvironmentVariableTarget] $Scope, [bool] $PreserveVariables = $False) { | ||
if ($pathType -eq [System.EnvironmentVariableTarget]::Machine) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The parameter name is |
||
$reg = [Microsoft.Win32.Registry]::Machine.OpenSubKey("Environment", $true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Look at my comment on line 36 - that's the machine registry path. It's not top level at the hive like it is for HKCU. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed this has not yet been updated. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mnadel still waiting for fixes here as well. |
||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does not handle the third possible EnvironmentVariableTarget, Process (which has nothing to do with the registry). |
||
$reg = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey("Environment", $true) | ||
} | ||
|
||
if ($PreserveVariables -eq $True) { | ||
$option = [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames | ||
} else { | ||
$option = [Microsoft.Win32.RegistryValueOptions]::None | ||
} | ||
|
||
$value = $reg.GetValue('Path', $null, $option) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, thanks. Will update PR.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed this hasn't yet been updated. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mnadel Take a look here, this PR hasn't been updated. |
||
|
||
$reg.Close() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. RegistryKey objects wrap unmanaged resources (registry handles), so their usage should be guarded by try...finally:
Otherwise, the handles will be leaked in case of errors. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
return $value | ||
} | ||
|
||
# Some enhancements to think about here. | ||
# $machinePath = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey("SYSTEM\CurrentControlSet\Control\Session Manager\Environment\").GetValue("PATH", "", [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames).ToString(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be more elegant (and more conforming with PowerShell conventions) to declare the argument as [switch] instead of [bool].
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
true