1

I want to read remote yaml file present in remote azure vm from my local machine using C# Runspace.

I'm able to run Powershell commands remotely using C# Runspace but I'm not getting how to read yaml file present in remote system like Azure VM using Runspace.

Below code is used to run powershell command remotely from my local system.

               using (Runspace remoteRunspace = RunspaceFactory.CreateRunspace(connectionInfo))
                {
                    remoteRunspace.Open();
                    using (PowerShell powershell = PowerShell.Create())
                    {    
                        powershell.Runspace = remoteRunspace;
                        powershell.AddScript("Get-Service");    
                     }    
                }
S.Chandra Sekhar
  • 453
  • 3
  • 11
  • 22

1 Answers1

0

Through my testing, I can read the .yml file on the server. Below is the code (.net framework 4.7.2) and screenshot.

44.yml file in vm server.

enter image description here

read file by powershell in vm server.

enter image description here

read file by my code.

enter image description here

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string shell = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
            var target = new Uri("http://***.***.***.**:5985/wsman");
            var secured = new SecureString();
            foreach (char letter in "yourpassword")
            {
                secured.AppendChar(letter);
            }
            secured.MakeReadOnly();

            var credential = new PSCredential("Administrator", secured);
            var connectionInfo = new WSManConnectionInfo(target, shell, credential);

            Runspace remoteRunspace = RunspaceFactory.CreateRunspace(connectionInfo);
            remoteRunspace.Open();
            using (PowerShell powershell = PowerShell.Create())
            {
                powershell.Runspace = remoteRunspace;
                // your yml file in vm 
                powershell.AddScript("$textfile=\"C:\\website\\wps\\upload\\44.yml\";");
                //read file
                powershell.AddScript("Get-Content -Path $textfile");
                powershell.Invoke();

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

                // Display the results.
                foreach (PSObject result in results)
                {
                    Console.WriteLine(result.BaseObject);
                }
            }
            Console.Read();
        }
    }
}
Jason Pan
  • 15,263
  • 1
  • 14
  • 29
  • Thanks for your suggestion. I have one more query. How can we update 44.yaml (located remotely) with data obtained from C# object . I want to use code using YamlDotnet similar to one given in https://stackoverflow.com/questions/37116684/build-a-yaml-document-dynamically-from-c-sharp to create some new data to be updated into existing yaml file. – S.Chandra Sekhar May 27 '20 at 12:51
  • The problem of this post has been solved. This is another question for you. I suggest you create a new post. I will try to code according to your requirements to help you solve the problem. – Jason Pan May 27 '20 at 14:48
  • adding $textFile path as '.\.puppetlabs\bolt\inventory.yaml' instead of 'c;\users\john\.puppetlabs\bolt\inventory.yaml' in above code, returning empty records. Actual path of file is 'c;\users\john\.puppetlabs\bolt\inventory.yaml' and if I use this path in code, getting results but if I use '.\.puppetlabs\bolt\inventory.yaml' returning empty result.How to use path '.\.puppetlabs\bolt\inventory.yaml' – S.Chandra Sekhar May 27 '20 at 18:04
  • The first thing I want to tell you is that you want to use this file path `.\.Puppetlabs\ bolt\inventory.yaml` to implement your function, which is basically a wrong idea. The connection of powershell in the code, and the subsequent addition of runnable scripts, are executed in the connected directory. – Jason Pan May 28 '20 at 05:07
  • @John To determine the specific location of a file, you must know the absolute path of the file. The path you give is relative. In the program, the relative path must be stored in the directory where the program runs. So my personal suggestion is that when you generate or save, record the absolute path of your file and read it directly at runtime. – Jason Pan May 28 '20 at 05:11