从C#调用Powershell脚本文件(example.ps1) [英] To call a powershell script file (example.ps1) from C#

查看:75
本文介绍了从C#调用Powershell脚本文件(example.ps1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用以下代码从C#运行脚本localwindows.ps1:

I tried running a script localwindows.ps1 from C# using the following Code :

               PSCredential credential = new PSCredential(userName, securePassword);
                WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, "machineName", 5985, "/wsman", shellUri, credential);
                using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
                {

                    runspace.Open();
                    String file = "C:\\localwindows.ps1";
                    Pipeline pipeline = runspace.CreatePipeline();
                    pipeline.Commands.AddScript(file);
                    pipeline.Commands.Add("Out-String");
                    Collection<PSObject> results = pipeline.Invoke();
                }

但遇到例外情况:'术语'C:\localwindows.ps1'不被识别为cmdlet,函数,脚本文件或可运行程序的名称。

But getting exception :'The term 'C:\localwindows.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program.

所以我尝试了以下操作:

So I tried the following :

PSCredential credential = new PSCredential(userName, securePassword);
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, "machineName", 5985, "/wsman", shellUri, credential);
using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
{

    runspace.Open();

    using (PowerShell powershell = PowerShell.Create())
    {
        powershell.Runspace = runspace;           

        PSCommand new1 = new PSCommand();
        String machinename = "machinename";
        String file = "C:\\localwindows.ps1";
        new1.AddCommand("Invoke-Command");
        new1.AddParameter("computername", machinename);
        new1.AddParameter("filepath", file);         


        powershell.Commands = new1;
        Console.WriteLine(powershell.Commands.ToString());
        Collection<PSObject> results = powershell.Invoke();

     }

我遇到了错误:找不到路径'C :\localwindows.ps1',因为它不存在。

I am getting the error : "Cannot find path 'C:\localwindows.ps1' because it does not exist."

但是使用命令'Invoke-Command -ComputerName machineName -filepath C:\localwindows.ps1 ',从本地计算机中的powershell在远程计算机中创建了一个新帐户。

But using command 'Invoke-Command -ComputerName "machineName" -filepath C:\localwindows.ps1' ,from powershell in local machine created a new account in the remote machine.

如何从C#调用脚本localwindows.ps1?
如何通过C#执行命令'Invoke-Command -ComputerName machineName -filepath C:\localwindows.ps1'?

How to call the script localwindows.ps1 from C#? How to execute the command 'Invoke-Command -ComputerName "machineName" -filepath C:\localwindows.ps1' through C#?

脚本localwindows。 ps1是

The script localwindows.ps1 is

$comp = [adsi]"WinNT://machinename,computer"
$user = $comp.Create("User", "account3")
$user.SetPassword("change,password.10")
$user.SetInfo()


推荐答案

实际上,您的调用样式应该可以,但是在您的两个示例中,脚本 c :\localwindows.ps1 必须位于本地计算机上,在Invoke-Command的情况下,它将被从本地计算机复制到远程计算机上。

Actually your invocation style should work. But in both of your examples, the script c:\localwindows.ps1 must reside on the local computer. In the Invoke-Command case, it will be copied from the local computer to the remote computer.

如果在Invoke-Command情况下,脚本已存在于远程计算机上,而您无需复制它,则删除 FilePath 参数并添加以下内容:

If, in the Invoke-Command case, the script already exists on the remote computer and you don't need to copy it over, remove the FilePath parameter and add this:

new1.AddParameter("Scriptblock", ScriptBlock.Create(file));

这篇关于从C#调用Powershell脚本文件(example.ps1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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