c# - How do I recreate PowerShell $profile variable? It's empty in a custom Host -


if implement custom powershell host system.management.automation (sma), of automatic variables avaialable, except seems $profile empty. how 1 go recreating it?

is in userprofile + \documents\windowspowershell\microsoft.powershell_profile.ps1? or need worried being in other places?

to clarify, care currentusercurrenthost profile.

details

given powershell script:

write-output "_profiles_"  write-output "currentusercurrenthost = '$($profile.currentusercurrenthost)'" write-output "currentuserallhosts = '$($profile.currentuserallhosts)'" write-output "alluserscurrenthost = '$($profile.alluserscurrenthost)'" write-output "allusersallhosts = '$($profile.allusersallhosts)'" 

running against system powershell has following output:

_profiles_ currentusercurrenthost = 'c:\users\rob\documents\windowspowershell\microsoft.powershell_profile.ps1' currentuserallhosts = 'c:\user\rob\documents\windowspowershell\profile.ps1' alluserscurrenthost = 'c:\windows\system32\windowspowershell\v1.0\microsoft.powershell_profile.ps1' allusersallhosts = 'c:\windows\system32\windowspowershell\v1.0\profile.ps1'

running custom c# powershell host (a system.management.automation.host.pshost implementation) shows:

_profiles_ currentusercurrenthost = '' currentuserallhosts = '' alluserscurrenthost = '' allusersallhosts = ''

background

https://github.com/chocolatey/choco/issues/667

this has been tested in powershell v3 / system.managment.automation (sma) v3 proven out in other powershell versions.

as possible fix, i've come with. mean $profile expected in documents folder.

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)); 

this done way due special accounts localsystem not have profile folder.

note: .format_with string formatter, {{ , }} see converted { , } when finishes string formatting.


Comments