在Azure-devops中,尝试使用PowerShell New-PSDrive创建驱动器,没有返回错误,但未创建映射

问题描述 投票:0回答:1

我正在尝试创建一个非常简单的 yaml 脚本,它将驱动器映射到远程计算机(Azure 可以访问),在目标计算机上创建一个文件夹并将文件复制到该文件夹。

我想使用 PowerShell 来执行此操作,因为复制文件后我想再运行几行代码+作业。

...
stages:
- stage: CommonStage
  jobs:
  - job: CommonJob
    pool: 
      vmImage: 'windows-latest'
    steps:
    - checkout: none
    - task: PowerShell@2
      displayName: 'Create folder'
      inputs:
        targetType: 'inline'
        script: | 
          $mappedName = "z"
          $machine = "$(MachineIPAddress)"

          # Map the drive
          New-PSDrive -Verbose -Name $mappedName -PSProvider "FileSystem" -Root "\\$machine\c$\"   

          # Set the location to the drive we _just_ mapped.
          z:
...

从输出中我可以看出该文件夹已映射,但是当我尝试为其设置位置时,我立即收到错误。

Name           Used (GB)     Free (GB) Provider      Root                                               CurrentLocation
----           ---------     --------- --------      ----                                               ---------------
z                                      FileSystem    \\aa.bb.cc.dd\c$\                                             

Set-Location : Cannot find path 'z:\' because it does not exist.
At line:1 char:1
+ Set-Location $MyInvocation.MyCommand.Name
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (z:\:String) [Set-Location], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand

为什么找不到我刚刚映射的文件夹? 如果映射不起作用(或没有起作用),为什么我没有收到错误?

如果我包含

Get-PSDrive
,我可以看到我的驱动器已映射,但我无法用它做任何事情。

...
z                                      FileSystem    \\aa.bb.cc.dd\c$\  

最后,如何映射驱动器并在我的管道中使用它?

powershell yaml azure-pipelines pipeline
1个回答
0
投票

您可以尝试按照以下步骤检查来解决问题:

  1. 登录到代理计算机,在该计算机上手动尝试相同的 PowerShell 脚本。确保脚本可以按预期工作。

  2. 在代理计算机上设置新的自托管代理。使用您登录机器的

    username
    password
    作为新代理的身份验证

  3. 使用新代理运行管道作业。

© www.soinside.com 2019 - 2024. All rights reserved.