我正在尝试在我的web服务上运行powershell脚本,该驱动程序将驱动器映射到另一个系统上的共享文件夹并将文件夹复制到它。我现在有一个奇怪的问题,那就是这个脚本看起来好像执行得很好,但是脚本运行的工作似乎没有完成。因此,我必须设定强制完成任务的超时时间,否则根本无法完成任务。然而,这并不是我想要的,因为如果脚本花费的时间比预期的要长,它会产生一些令人讨厌的副作用。另一方面,我希望在给定场景中尽可能快地执行,所以我想让剧本“自然地”完成。在c#中通过Start-Job在计划任务中映射驱动器与powershell
这是我的当前设置
C#的Web服务调用PowerShell脚本是这样的:
public Collection<PSObject> executeCommand(String pCommand, String pName)
{
// Call the script
var runspaceConfiguration = RunspaceConfiguration.Create();
var runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();
var pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(pCommand);
pipeline.Commands.AddScript("Wait-Job -Name " + pName + " -Timeout 60");
pipeline.Commands.AddScript("Receive-Job -Name " + pName);
return pipeline.Invoke();
}
String shareArguments = "some stuff here";
String shareCommandName = "copyFolder";
String shareCommand = "Start-Job -filepath " + currentDirectory + "\\scripts\\copyFolder.ps1 -ArgumentList " + shareArguments + " -Name " + shareCommandName + " -RunAs32";
Collection<PSObject> results1 = executeCommand(shareCommand, shareCommandName);
StreamWriter sharestream = new StreamWriter("D:\\shareoutput.txt");
foreach (PSObject obj in results1)
{
sharestream.WriteLine(obj.ToString());
}
sharestream.Close();
脚本本身:
param($sharepath,$shareuser,$sharepassword,$hostname,$sourcefolder)
# create a credentials object
$secpasswd = ConvertTo-SecureString $sharepassword -AsPlainText -Force
Write-Output "0"
$cred = New-Object System.Management.Automation.PSCredential ($shareuser, $secpasswd)
Write-Output "1"
# Access the share
New-PSDrive -Name J -PSProvider FileSystem -Root $sharepath -Credential $cred
Write-Output "2"
# Copy the folder including the file
Copy-Item $sourcefolder "J:\" -Recurse -Force
Write-Output "3"
# Unmap drive
Remove-PSDrive -Name J
当我取回的调试输出工作,输出看起来像这样。如此看来,新-PSDrive来调用似乎这里莫名其妙阻止:
0
1
任何想法究竟是什么原因,我该如何解决?
在此先感谢您的任何提示
如果y ou对照本地系统上的目录进行测试,而不指定凭证? – mjolinor
感谢您的提示,我只是检查它,并没有凭据的本地文件夹它工作正常,工作完成得相当快。这意味着什么?我的意思是原始脚本已正确执行但未完成,因此凭据不会错误。 – vm370
您有可能通过使用该后台作业引入“第二跳”身份验证方案,并且会干扰该驱动器映射? – mjolinor