如何使用C#运行空间从本地计算机读取Azure VM中存在的远程Yaml文件 [英] How to read remote yaml file present in Azure VM from local machine using C# Runspace

查看:65
本文介绍了如何使用C#运行空间从本地计算机读取Azure VM中存在的远程Yaml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 C#Runspace 从本地计算机读取远程 azure vm 中存在的远程 yaml 文件.

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

我能够使用 C#Runspace 远程运行 Powershell 命令,但是我不知道如何读取远程存在的 yaml 文件使用 Runspace Azure VM 这样的系统.

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.

以下代码用于从我的本地系统远程运行 powershell 命令.

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");    
                     }    
                }

推荐答案

通过测试,我可以读取服务器上的.yml文件.下面是代码(.net框架4.7.2)和屏幕截图.

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 文件.

在Powershell中通过Powershell读取文件.

read file by powershell in vm server.

通过我的代码读取文件.

read file by my code.

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();
        }
    }
}

这篇关于如何使用C#运行空间从本地计算机读取Azure VM中存在的远程Yaml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