Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# $global:在PowerShell会话中设置,但不在System.Management.Automation.PowerShell实例中设置_C#_.net_Powershell - Fatal编程技术网

C# $global:在PowerShell会话中设置,但不在System.Management.Automation.PowerShell实例中设置

C# $global:在PowerShell会话中设置,但不在System.Management.Automation.PowerShell实例中设置,c#,.net,powershell,C#,.net,Powershell,我正在将通常在独立PowerShell会话中运行的PowerShell脚本(StartBackup.ps1)的执行移动到C#应用程序中。该脚本通常直接在PowerShell中执行,导入模块/DLL,调用其他脚本并设置大量变量 在C#应用程序中,我有: using (PowerShell powerShell = PowerShell.Create()) { powerShell.AddCommand("Set-ExecutionPolicy"); powerShell.AddPa

我正在将通常在独立PowerShell会话中运行的PowerShell脚本(StartBackup.ps1)的执行移动到C#应用程序中。该脚本通常直接在PowerShell中执行,导入模块/DLL,调用其他脚本并设置大量变量

在C#应用程序中,我有:

using (PowerShell powerShell = PowerShell.Create())
{
    powerShell.AddCommand("Set-ExecutionPolicy");
    powerShell.AddParameter("Scope", "Process");
    powerShell.AddParameter("ExecutionPolicy", "RemoteSigned");

    powerShell.AddCommand("Set-Location");
    powerShell.AddParameter("Path", "E:\\BackupTools");

    powerShell.AddCommand("E:\\BackupTools\\StartBackup.ps1", false);
    powerShell.AddParameter("Type", "Closed");

    Collection<PSObject> results = powerShell.Invoke();

    foreach (var resultItem in results)
    {
        ...
    }
}
使用(PowerShell PowerShell=PowerShell.Create())
{
powerShell.AddCommand(“设置执行策略”);
powerShell.AddParameter(“范围”、“过程”);
AddParameter(“ExecutionPolicy”、“RemoteSigned”);
powerShell.AddCommand(“设置位置”);
powerShell.AddParameter(“路径”,“E:\\BackupTools”);
powerShell.AddCommand(“E:\\BackupTools\\StartBackup.ps1”,false);
powerShell.AddParameter(“类型”、“关闭”);
收集结果=powerShell.Invoke();
foreach(结果中的var resultItem)
{
...
}
}
上面的内容一直运行到$global:stufacts设置的地方,这就是它开始抛出错误的地方。所有这些值都为null/空

我添加了几个powerShell.add命令,以检查这些值是否在脚本执行后设置,并且在powerShell实例中它们实际上都为null。在独立的shell中,它们都设置得很好

这里的问题是什么?为什么PowerShell实例与实际shell不同


编辑:目的并不是为了简单地解雇和忘记脚本。其目的是让它完成它的工作,然后继续处理它在PowerShell实例中留下的任何工件,就像我通常使用的PowerShell.exe一样。

如果您只想执行现有的PowerShell脚本,最简单的方法就是使用该类。您可以构建命令行并运行它

如果要在C代码中构建脚本本身,则需要C#PowerShell类

此外,您的
AddCommand
将链接这些命令。这是你的要求吗

调用AddCommand()方法将此内容添加到执行管道中


您尚未显示$global stuff的设置位置或错误。您的
set Location
命令无效,这可能会导致您的问题。但是如果没有它,很难说清楚。@PetSerAl,你能详细解释一下为什么它没有效果吗?你正在用
PowerShell
类构建的管道,看起来是这样的:
Set ExecutionPolicy-Scope:Process-ExecutionPolicy:RemoteSigned | Set Location-Path:E:\BackupTools |。E:\\BackupTools\\StartBackup.ps1-类型:Closed
。试着在“实际的外壳”中运行它,看看结果。这对我来说是一个巨大的错误。美元的全球价值确实是由外部设定的。策略、路径和最终脚本命令之间没有PowerShell.Invoker()。
using (PowerShell PowerShellInstance = PowerShell.Create())
{
    // use "AddScript" to add the contents of a script file to the end of the execution pipeline.
    // use "AddCommand" to add individual commands/cmdlets to the end of the execution pipeline.
    PowerShellInstance.AddScript("param($param1) $d = get-date; $s = 'test string value'; " +
            "$d; $s; $param1; get-service");

    // use "AddParameter" to add a single parameter to the last command/script on the pipeline.
    PowerShellInstance.AddParameter("param1", "parameter 1 value!");
}