为什么我的Azure PowerShell任务脚本可以使用find-module而不是import-module来找到模块​​?

时间:2019-06-05 17:23:23

标签: azure powershell tfs

我一直难以通过Azure PowerShell任务在PowerShell脚本中导入社区模块PoshRSJob。它一直给我这个错误:

  

未加载指定的模块'PoshRSJob',因为在任何模块目录中均未找到有效的模块文件。

我试图解决此问题的方法是安装另一个名为InstallModuleFromGitHub的模块,我曾经使用命令PoshRSJob直接安装Find-Module PoshRSJob | Install-ModuleFromGitHub。我仅使用InstallModuleFromGitHubPoshRSJob模块(以及其他所有注释掉的模块)安装了脚本,然后成功。脚本的其余部分将调用调用Import-Module PoshRSJob的函数,因此,如果模块安装正确,则其他所有功能均应正常工作。好吧,尽管据说模块已成功安装,但构建仍失败,并出现了与以前相同的错误。当我调用Find-Module -Name PoshRSJob时,构建成功找到它。但是,当我致电Import-Module -Name PoshRSJob时,构建失败并再次给我错误。

这些是失败的构建中的控制台日志:

******************************************************************************
Import-Module -Name C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ResourceManager\AzureResourceManager\AzureRM.Profile\AzureRM.Profile.psd1 -Global
Add-AzureRMAccount -ServicePrincipal -Tenant a9691d3f-49e3-46a8-8b23-ddad274d0523 -Credential System.Management.Automation.PSCredential
Select-AzureRMSubscription -SubscriptionId 4242fd68-ea68-4c3c-869a-43269a2b8271 -TenantId a9691d3f-49e3-46a8-8b23-ddad274d0523
& '\\neenah-san1\KTS-Dev\temp\Noah\LoadTestingTests\StartVM.ps1'
Version    Name                                Repository           Description
-------    ----                                ----------           -----------
1.7.4.4    PoshRSJob                           PSGallery            Module d...
The specified module 'PoshRSJob' was not loaded because no valid module file was found in any module directory.
******************************************************************************
Finishing task: AzurePowerShell
******************************************************************************
System.Exception: Task AzurePowerShell failed. This caused the job to fail. Look at the logs for the task for more details.
at Microsoft.TeamFoundation.DistributedTask.Worker.JobRunner.Run(IJobContext jobContext, IJobRequest job, IJobExtension jobExtension, CancellationTokenSource tokenSource)
Worker Worker-9aa3b65a-1416-4840-a9fa-44dce695db94 finished running job 9aa3b65a-1416-4840-a9fa-44dce695db94
******************************************************************************
Finishing Build

这是StartVM.ps1中引起这些日志的相关代码:

# import modules
Install-Module -Name InstallModuleFromGitHub -RequiredVersion 0.3 -Force -Scope CurrentUser
Find-Module PoshRSJob | Install-ModuleFromGitHub -Verbose
Import-Module '\\neenah-san1\TSbuild\Deployment\Tools\PowerShell\Azure\JJK.TS.Azure.psm1' -Force -Prefix 'TS' -Verbose 

Find-Module -Name PoshRSJob
Import-Module -Name PoshRSJob

\JJK.TS.Azure.psm1\中从StartVM调用的函数,该函数具有命令Import-Module PoshRSJob

