2016-09-14 65 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运行该命令..它是一个大的远程文件夹,并花费一些时间将所有文件写入日志 –