Skip to content

Commit fa27b13

Browse files
authored
Merge pull request #3787 from corbob/3681-refreshenv-as-system
(#3681) Check for correct computer name when refreshing environment variables
2 parents 6d82bc0 + 5040369 commit fa27b13

File tree

5 files changed

+76
-1
lines changed

5 files changed

+76
-1
lines changed

src/Chocolatey.PowerShell/Chocolatey.PowerShell.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,8 @@
8787
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
8888
</Content>
8989
</ItemGroup>
90+
<ItemGroup>
91+
<None Include="ReadMe.md" />
92+
</ItemGroup>
9093
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
9194
</Project>

src/Chocolatey.PowerShell/Helpers/EnvironmentHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ public static void UpdateSession(PSCmdlet cmdlet)
226226
var computerName = GetVariable(cmdlet, EnvironmentVariables.System.ComputerName, EnvironmentVariableTarget.Process);
227227

228228
// User scope should override (be checked after) machine scope, but only if we're not running as SYSTEM
229-
if (userName != computerName && userName != Shared.EnvironmentNames.System)
229+
if (userName != $"{computerName}$" && userName != Shared.EnvironmentNames.System)
230230
{
231231
scopeList.Add(EnvironmentVariableTarget.User);
232232
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## About this project
2+
3+
The Chocolatey.PowerShell project within the Chocolatey CLI solution is the compiled PowerShell cmdlets.
4+
5+
## Debugging the Chocolatey.PowerShell cmdlets
6+
7+
Because the Chocolatey.PowerShell module is a compiled module, debugging it is a little more involved than a script based module.
8+
If you need to debug cmdlets within a Chocolatey CLI execution, you can merely create a package that calls the Chocolatey.PowerShell function you are debugging, and it will work.
9+
However, if you need to test the functions outside of a Chocolatey CLI invocation, that is a little bit more involved.
10+
Fortunately, Marc-Andr� Moreau provides and [excellent guide](https://awakecoding.com/posts/debugging-powershell-binary-modules-in-visual-studio/) on debugging a binary PowerShell module from Visual Studio.
11+
The below steps are greatly simplified and targetted specifically at the Chocolatey.PowerShell project.
12+
13+
1. Edit or create a `Chocolatey.PowerShell.csproj.user` file beside the `Chocolatey.PowerShell.csproj` file (located at `$ChocoSourceRepository/src/Chocolatey.PowerShell`).
14+
1. Set it's contents to the XML following this list.
15+
1. (Re)Launch Visual Studio to have it pick up the addition of the `Chocolatey.PowerShell.csproj.user` file.
16+
1. Set the `Chocolatey.PowerShell` project as the startup project.
17+
1. Start the debugger.
18+
1. In the Windows PowerShell that launched, import the module with `Import-Module ./Chocolatey.PowerShell.dll`.
19+
1. Now when you call any of the compiled PowerShell files, you will be able to stop at set breakpoints and debug as normal.
20+
21+
```xml
22+
<?xml version="1.0" encoding="utf-8"?>
23+
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
24+
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
25+
<StartAction>Program</StartAction>
26+
<StartProgram>c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe</StartProgram>
27+
</PropertyGroup>
28+
</Project>
29+
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function ConvertTo-Base64String {
2+
<#
3+
.Synopsis
4+
Helper function to Convert a string to a Base64 encoded string.
5+
#>
6+
[CmdletBinding()]
7+
param(
8+
# The string to be converted to base64.
9+
[Parameter(ValueFromPipeline)]
10+
[string]$InputObject
11+
)
12+
$bytes = [system.text.encoding]::Unicode.GetBytes($InputObject)
13+
[Convert]::ToBase64String($bytes)
14+
}

tests/pester-tests/features/EnvironmentVariables.Tests.ps1

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,32 @@ Describe "Ensuring Chocolatey Environment variables are not present (<_>)" -Tag
8787
$Output.Lines | Should -Not -Contain $ExpectedLine -Because $Output.String
8888
}
8989
}
90+
91+
Describe "Ensuring variables are not incorrectly set for the SYSTEM account" -Tag EnvironmentVariables, Chocolatey, SystemAccount {
92+
BeforeAll {
93+
Initialize-ChocolateyTestInstall
94+
New-ChocolateyInstallSnapshot
95+
Invoke-Choco install psexec -s https://community.chocolatey.org/api/v2/
96+
97+
# We execute this as an encoded command as it doesn't seem to like the strings unless it's encoded.
98+
$encodedCommand = @'
99+
"Temp: $($env:TEMP)" > {1}/before.txt
100+
"Tmp: $($env:TMP)" >> {1}/before.txt
101+
Import-Module {0}/helpers/chocolateyProfile.psm1 -Verbose *>&1
102+
refreshenv
103+
"Temp: $($env:TEMP)" > {1}/after.txt
104+
"Tmp: $($env:TMP)" >> {1}/after.txt
105+
'@ -f (Get-ChocolateyTestLocation), $PSScriptRoot | ConvertTo-Base64String
106+
$Output = psexec /accepteula /s powershell.exe -NoProfile -EncodedCommand $encodedCommand 2>$null
107+
}
108+
109+
AfterAll {
110+
Remove-ChocolateyTestInstall
111+
}
112+
113+
It 'Should not change the temp environment variables' {
114+
$Before = Get-Content $PSScriptRoot/before.txt
115+
$After = Get-Content $PSScriptRoot/after.txt
116+
$Before | Should -Be $After -Because $Output
117+
}
118+
}

0 commit comments

Comments
 (0)