2016-09-14 2 views
0

Get-ChildItemコマンドレットを使用してPowerShellスクリプトを使用して、リモートフォルダからファイルの一覧を取得しています。 すべての私は、サーバー スクリプトでスクリプトを実行していたときには良い作業:PowerShellスクリプトをC言語で実行しているときにGet-ChildItemが動作しない

$serverip = someremoteip 
$mainbackuplocation= someremotelocation 
$folder = someremotefolder 

$remotefolder = Get-ChildItem "\\$serverip\\d$\\$mainbackuplocation\\$folder\\wwwroot" | 
       Out-File d:\log.txt 

しかし、私はC#の通過スクリプトを実行しようとしていた場合、すべてのファイルへの出力はありません。

SecureString securepassword = String2SecureString(password); 
PSCredential credential = new PSCredential(userName, securepassword); 
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(target,shell,credential); 
Runspace remote = RunspaceFactory.CreateRunspace(connectionInfo); 
remote.Open(); 
PowerShell PowerShellInstance = PowerShell.Create(); 
PowerShellInstance.Runspace = remote; 
PowerShellInstance.AddScript("D:\\backup\\test.ps1"); 
var result = PowerShellInstance.Invoke(); 

ログファイルがサーバー上に作成されているため、PowerShellスクリプトは実行されていますが、dirコマンドは何もしません。

私が間違っていることを知りたいですか?

+0

おそらく悪名高い[第二ホップ問題](https://blogs.technet.microsoft.com/heyscriptingguy/2012/11/14/enable-powershell-second-hop-functionality -with-credssp /)、あなたはC#コードからスクリプトをリモートで実行しているからですか? –

+0

は、スクリプトがサーバー上で実行されているので、私はちょうどC# –

+0

から 'target'を起動していないので、別のホストではないと思いますか? 'userName'は' $ serverip'に対して管理者権限を持っていますか? –

答えて

0

上記のコードを正確に使用している場合は、小さなバッグがあります。 Get-ChildItemの後にスペースがなく、このバグのためにlog.txtファイルが作成されません。

試してみてください。

$remotefolder = Get-ChildItem "\\$serverip\\d$\\$mainbackuplocation\\$folder\\wwwroot" | Out-File d:\log.txt 
+0

ありがとうが、これは問題が正しいコマンドで更新されました (スクリプトはサーバー上で正常に動作しています) 私はそれを実行する時間と何かを考えるコマンドは..それは大きなリモートフォルダとそのログにすべてのファイルを書き込むために時間がかかる –

関連する問題