我試着用下面的代碼運行在C#腳本localwindows.ps1 PowerShell腳本文件(example.ps1):要調用從C#
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()
我試過管道pipeline.Commands.AddScript(System.IO.File.ReadAllText(file));.我在表達式或語句中出現錯誤:'意外的標記' WinNT://machineName ''。 而相同的腳本使用invoke-command創建本地用戶帳戶。我嘗試瞭解決方案 - 我試過解決方案 - pipeline.Commands.AddScript(System.IO.File.ReadAllText(「。」+ file)); 該程序無法找到該文件。 –
我試過 - new1.AddParameter(「Scriptblock」,「{。」+ file +「}」); 我收到錯誤 - '無法綁定參數'ScriptBlock'。無法將類型「System.String」的「{C:\ localwindows.ps1}」值轉換爲鍵入「System.Management.Automation.ScriptBlock」。 –
查看已更新的答案。我相信問題的癥結在於腳本不在本地計算機的'c:\ localwindows.ps1'上。 –