[英]Powershell Invoke() returning zero in C# for bolt command run
I'm able to run puppet bolt command in powershell.我可以在 powershell 中运行 puppet bolt 命令。 In powershell, I got output as below
在powershell中,我得到如下输出
Started on winrm://remotemachine
从 winrm://remotemachine 开始
Finished on winrm://remotemachine
在 winrm://remotemachine 上完成
STDOUT: RemoteMachineHostName
Successful on 1 target Run on 1 tartget in 3.2 sec
在 1 个目标上成功在 3.2 秒内在 1 个目标上运行
My C# is as below我的 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
}
I tried changing build platform target to x64 in Visual Studio 2019 but it didn't worked.我尝试在 Visual Studio 2019 中将构建平台目标更改为 x64,但没有奏效。
How to fix above issue如何解决上述问题
Update 1:更新 1:
I have used below command in powershell script.我在 powershell 脚本中使用了以下命令。
bolt command run hostname --targets winrm://158.28.0.546 --no-ssl-verify --user testuser123 --password test@84p
It seems you cannot input the path of the PowerShell to the method AddScript()
.似乎您无法将 PowerShell 的路径输入到
AddScript()
方法中。 Here is an example that runs the PowerShell script through C#:以下是通过 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);
}
}
}
So you can try to read the script out and then input them in the method AddScript()
.因此,您可以尝试读取脚本,然后将它们输入到
AddScript()
方法中。
I figured it out that, Visual Studio is calling 32 bit powershell and unable to run bolt commands as bolt module installed on 64 bit powershell.我发现,Visual Studio 正在调用 32 位 powershell,并且无法将 bolt 命令作为安装在 64 位 powershell 上的 bolt 模块运行。
I have changed project build platform to x64 and build it.我已将项目构建平台更改为 x64 并构建它。
It worked.有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.