【问题标题】:Powershell SQLPS module not importing within C#Powershell SQLPS模块未在C#中导入
【发布时间】:2018-11-02 17:51:49
【问题描述】:

我正在尝试在 C# 中的 Powershell 中执行 SQL 查询。我已经通过 ActiveDirectory cmdlet 成功地做到了这一点,并希望更进一步。

我的第一个问题是,虽然以下格式适用于 ActiveDirectory(和 ISE),但它在 C# 中失败:

using (PowerShell pS = PowerShell.Create())
    {
        pS.AddCommand("import-module");
        pS.AddArgument("sqlps");
        pS.Invoke();
    }

我早就将安全设置为无限制,但我得到的错误是:

CmdletInvocationException 未处理

文件 C:\Program Files (x86)\Microsoft SQL Server\110\Tools\PowerShell\Modules\sqlps\Sqlps.ps1 无法加载,因为在此系统上禁用了运行脚本。有关详细信息,请参阅http://go.microsoft.com/fwlink/?LinkID=135170 上的 about_Execution_Policies。

但是,如果我这样运行,我不会收到任何错误,尽管稍后的“Get-Module -all”调用显示没有模块的迹象:

using (PowerShell pS = PowerShell.Create())
    {
        pS.AddScript("Import-Module sqlps");
        pS.Invoke();
    }

如果我尝试导入 ActiveDirectory 模块并调用 Get-Module,它不会显示任何内容。

这是怎么回事?

【问题讨论】:

    标签: c# powershell sqlps


    【解决方案1】:

    我对 C sharp 不是很好,但是当从 powershell 外部调用脚本时,执行程序时会出现一个标志以绕过执行策略,即

    powershell.exe -executionpolicy bypass -command "& '\\somepath\somescript.ps1' "
    

    这允许调用远程脚本,因为即使使用不受限制的设置,我仍然发现它想要提示执行某些脚本,因此例如在任务调度程序中它将无法运行。

    此外,在导入 SQLPS 时,我还发现添加 -DisableNameChecking 标志很有用,您可能还希望事先推送您的位置并在之后弹出它,否则您将最终进入 SQLPS PSdrive,无法访问本地位置,如果你需要它。

    【讨论】:

    • 我从来没有遇到过任务调度程序的问题——我有许多脚本我每晚运行都没有问题。但是,我会看看是否有在 C# 中执行的类似解决方案。至于 SQLPS 的导入,我已经尝试过该参数以及指定要拉入 (import-module sqlps -cmdlet invoke-sqlcmd) 的 cmdlet,结果都没有差异。
    【解决方案2】:

    你尝试过这样的事情吗?

            PowerShell ps = PowerShell.Create();
            ps.AddScript("set-executionpolicy unrestricted -scope process");
            ps.AddScript("import-module sqlps");
            ps.AddScript("get-module sqlps");
    
            var m = ps.Invoke();
            foreach (var mm in m.Select(x => x.BaseObject as PSModuleInfo))
                Console.WriteLine(new { mm.Name, mm.Version });
    

    【讨论】:

    • 虽然这段代码可以回答问题,但最好包含一些上下文,解释如何它的工作原理和何时 i> 使用它。从长远来看,纯代码的答案没有用处。
    【解决方案3】:

    我对 sqlServer ps 模块也有类似的问题。看起来从 C# 执行时,您需要手动将模块加载到运行空间中才能使其工作。

    string scriptText = File.ReadAllText("yourScript.ps1");
    
    //This is needed to use Invoke-sqlcommand in powershell. The module needs to be loaded into the runspace before executing the powershell.
    
    InitialSessionState initial = InitialSessionState.CreateDefault();
    initial.ImportPSModule(new string[] { @"SqlServer\SqlServer.psd1" });
    
    Runspace runspace = RunspaceFactory.CreateRunspace(initial);
    runspace.Open();
    
    using (PowerShell psInstance = PowerShell.Create())
    {
       psInstance.Runspace = runspace;
       psInstance.AddScript(scriptText);
       var PSOutput = psInstance.Invoke();
    }
    

    同时添加位于 SqlServer.psd1 中的所有引用。该文件通常位于“C:\Program Files\WindowsPowerShell\Modules\SqlServer”中。我将文件夹添加到我的解决方案中,以便能够在远程服务器上执行。 您需要添加 Microsoft.SqlServer.BatchParser.dll 引用才能从 Powershell 执行 invoke-sqlcommand。 您应该能够对 sqlps 模块执行相同的操作。而是使用较新的 SqlServer。

    【讨论】:

      猜你喜欢
      • 2016-05-31
      • 2013-06-08
      • 2017-12-14
      • 1970-01-01
      • 2016-11-12
      • 2011-08-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多