2016-09-14 43 views
0

我有一個PowerShell腳本,它使用Get-ChildItem cmdlet從遠程文件夾(該命令花費很長時間,因爲它是一個大文件夾)獲取文件列表。 所有的工作很好,當我運行該腳本的服務器 劇本上:從C#運行PowerShell腳本時,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之後沒有空格 - 並且由於此bug - log.txt文件未創建。

嘗試:

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

謝謝,但這不是問題已更新與正確的命令 (該腳本在服務器上運行正常) 我認爲它的東西與時間itke運行該命令..它是一個大的遠程文件夾,並花費一些時間將所有文件寫入日誌 –

相關問題