首页 > 解决方案 > 以值作为参数的 Powershell 错误

问题描述

我在 Powershell 中有以下脚本:

#more code here

$CRMConn = "AuthType=AD;Url=http://${ipSolution}/${organizationName}; Domain=${domain}; Username=${username}; Password=${password}; OrganizationName=${organizationName}"

echo $CRMConn

Invoke-Command -Computername $hostnameSolution -ScriptBlock {Import-XrmSolution -SolutionFilePath "${fDrive}:\DEPLOYMENT\TCRM\CrmSolution\${solutionName}" -ConnectionString $CRMConn -PublishWorkflows $true -OverwriteUnmanagedCustomizations $true -SkipProductUpdateDependencies $true -WaitForCompletion $true -Timeout 7200 -verbose:$true} -Credential $cred

当我执行它时,我收到以下错误(敏感信息已被修改):

AuthType=AD;Url= http://192.168.10.53/ORGNAME ; 域=域;用户名=用户名;密码=密码123@;组织名称=组织名称

无法将参数绑定到参数“ConnectionString”,因为它为空。+ CategoryInfo:无效数据:(:) [Import-XrmSolution],参数 BindingValidationException + FullyQualifiedErrorId:ParameterArgumentValidationErrorNullNotAllowed,X
rm.Framework.CI.PowerShell.Cmdlets.ImportXrmSolutionCommand + PSComputerName:li1appcrmf14

标签: powershellpowershell-remotinginvoke-command

解决方案


问题是您试图将变量从“外部”运行脚本传递到独立的“内部”脚本块中。也就是说,您应该将脚本块视为完全独立的代码块,应该完全自包含。如果你想传递信息或变量,你应该在脚本块中使用参数来做到这一点(见@dee-see post)。唯一的替代方法(PowerShell v3+)是使用$using:范围变量(PowerShell:将变量传递给远程命令

Invoke-Command -Computername $hostnameSolution -ScriptBlock {Import-XrmSolution -SolutionFilePath "${fDrive}:\DEPLOYMENT\TCRM\CrmSolution\${solutionName}" -ConnectionString $using:CRMConn -PublishWorkflows $true -OverwriteUnmanagedCustomizations $true -SkipProductUpdateDependencies $true -WaitForCompletion $true -Timeout 7200 -verbose:$true} -Credential $cred

推荐阅读