0

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

mklement0
  • 382,024
  • 64
  • 607
  • 775
Raul Mercado
  • 324
  • 1
  • 4
  • 14

2 Answers2

1

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
HAL9256
  • 12,384
  • 1
  • 34
  • 46
0

The $CRMConn variable isn't visible inside your script block. You have to use the ArgumentList parameter of Invoke-Command to pass the variables to your script block.

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
dee-see
  • 23,668
  • 5
  • 58
  • 91