简体   繁体   English

从 Gitlab CI/CD 上的 Docker 映像运行 PowerCLI 时出错

[英]Error running PowerCLI from Docker image on Gitlab CI/CD

I have a Gitlab CI/CD setup that pulls the vmware/powerclicore image from Dockerhub and runs a ps1 script.我有一个 Gitlab CI/CD 设置,它从 Dockerhub 中提取 vmware/powerclicore 映像并运行 ps1 脚本。 This script runs correctly on a Windows host, or interactively from the Docker image, but fails if ran through the Gitlab CI/CD with the error:此脚本在 Windows 主机上正确运行,或从 Docker 映像以交互方式运行,但如果通过 Gitlab CI/CD 运行并出现错误,则会失败:

Set-NetworkAdapter : 02/04/2020 20:28:38    Set-NetworkAdapter      Length cannot be less than zero.
 Parameter name: length

The gitlab-ci.yml is: gitlab-ci.yml 是:

veeam-map-rep-networks:
  image:
    name: vmware/powerclicore
  stage: deploy
  script:
    - pwsh powershell/ps_dr_replication_remapping.ps1 -pass $vCenterPassword

ps_dr_replication_remapping.ps1 is: ps_dr_replication_remapping.ps1 是:

 # Define passed in variables
param (
    [Parameter(Mandatory=$True,Position=1)]
    [string]$pass
)
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Scope AllUsers -Confirm:$false
Set-PowerCLIConfiguration -ParticipateInCeip $false -Confirm:$false
$ConfirmPreference = "None"

Connect-VIServer -Server server.example.com -User administrator@example.com -Password $pass -Force
Write-Host (Get-VM -Name "REDACTED-VM-Name")
Write-Host (Get-VM -Name "REDACTED-VM-Name" | Get-NetworkAdapter -Name "Network adapter 1")
Get-VM -Name "REDACTED-VM-Name" | Get-NetworkAdapter -Name "Network adapter 1" | Set-NetworkAdapter -NetworkName "Target Network" 
Write-Host ($error[0].Exception | select *)
Write-Host ($error[0].Exception.InnerException | select *)

Output of script in Gitlab CI/CD Pipeline: Gitlab CI/CD 管道中的脚本输出:

Running with gitlab-runner 12.7.0 (58272c27)
   on REDACTED
Using Docker executor with image vmware/powerclicore ...
00:00
 Pulling docker image vmware/powerclicore ...
 Using docker image sha256:ad74fa86c83bc47805e3db4b40b7baeb847d1c1924f7bb99d9cbdcecd7a55a6a for vmware/powerclicore ...
Running on runner-REDACTED via REDACTED...
00:02
Fetching changes with git depth set to 50...
00:01
 Reinitialized existing Git repository in REDACTED
  * [new ref]         refs/pipelines/114987846 -> refs/pipelines/114987846
    104dcbf..5978962  REDACTED
 Skipping Git submodules setup
