示例#1
0
        private async Task <PowerShellResult> ExecuteScriptAsync(string script)
        {
            consoleLogger.Debug(script);

            var documentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            var currentUserCurrentHostProfile = Path.Combine(documentsFolder, "WindowsPowerShell\\Microsoft.PowerShell_profile.ps1");

            var environmentReadyScript = $@"
$env:Path = [System.Environment]::GetEnvironmentVariable(""Path"",""Machine"") + "";"" + [System.Environment]::GetEnvironmentVariable(""Path"",""User"")

if ($profile -eq $null -or $profile -eq '') {{
  $global:profile = ""{currentUserCurrentHostProfile}""
}}

{script}";

            powershell.AddScript(environmentReadyScript);

            var output = await powershell.InvokeAsync();

            var result = output.LastOrDefault();

            Clear();

            return(new PowerShellResult
            {
                AsString = result?.ToString() ?? ""
            });
        }