【问题标题】:Mapping a drive with powershell in a scheduled task via Start-Job in c#通过 C# 中的 Start-Job 在计划任务中使用 powershell 映射驱动器
【发布时间】:2013-04-13 14:28:22
【问题描述】:

我目前正在尝试在我的 Web 服务上运行一个 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

当我检索作业的调试输出时,输出如下所示。所以似乎 New-PSDrive 调用似乎以某种方式在这里阻塞:

0
1

知道这是什么原因造成的,我该如何解决?

提前感谢任何提示

【问题讨论】:

  • 如果您针对本地系统上的目录进行测试,而不指定凭据,它是否有效?
  • 感谢您的提示,我刚刚检查了一下,使用没有凭据的本地文件夹,它工作正常,工作完成得很快。那意味着什么?我的意思是原始脚本已正确执行但未完成,因此凭据不会出​​错。
  • 您是否有可能通过使用该后台作业引入“第二跳”身份验证方案,并且它会干扰该驱动器映射?
  • 我担心我不知道您所说的第二跳身份验证是什么意思。据我所知,共享是通过域帐户访问的,其他用户也使用它。不确定这是否能回答您的问题。
  • 如果您只是在新的运行空间中运行脚本,而不是使用运行空间创建后台作业,它的工作方式会有所不同吗?

标签: c# powershell drive


【解决方案1】:

New-PSDrive 有问题。首先检查您的 PSCredential 对象。 然后用

更新脚本

New-PSDrive -Name J -PSProvider 文件系统 -Root $sharepath -Credential $cred -坚持

完美的文章: https://technet.microsoft.com/en-us/library/hh849829.aspx

【讨论】:

    猜你喜欢
    • 2013-12-22
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-02
    相关资源
    最近更新 更多