에서 파워 쉘 스크립트 파일 (example.ps1)을 호출하려면 \ localwindows .ps1 '은 cmdlet, 함수, 스크립트 파일 또는 작동 가능 프로그램의 이름으로 인식되지 않습니다.내가 다음 코드를 사용하여 C에서 # 스크립트 localwindows.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();
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'.이 존재하지 않기 때문에"로컬 컴퓨터의 powershell에서 'Invoke-Command -ComputerName "컴퓨터 이름"-filepath C : \ localwindows.ps1 "명령을 사용하면 원격 컴퓨터에 새 계정이 만들어졌습니다.
C#에서 localwindows.ps1 스크립트를 호출하는 방법은 무엇입니까? 'Invoke-Command -ComputerName "machineName"-filepath C : \ localwindows.ps1'명령을 C#을 통해 실행하는 방법?
스크립트 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.Management.Automation.ScriptBlock"유형으로 "System.String"유형의 "{C : \ localwindows.ps1}"값을 변환 할 수 없습니다. ' –
업데이트 된 답변보기문제의 핵심은 스크립트가 로컬 컴퓨터의 'c : \ localwindows.ps1'에 없다는 것입니다. –