function Start-AzureVM {
    <#
    .synopsis
    Starts one or more VMs using background threads
    .example
    Get-AzureRmVM -ResourceGroupName 'TS-LoadTest-TST' | Start-AzureVM
    #>
    [CmdletBinding(SupportsShouldProcess = $true)]

    param (
      [Alias("Name")]
      [Parameter( Mandatory=$true,
                  ValueFromPipeline=$false,
                  ValueFromPipelineByPropertyName=$true)]
                    [string]$VMName,
      [Parameter( Mandatory=$true,
                  ValueFromPipeline=$false,
                  ValueFromPipelineByPropertyName=$true)]
                    [string]$ResourceGroupName,
      [Parameter( Mandatory=$false,
                  ValueFromPipeline=$false,
                  ValueFromPipelineByPropertyName=$false)]
                    [string]$SubscriptionName,
      [Parameter( Mandatory=$false,
                  ValueFromPipeline=$false,
                  ValueFromPipelineByPropertyName=$false)]
                    [PSCredential]$Credential
    )

    begin {

      $ErrorActionPreference = "Stop"

      # import community module to handle powershell runspaces
      Import-Module 'PoshRSJob'

代理如何同时查找和找不到模块?
 为什么它不能首先找到PoshRSJob

[编辑]我尝试使用Get-Module -ListAvailable -Name PoshRSJob | Import-Module将其通过管道传输到Import-Module,但我真的不知道如何解释结果。看来任务成功导入了PoshRSJob,然后完全忽略了它,说它找不到模块,并失败了。我认为存储库PoshRSJob与代理正在查找的存储库之间必须断开连接。如果是这种情况,我将如何补救? (为简洁起见,我只包括了由这两个命令Get-Module -ListAvailable -Name PoshRSJob | Import-Module -Force -VerboseGet-AzureRmVM -ResourceGroupName 'TS-LoadTest-TST' | Where-Object {$_.Name -match 'vstc'} | Start-TSAzureVM创建的日志)

2019-06-05T20:26:23.7173881Z ##[debug]Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\PoshRSJob\PoshRSJob\Scripts\TabExpansion.ps1'.
2019-06-05T20:26:23.7223881Z ##[debug]Dot-sourcing the script file 'C:\Program Files\WindowsPowerShell\Modules\PoshRSJob\PoshRSJob\Scripts\TabExpansion.ps1'.
2019-06-05T20:26:23.7463881Z ##[debug]Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\PoshRSJob\PoshRSJob\PoshRSJob.psm1'.
2019-06-05T20:26:24.1043881Z ##[debug]Importing function 'Get-RSJob'.
2019-06-05T20:26:24.1063881Z ##[debug]Importing function 'Receive-RSJob'.
2019-06-05T20:26:24.1083881Z ##[debug]Importing function 'Remove-RSJob'.
2019-06-05T20:26:24.1093881Z ##[debug]Importing function 'Start-RSJob'.
2019-06-05T20:26:24.1113881Z ##[debug]Importing function 'Stop-RSJob'.
2019-06-05T20:26:24.1133881Z ##[debug]Importing function 'Wait-RSJob'.
2019-06-05T20:26:24.1153881Z ##[debug]Importing alias 'gsj'.
2019-06-05T20:26:24.1173881Z ##[debug]Importing alias 'rmsj'.
2019-06-05T20:26:24.1193881Z ##[debug]Importing alias 'rsj'.
2019-06-05T20:26:24.1213881Z ##[debug]Importing alias 'spsj'.
2019-06-05T20:26:24.1233881Z ##[debug]Importing alias 'ssj'.
2019-06-05T20:26:24.1243881Z ##[debug]Importing alias 'wsj'.
2019-06-05T20:26:24.2903881Z ##[error]The specified module 'PoshRSJob' was not loaded because no valid module file was found in any module directory.
2019-06-05T20:26:28.9823881Z Finishing task: AzurePowerShell
2019-06-05T20:26:28.9873881Z ##[error]System.Exception: Task AzurePowerShell failed. This caused the job to fail. Look at the logs for the task for more details.
2019-06-05T20:26:28.9873881Z ##[error]   at Microsoft.TeamFoundation.DistributedTask.Worker.JobRunner.Run(IJobContext jobContext, IJobRequest job, IJobExtension jobExtension, CancellationTokenSource tokenSource)

1 个答案:

答案 0 :(得分:0)

我解决了! Install-ModuleFromGitHub最终在代理无法访问的存储库中安装了PoshRSJob。我通过Uninstall-Module -Name PoshRSJob -AllVersions -Force卸载了该模块。然后,我尝试仅使用普通的Install-Module命令。这以前没用过,这是我尝试使用Install-ModuleFromGitHub的部分原因,但由于某种原因现在可以使用。因此,无论如何,在调用Install-Module -Name PoshRSJob -Force之后,我使用Import-Module -Name PoshRSJob导入了它。 我通过tfs构建进行了测试,它不仅能够找到PoshRSJob,而且还可以在其他东西坏掉之前执行很多代码!但这与这个特定问题无关,因此我将其保留。