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

从Gitlab CI/CD上的Docker映像运行PowerCLI时出错,docker,gitlab-ci,powercli,Docker,Gitlab Ci,Powercli,我有一个Gitlab CI/CD设置,它从Dockerhub中提取vmware/powerclicore映像并运行ps1脚本。此脚本在Windows主机上正确运行,或从Docker映像以交互方式运行,但如果通过Gitlab CI/CD运行,则会失败,错误为: Set-NetworkAdapter : 02/04/2020 20:28:38 Set-NetworkAdapter Length cannot be less than zero. Parameter name: le

我有一个Gitlab CI/CD设置,它从Dockerhub中提取vmware/powerclicore映像并运行ps1脚本。此脚本在Windows主机上正确运行,或从Docker映像以交互方式运行,但如果通过Gitlab CI/CD运行,则会失败,错误为:

Set-NetworkAdapter : 02/04/2020 20:28:38    Set-NetworkAdapter      Length cannot be less than zero.
 Parameter name: length
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_remap.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 *)
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
使用gitlab runner 12.7.0(58272c27)运行
论编辑
将Docker executor与映像vmware/powerclicore一起使用。。。
00:00
正在提取docker映像vmware/powerclicore。。。
正在为vmware/powerclicore使用docker映像sha256:AD74FA86C83BC47805E3DB4B40B7BAEB847D1C1924F7BB99D9CBDCED7A55A6A。。。
在跑步者身上跑步通过编辑。。。
00:02
正在获取git深度设置为50的更改。。。
00:01
在修订版中重新初始化现有Git存储库
*[新参考文献]参考文献/管道/114987846->参考文献/管道/114987846
104dcbf..5978962已编辑
正在跳过Git子模块安装程序
$TZ=“:美国/东部”日期
00:07
2020年2月4日星期二20:28:31
$pwsh powershell/ps\u dr\u replication\u remapping.ps1-通过$pass
警告:请考虑加入VMware客户体验改进计划,这样您可以帮助我们使PowerCLI更好的产品。您可以使用以下命令加入:
将PowerCLIConfiguration-Scope用户-participatienceIP$设置为true
VMware的客户体验改善计划(“CEIP”)为VMware提供了信息,使VMware能够改进其产品和服务,解决问题,并就如何最好地部署和使用我们的产品向您提供建议。作为CEIP的一部分,VMware定期收集有关您的组织使用VMware产品和服务的技术信息,并与您的组织VMware许可证密钥相关联。此信息不代表个人身份。
有关更多详细信息:键入“help about_ceip”以查看相关的帮助文章。
要禁用此警告并设置首选项,请使用以下命令并重新启动PowerShell:
将PowerCLIConfiguration-Scope User-participatienceIP设置为$true或$false。
警告:cmdlet“Get-PowerCLIVersion”已弃用。请改用“获取模块”cmdlet。
VMware PowerCLI 11.5.0版本14912921

编辑的VM名称在使用powerclicore容器时也存在类似问题。我发现我可以通过将对象直接传递到set命令而不是通过管道来解决这个问题

请尝试以下方法:

$netadapter=Get-VM-Name“修订的VM名称”| Get-NetworkAdapter-Name“网络适配器1”

设置NetworkAdapter-NetworkAdapater$netadapter-NetworkName“目标网络”

我遇到了类似的问题。它看起来像vmware/powerclicore docker映像中的一个bug

因此,我使用了docker映像:mcr.microsoft.com/powershell而不是vmware/powerclicore,并运行以下命令将powercli模块安装到其powershell实例。所有其他powerCLI命令应紧跟其后

设置PSRepository-名称PSGallery-InstallationPolicy Trusted


安装模块VMware.PowerCLI-确认:$false

此问题是由基于Photon OS的映像上的PowerShell进度条引起的。为了避免它的设置

$ProgressPreference='SilentlyContinue'


在脚本顶部。

我们使用修复程序更新了powerclicore映像。默认情况下禁用进度条以避免此异常

解决方案的映像是vmware/powercli:latestvmware/powercli:12.2.1