Для вызова файла сценария powershell (example.ps1) из 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();
                    String file = "C:\\localwindows.ps1";
                    Pipeline pipeline = runspace.CreatePipeline();
                    pipeline.Commands.AddScript(file);
                    pipeline.Commands.Add("Out-String");
                    Collection results = pipeline.Invoke();
                }

Но получаю исключение:Семестр '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();

    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 results = powershell.Invoke();

     }

Я получаю ошибку: "Не могу найти путьC: \ localwindows.ps1' потому что его не существует ".

Но используя команду 'Invoke-Command -ComputerName "имя_компьютера» -pilepath C: \ localwindows.ps1 ' , из powershell на локальной машине создал новую учетную запись на удаленной машине.

Как вызвать скрипт localwindows.ps1 из C #? Как выполнить команду?Invoke-Command -ComputerName "имя_компьютера» -pilepath C: \ localwindows.ps1 ' через C #?

Сценарий localwindows.ps1 является

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

Ответы на вопрос(2)

Ваш ответ на вопрос