$ TZ=":US/Eastern" date
00:07
 Tue Feb  4 20:28:31  2020
 $ pwsh powershell/ps_dr_replication_remapping.ps1 -pass $pass
 WARNING: Please consider joining the VMware Customer Experience Improvement Program, so you can help us make PowerCLI a better product. You can join using the following command:
 Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $true
 VMware's Customer Experience Improvement Program ("CEIP") provides VMware with information that enables VMware to improve its products and services, to fix problems, and to advise you on how best to deploy and use our products.  As part of the CEIP, VMware collects technical information about your organizations use of VMware products and services on a regular basis in association with your organizations VMware license key(s).  This information does not personally identify any individual.
 For more details: type "help about_ceip" to see the related help article.
 To disable this warning and set your preference use the following command and restart PowerShell: 
 Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $true or $false.
 WARNING: The cmdlet "Get-PowerCLIVersion" is deprecated. Please use the 'Get-Module' cmdlet instead.
 VMware PowerCLI 11.5.0 build 14912921
 REDACTED-VM-Name <--This confirms connection to vCenter and VM
 Network adapter 1 <--This confirms I can see the portgroup
 Set-NetworkAdapter : 02/04/2020 20:28:38   Set-NetworkAdapter      Length cannot be less than zero.
 Parameter name: length 
 At REDACTED/powershell/ps_dr_replication_remapping.ps1:32 char:87
 + ... etwork adapter 1" | Set-NetworkAdapter -NetworkName "Target Network"
 +                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 + CategoryInfo          : NotSpecified: (:) [Set-NetworkAdapter], VimException
 + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.SetNetworkAdapter

 @{ErrorId=Core_BaseCmdlet_UnknownError; ErrorCategory=NotSpecified; TargetObject=; RecommendedAction=Error occured while executing cmdlet: Set-NetworkAdapter. Check inner exception for more details.; SessionId=; ConnectionId=; Severity=Error; Message=02/04/2020 20:28:38 Set-NetworkAdapter      Length cannot be less than zero.
 Parameter name: length ; Data=System.Collections.ListDictionaryInternal; InnerException=System.ArgumentOutOfRangeException: Length cannot be less than zero.
 Parameter name: length
    at System.String.Substring(Int32 startIndex, Int32 length)
    at System.Management.Automation.Internal.StringUtil.TruncateToBufferCellWidth(PSHostRawUserInterface rawUI, String toTruncate, Int32 maxWidthInBufferCells) in /usr/src/photon/BUILD/PowerShell-6.2.3/src/System.Management.Automation/utils/StringUtil.cs:line 46
    at Microsoft.PowerShell.ProgressNode.RenderFullDescription(String description, String indent, Int32 maxWidth, PSHostRawUserInterface rawUi, ArrayList strCollection, Boolean isFullPlus) in /usr/src/photon/BUILD/PowerShell-6.2.3/src/Microsoft.PowerShell.ConsoleHost/host/msh/ProgressNode.cs:line 214
    at Microsoft.PowerShell.ProgressNode.LinesRequiredInFullStyleMethod(PSHostRawUserInterface rawUi, Int32 maxWidth, Boolean isFullPlus) in /usr/src/photon/BUILD/PowerShell-6.2.3/src/Microsoft.PowerShell.ConsoleHost/host/msh/ProgressNode.cs:line 441
    at Microsoft.PowerShell.ProgressNode.LinesRequiredMethod(PSHostRawUserInterface rawUi, Int32 maxWidth) in /usr/src/photon/BUILD/PowerShell-6.2.3/src/Microsoft.PowerShell.ConsoleHost/host/msh/ProgressNode.cs:line 398
    at Microsoft.PowerShell.PendingProgress.HeightTallyer.Visit(ProgressNode node, ArrayList unused, Int32 unusedToo) in /usr/src/photon/BUILD/PowerShell-6.2.3/src/Microsoft.PowerShell.ConsoleHost/host/msh/PendingProgress.cs:line 678
    at Microsoft.PowerShell.PendingProgress.NodeVisitor.VisitNodes(ArrayList nodes, NodeVisitor v) in /usr/src/photon/BUILD/PowerShell-6.2.3/src/Microsoft.PowerShell.ConsoleHost/host/msh/PendingProgress.cs:line 1005
    at Microsoft.PowerShell.PendingProgress.TallyHeight(PSHostRawUserInterface rawUi, Int32 maxHeight, Int32 maxWidth) in /usr/src/photon/BUILD/PowerShell-6.2.3/src/Microsoft.PowerShell.ConsoleHost/host/msh/PendingProgress.cs:line 715
    at Microsoft.PowerShell.PendingProgress.Render(Int32 maxWidth, Int32 maxHeight, PSHostRawUserInterface rawUI) in /usr/src/photon/BUILD/PowerShell-6.2.3/src/Microsoft.PowerShell.ConsoleHost/host/msh/PendingProgress.cs:line 581
    at Microsoft.PowerShell.ProgressPane.Show(PendingProgress pendingProgress) in /usr/src/photon/BUILD/PowerShell-6.2.3/src/Microsoft.PowerShell.ConsoleHost/host/msh/ProgressPane.cs:line 182
    at Microsoft.PowerShell.ConsoleHostUserInterface.HandleIncomingProgressRecord(Int64 sourceId, ProgressRecord record) in /usr/src/photon/BUILD/PowerShell-6.2.3/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs:line 96
    at Microsoft.PowerShell.ConsoleHostUserInterface.WriteProgress(Int64 sourceId, ProgressRecord record) in /usr/src/photon/BUILD/PowerShell-6.2.3/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs:line 1219
    at System.Management.Automation.Internal.Host.InternalHostUserInterface.WriteProgress(Int64 sourceId, ProgressRecord record) in /usr/src/photon/BUILD/PowerShell-6.2.3/src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs:line 562
    at System.Management.Automation.MshCommandRuntime.WriteProgress(Int64 sourceId, ProgressRecord progressRecord, Boolean overrideInquire) in /usr/src/photon/BUILD/PowerShell-6.2.3/src/System.Management.Automation/engine/MshCommandRuntime.cs:line 414
    at System.Management.Automation.MshCommandRuntime.WriteProgress(ProgressRecord progressRecord, Boolean overrideInquire) in /usr/src/photon/BUILD/PowerShell-6.2.3/src/System.Management.Automation/engine/MshCommandRuntime.cs:line 355
    at System.Management.Automation.MshCommandRuntime.WriteProgress(ProgressRecord progressRecord) in /usr/src/photon/BUILD/PowerShell-6.2.3/src/System.Management.Automation/engine/MshCommandRuntime.cs:line 325
    at System.Management.Automation.Cmdlet.WriteProgress(ProgressRecord progressRecord) in /usr/src/photon/BUILD/PowerShell-6.2.3/src/System.Management.Automation/engine/cmdlet.cs:line 563
    at VMware.VimAutomation.Sdk.Util10Ps.BaseCmdlet.BaseCmdlet.ProgressCallback(Task task, Object result, Boolean writeResultToScreen)
    at VMware.VimAutomation.Sdk.Util10Ps.BaseCmdlet.BaseCmdlet.EndProcessingErrorHandled()
    at VMware.VimAutomation.ViCore.Util10Ps.BaseCmdlet.BaseCmdlet.EndProcessingErrorHandled(); TargetSite=Void ThrowTerminatingError(System.Management.Automation.ErrorRecord); StackTrace=   at System.Management.Automation.MshCommandRuntime.ThrowTerminatingError(ErrorRecord errorRecord) in /usr/src/photon/BUILD/PowerShell-6.2.3/src/System.Management.Automation/engine/MshCommandRuntime.cs:line 2055; HelpLink=; Source=System.Management.Automation; HResult=-2146232832}
 @{Message=Length cannot be less than zero.
 Parameter name: length; ActualValue=; ParamName=length; Data=System.Collections.ListDictionaryInternal; InnerException=; TargetSite=System.String Substring(Int32, Int32); StackTrace=   at System.String.Substring(Int32 startIndex, Int32 length)
    at System.Management.Automation.Internal.StringUtil.TruncateToBufferCellWidth(PSHostRawUserInterface rawUI, String toTruncate, Int32 maxWidthInBufferCells) in /usr/src/photon/BUILD/PowerShell-6.2.3/src/System.Management.Automation/utils/StringUtil.cs:line 46
    at Microsoft.PowerShell.ProgressNode.RenderFullDescription(String description, String indent, Int32 maxWidth, PSHostRawUserInterface rawUi, ArrayList strCollection, Boolean isFullPlus) in /usr/src/photon/BUILD/PowerShell-6.2.3/src/Microsoft.PowerShell.ConsoleHost/host/msh/ProgressNode.cs:line 214
    at Microsoft.PowerShell.ProgressNode.LinesRequiredInFullStyleMethod(PSHostRawUserInterface rawUi, Int32 maxWidth, Boolean isFullPlus) in /usr/src/photon/BUILD/PowerShell-6.2.3/src/Microsoft.PowerShell.ConsoleHost/host/msh/ProgressNode.cs:line 441
    at Microsoft.PowerShell.ProgressNode.LinesRequiredMethod(PSHostRawUserInterface rawUi, Int32 maxWidth) in /usr/src/photon/BUILD/PowerShell-6.2.3/src/Microsoft.PowerShell.ConsoleHost/host/msh/ProgressNode.cs:line 398
    at Microsoft.PowerShell.PendingProgress.HeightTallyer.Visit(ProgressNode node, ArrayList unused, Int32 unusedToo) in /usr/src/photon/BUILD/PowerShell-6.2.3/src/Microsoft.PowerShell.ConsoleHost/host/msh/PendingProgress.cs:line 678
    at Microsoft.PowerShell.PendingProgress.NodeVisitor.VisitNodes(ArrayList nodes, NodeVisitor v) in /usr/src/photon/BUILD/PowerShell-6.2.3/src/Microsoft.PowerShell.ConsoleHost/host/msh/PendingProgress.cs:line 1005
    at Microsoft.PowerShell.PendingProgress.TallyHeight(PSHostRawUserInterface rawUi, Int32 maxHeight, Int32 maxWidth) in /usr/src/photon/BUILD/PowerShell-6.2.3/src/Microsoft.PowerShell.ConsoleHost/host/msh/PendingProgress.cs:line 715
    at Microsoft.PowerShell.PendingProgress.Render(Int32 maxWidth, Int32 maxHeight, PSHostRawUserInterface rawUI) in /usr/src/photon/BUILD/PowerShell-6.2.3/src/Microsoft.PowerShell.ConsoleHost/host/msh/PendingProgress.cs:line 581
    at Microsoft.PowerShell.ProgressPane.Show(PendingProgress pendingProgress) in /usr/src/photon/BUILD/PowerShell-6.2.3/src/Microsoft.PowerShell.ConsoleHost/host/msh/ProgressPane.cs:line 182
    at Microsoft.PowerShell.ConsoleHostUserInterface.HandleIncomingProgressRecord(Int64 sourceId, ProgressRecord record) in /usr/src/photon/BUILD/PowerShell-6.2.3/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs:line 96
    at Microsoft.PowerShell.ConsoleHostUserInterface.WriteProgress(Int64 sourceId, ProgressRecord record) in /usr/src/photon/BUILD/PowerShell-6.2.3/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs:line 1219
    at System.Management.Automation.Internal.Host.InternalHostUserInterface.WriteProgress(Int64 sourceId, ProgressRecord record) in /usr/src/photon/BUILD/PowerShell-6.2.3/src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs:line 562
    at System.Management.Automation.MshCommandRuntime.WriteProgress(Int64 sourceId, ProgressRecord progressRecord, Boolean overrideInquire) in /usr/src/photon/BUILD/PowerShell-6.2.3/src/System.Management.Automation/engine/MshCommandRuntime.cs:line 414
    at System.Management.Automation.MshCommandRuntime.WriteProgress(ProgressRecord progressRecord, Boolean overrideInquire) in /usr/src/photon/BUILD/PowerShell-6.2.3/src/System.Management.Automation/engine/MshCommandRuntime.cs:line 355
    at System.Management.Automation.MshCommandRuntime.WriteProgress(ProgressRecord progressRecord) in /usr/src/photon/BUILD/PowerShell-6.2.3/src/System.Management.Automation/engine/MshCommandRuntime.cs:line 325
    at System.Management.Automation.Cmdlet.WriteProgress(ProgressRecord progressRecord) in /usr/src/photon/BUILD/PowerShell-6.2.3/src/System.Management.Automation/engine/cmdlet.cs:line 563
    at VMware.VimAutomation.Sdk.Util10Ps.BaseCmdlet.BaseCmdlet.ProgressCallback(Task task, Object result, Boolean writeResultToScreen)
    at VMware.VimAutomation.Sdk.Util10Ps.BaseCmdlet.BaseCmdlet.EndProcessingErrorHandled()
    at VMware.VimAutomation.ViCore.Util10Ps.BaseCmdlet.BaseCmdlet.EndProcessingErrorHandled(); HelpLink=; Source=System.Private.CoreLib; HResult=-2146233086}
