从C#中调用powershell脚本文件(example.ps1)
我尝试使用以下代码从C#运行脚本localwindows.ps1:
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,函数,脚本文件或可操作程序的名称。
所以我尝试了以下内容:
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',因为它不存在。”
但是使用命令“Invoke-Command -ComputerName”machineName“-filepath C: localwindows.ps1”,从本地机器的powershell在远程机器中创建了一个新帐户。
如何从C#调用脚本localwindows.ps1? 如何通过C#执行命令“Invoke-Command -ComputerName”machineName“-filepath C: localwindows.ps1”?
脚本localwindows.ps1是
$comp = [adsi]“WinNT://machinename,computer”
$user = $comp.Create(“User”, "account3")
$user.SetPassword(“change,password.10")
$user.SetInfo()
其实你的调用风格应该工作。 但在这两个示例中,脚本c:localwindows.ps1
必须驻留在本地计算机上。 在Invoke-Command的情况下,它将从本地计算机复制到远程计算机。
如果在Invoke-Command情况下,脚本已经存在于远程计算机上,并且您不需要复制它,请删除FilePath
参数并添加以下内容:
new1.AddParameter("Scriptblock", ScriptBlock.Create(file));
我有一篇文章描述了通过.NET的WinRM运行Powershell的简单方法,网址为http://getthinktank.com/2015/06/22/naos-winrm-windows-remote-management-through-net/。
如果您想复制代码,则代码位于单个文件中,它也是一个NuGet程序包,其中包含对System.Management.Automation的引用。
它自动管理可信主机,可以运行脚本块,并发送文件(这不是真的支持,但我创建了一个工作)。 返回始终是Powershell的原始对象。
// this is the entrypoint to interact with the system (interfaced for testing).
var machineManager = new MachineManager(
"10.0.0.1",
"Administrator",
MachineManager.ConvertStringToSecureString("xxx"),
true);
// for your specific issue I think this would be easier
var results = machineManager.RunScript(
File.ReadAllText("C:LocalWindows.ps1"));
// will perform a user initiated reboot.
machineManager.Reboot();
// can run random script blocks WITH parameters.
var fileObjects = machineManager.RunScript(
"{ param($path) ls $path }",
new[] { @"C:PathToList" });
// can transfer files to the remote server (over WinRM's protocol!).
var localFilePath = @"D:TempBigFileLocal.nupkg";
var fileBytes = File.ReadAllBytes(localFilePath);
var remoteFilePath = @"D:TempBigFileRemote.nupkg";
machineManager.SendFile(remoteFilePath, fileBytes);
如果有帮助,请标记为答案。 我一直在使用我的自动化部署一段时间。 如果您发现问题,请留下评论。
链接地址: http://www.djcxy.com/p/17375.html上一篇: To call a powershell script file (example.ps1) from C#
下一篇: Appcelerator and CommonJS modules (caching and circular references)