Skip to content

Commit

Permalink
(GH-667) PowerShell Custom Host - Set $Profile
Browse files Browse the repository at this point in the history
When running a custom host, it appears all automatic variables are set
but `$profile`. It is empty for all the different profiles.  When the
profile value is empty and the documents folder exists for the
user, set `$profile` to a string value that represents what it would
have been set to in a normal PowerShell scenario. We can get away with
a single value for `$profile` as most scripts do not look at each of the
profiles, only what is returned by the `$profile` string, which is
`CurrentUserCurrentHost`.

We check for the existence of the documents folder because it tells us
that this is not the LocalSystem user, and we don't want to set
`$profile` when the SYSTEM user is running choco.
  • Loading branch information
ferventcoder committed Mar 26, 2016
1 parent def91a5 commit 480cf4b
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/chocolatey/infrastructure.app/services/PowershellService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,17 @@ private PowerShellExecutionResults run_host(ChocolateyConfiguration config, stri
}
};


var documentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments, Environment.SpecialFolderOption.DoNotVerify);
var currentUserCurrentHostProfile = _fileSystem.combine_paths(documentsFolder, "WindowsPowerShell\\Microsoft.PowerShell_profile.ps1");

var profileFix = @"
if ((Test-Path(""{0}"")) -and ($profile -eq $null -or $profile -eq '')) {{
$global:profile = ""{1}""
}}
".format_with(documentsFolder, currentUserCurrentHostProfile);

pipeline.Commands.Add(new Command(profileFix, isScript: true, useLocalScope: false));
pipeline.Commands.Add(new Command(commandToRun, isScript: true, useLocalScope: false));

try
Expand Down

0 comments on commit 480cf4b

Please sign in to comment.