2013-11-25 10 views
7

からPowerShellスクリプトファイル(example.ps1)を呼び出すには\ localwindows .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(); 

    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 "machineName" -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() 

答えて

3

が実際にあなたの呼び出しスタイルが動作するはずです。しかし、どちらの例でも、スクリプトc:\localwindows.ps1はローカルコンピュータに存在する必要があります。 Invoke-Commandの場合、ローカルコンピュータからリモートコンピュータにコピーされます。

Invoke-Commandコマンドの場合には、スクリプトがすでにリモートコンピュータ上に存在し、あなたは、それをコピーFilePathパラメータを削除し、これを追加する必要はありません、もし:

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

私はケースpipeline.Commands.AddScript(System.IO.File.ReadAllText(file));を試しました。エラーが発生しました: '予期しないトークン' WinNT://machineName 'in式またはステートメント。' 同じスクリプトは、invoke-commandを使用してローカルユーザーアカウントを作成します。 私は解決策を試しました - pipeline.Commands.AddScript(System.IO.File.ReadAllText( "。" + file)); プログラムはファイルを見つけることができませんでした。 –

+0

私は試しました - new1.AddParameter( "Scriptblock"、 "{。" + file + "}"); エラーが発生しました - 'パラメータ' ScriptBlock 'をバインドできません。タイプ "System.String"の "{C:¥localwindows.ps1}"値を "System.Management.Automation.ScriptBlock"に変換できません。 –

+0

更新された回答を参照してください。あなたの問題の要点は、スクリプトがローカルコンピュータの 'c:\ localwindows.ps1'にないということです。 –

0

私がしましたhttp://getthinktank.com/2015/06/22/naos-winrm-windows-remote-management-through-net/で.NETからWinRMを介してPowershellを実行する簡単な方法を説明した記事があります。

コードはコピーするだけで1つのファイルになり、System.Management.Automationへの参照を含むNuGetパッケージにもなります。

信頼できるホストを自動管理し、スクリプトブロックを実行し、ファイルを送信することもできます(実際にはサポートされていませんが、回避策を作成しました)。返品は常に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:\Temp\BigFileLocal.nupkg"; 
var fileBytes = File.ReadAllBytes(localFilePath); 
var remoteFilePath = @"D:\Temp\BigFileRemote.nupkg"; 
machineManager.SendFile(remoteFilePath, fileBytes); 

これが役立つ場合は、回答としてマークしてください。私は私の自動化された展開でこれをしばらく使用してきました。問題が見つかった場合は、コメントを残してください。

関連する問題