0

I'm able to run puppet bolt command in powershell. In powershell, I got output as below

Started on winrm://remotemachine

Finished on winrm://remotemachine

STDOUT:
RemoteMachineHostName

Successful on 1 target Run on 1 tartget in 3.2 sec

My C# is as below

        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.

How to fix above issue

Update 1:

I have used below command in powershell script.

bolt command run hostname --targets winrm://158.28.0.546 --no-ssl-verify --user testuser123 --password test@84p
Community
  • 1
  • 1
S.Chandra Sekhar
  • 453
  • 3
  • 11
  • 22

2 Answers2

0

It seems you cannot input the path of the PowerShell to the method AddScript(). Here is an example that runs the PowerShell script through C#:

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().

Charles Xu
  • 29,862
  • 2
  • 22
  • 39
  • I tried with above code still I'm getting zero records. – S.Chandra Sekhar Apr 02 '20 at 16:10
  • I observed that if powershell script contains only command as hostname instead of my bolt command, it is returning data in c# aswell.How to resolve issue. – S.Chandra Sekhar Apr 02 '20 at 16:36
  • Is there anyway to get only STDOUT value without any other messages from above bolt command output. – S.Chandra Sekhar Apr 02 '20 at 17:51
  • @John What is the STDOUT you mean in the remote connect? As I know the remote connect just an interactive session without any output. – Charles Xu Apr 03 '20 at 01:23
  • STDOUT is the powershell output.You can find it in my question. – S.Chandra Sekhar Apr 03 '20 at 01:41
  • @John Yes, I saw. But I mean your PowerShell script is to create an interactive session. So I don't know what does it mean to. – Charles Xu Apr 03 '20 at 01:47
  • Actually I tried your code but it didn't worked.Still getting value 0.I tried with another powershell script which has only command just hostname and I called it from C#.I got hostname output in C# .So I guess with bolt command in powershell, the output I got include some messages like starting finishing STDOUT successful run on messages .I'm thinking if we remove those messages from powershell output and get only STDOUT value,it may work.How to get only STDOUT value, I'm not getting. – S.Chandra Sekhar Apr 03 '20 at 01:47
  • I made it work by changing project build platform type to 64 bit.Thanks for your suggestions. I'm facing another issue https://stackoverflow.com/questions/61032882/unable-to-run-remote-powershell-script-which-is-located-in-remote-azure-vm-d-dr . Can you please guide me on that. – S.Chandra Sekhar Apr 04 '20 at 18:32
0

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.

I have changed project build platform to x64 and build it.

It worked.

S.Chandra Sekhar
  • 453
  • 3
  • 11
  • 22