Running after script...
00:02
 $ TZ=":US/Eastern" date
 Tue Feb  4 20:28:39  2020
 Job succeeded

Why would Gitlab's runner cause this to fail on the same Docker image I can manually make it run as?为什么 Gitlab 的运行程序会在我可以手动运行的同一个 Docker 映像上导致它失败? The Gitlab output confirms I'm connecting to vCenter and can see the VM's and Portgroups Gitlab 输出确认我正在连接到 vCenter 并且可以看到虚拟机和端口组

Had a similar issue using the powerclicore container.使用 powerclicore 容器也有类似的问题。 I found I was able to workaround the issue by passing the object directly into the set command rather than through the pipeline.我发现我可以通过将对象直接传递到 set 命令而不是通过管道来解决这个问题。

Try this instead:试试这个:

$netadapter = Get-VM -Name "REDACTED-VM-Name" | Get-NetworkAdapter -Name "Network adapter 1" Set-NetworkAdapter -NetworkAdapater $netadapter -NetworkName "Target Network"

I had similar issue.我有类似的问题。 It looked like a bug in the vmware/powerclicore docker image.它看起来像是 vmware/powerclicore docker 映像中的一个错误。

So I used the docker image: mcr.microsoft.com/powershell instead of vmware/powerclicore and ran the following commands to install powercli module to its powershell instance.所以我使用了 docker 镜像:mcr.microsoft.com/powershell 而不是 vmware/powerclicore 并运行以下命令将 powercli 模块安装到它的 powershell 实例。 All other powerCLI commands should follow after them.所有其他 powerCLI 命令都应该跟在它们之后。

Set-PSRepository -Name PSGallery -InstallationPolicy Trusted Set-PSRepository -Name PSGallery -InstallationPolicy Trusted

Install-Module VMware.PowerCLI -Confirm:$false安装模块 VMware.PowerCLI -Confirm:$false

This issue is caused by the PowerShell progress bar on Photon OS based image.此问题是由基于 Photon OS 的图像上的 PowerShell 进度条引起的。 In order to avoid it set为了避免它设置

$ProgressPreference = 'SilentlyContinue' $ProgressPreference = 'SilentlyContinue'

on top of the script.在脚本之上。

We updated the powerclicore image with a fix.我们通过修复更新了 powerclicore 图像。 The progress bar is disabled by default to avoid this exception.默认情况下禁用进度条以避免此异常。

The images with the workaround are vmware/powercli:latest and vmware/powercli:12.2.1解决方法的映像是vmware/powercli:latestvmware/powercli:12.2.1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM