0

我可以在 powershell 中运行 puppet bolt 命令。在powershell中,我得到如下输出

开始于 winrm://remotemachine

在 winrm://remotemachine 上完成

STDOUT:
RemoteMachineHostName

在 1 个目标上成功在 3.2 秒内在 1 个目标上运行

我的 C# 如下

        PowerShell ps = PowerShell.Create();

        ps.AddScript("C:\\User1\\GetRemoteAzureVMHostName.ps1");

        Collection<PSObject> results =  ps.Invoke();  // in results, I'm getting value as 0.

        foreach (PSObject result in results)
        {
            //Do something
        }

我尝试在 Visual Studio 2019 中将构建平台目标更改为 x64,但没有成功。

如何解决上述问题

更新1:

我在 powershell 脚本中使用了以下命令。

bolt command run hostname --targets winrm://158.28.0.546 --no-ssl-verify --user testuser123 --password test@84p
4

2 回答 2

0

看来您无法将 PowerShell 的路径输入到方法AddScript()中。下面是一个通过 C# 运行 PowerShell 脚本的示例:

private static string script =File.ReadAllText(@"Path\Sum.ps1");
private static void CallPS1()
{
    using (Runspace runspace = RunspaceFactory.CreateRunspace())
        {
         runspace.Open();

         PowerShell ps = PowerShell.Create();
         ps.Runspace = runspace;
         ps.AddScript(script);
         ps.Invoke();

         foreach (PSObject result in ps.Invoke())
         {
             Console.WriteLine("CallPS1()");
             Console.WriteLine(result);
         }

}

        }

所以你可以试着把脚本读出来,然后在方法中输入AddScript()

于 2020-04-02T02:55:11.163 回答
0

我发现,Visual Studio 正在调用 32 位 powershell,并且无法将 bolt 命令作为安装在 64 位 powershell 上的 bolt 模块运行。

我已将项目构建平台更改为 x64 并构建它。

有效。

于 2020-04-04T18:07:45.173 回答