【问题标题】:Powershell error with value as a parameter [duplicate]将值作为参数的Powershell错误[重复]
【发布时间】:2023-03-15 16:56:01
【问题描述】:
我在 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 : InvalidData: (:) [Import-XrmSolution],参数 BindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,X
rm.Framework.CI.PowerShell.Cmdlets.ImportXrmSolutionCommand
+ PSComputerName : li1appcrmf14
【问题讨论】:
标签:
powershell
powershell-remoting
invoke-command
【解决方案1】:
问题是您试图将变量从“外部”运行脚本传递到独立的“内部”脚本块中。也就是说,您应该将脚本块视为一个完全独立的代码块,应该是完全自包含的。如果你想传递信息或变量,你应该在脚本块中使用参数来做到这一点(见@dee-see post)。唯一的替代方法(PowerShell v3+)是使用$using:范围变量(PowerShell: Passing variables to remote commands)
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
【解决方案2】:
$CRMConn 变量在您的脚本块中不可见。您必须使用Invoke-Command 的ArgumentList 参数将变量传递给您的脚本块。
Invoke-Command -Computername $hostnameSolution `
-ScriptBlock {param($conn, $fDrive, $solutionName) Import-XrmSolution -SolutionFilePath "${fDrive}:\DEPLOYMENT\TCRM\CrmSolution\${solutionName}" -ConnectionString $conn -PublishWorkflows $true -OverwriteUnmanagedCustomizations $true -SkipProductUpdateDependencies $true -WaitForCompletion $true -Timeout 7200 -verbose:$true} `
-Credential $cred `
-ArgumentList $CRMConn, $fDrive, $solutionName