Search code examples
powershellpowershell-remotinginvoke-command

Powershell error with value as a parameter


I have the following script in 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

Whe I execute it, I get the following error (sensitive information has been modified):

AuthType=AD;Url=http://192.168.10.53/ORGNAME; Domain=domain; Username=username; Password=Password123@; OrganizationName=ORGNAME

Cannot bind argument to parameter 'ConnectionString' because it is null. + CategoryInfo : InvalidData: (:) [Import-XrmSolution], Parameter BindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,X
rm.Framework.CI.PowerShell.Cmdlets.ImportXrmSolutionCommand + PSComputerName : li1appcrmf14


Solution

  • The problem is that you are trying to pass a variable from your "outside" running script into an independent "inside" script block. i.e. you should treat Script Blocks as a completely separate independent chunk of code that should be completely self contained. If you want to pass in information or variables, you should use parameters inside the script block to do that (see @dee-see post). The only alternate way (PowerShell v3+) is to use the $using: scope variable (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