1

Going to give an example to make it clearer what I want to do

$AzLogin = @{

 Subscription = [string] 'SubscriptionID';
 Tenant = [string] 'tenantID';
 Credential = [System.Management.Automation.PSCredential] $credsServicePrincipal;
 ServicePrincipal = $true;

}

try{
 Connect-Azaccount @$AzLogin -errorAction Stop
}catch{
 Write-Host "Error: $($_.exception)" -foregroundcolor red
}

This works correctly.

The one I want to do is pass splatted arguments stored in property 'CommonArgs' of object 'CSObject', something like this:

$CSObject =@ {
 [PScustomObject]@{CommonArgs=$AzLogin;}
}

try{
 Connect-Azaccount @CSObject.commonArgs -errorAction Stop
}catch{
 Write-Host "Error: $($_.exception)" -foregroundcolor red
}

1 Answer 1

4
  • You can only splat a variable as a whole, not an expression that returns a property value - as of PowerShell 7.1

    • However, there's an approved RFC that would allow for expression-based splatting; there is no specific time frame for its implementation, though; community members are welcome to contribute.
  • A variable used for splatting may only contain a hashtable (containing parameter-name-and-argument pairs, as in your question) or an array (containing positional arguments), not a [pscustomobject] - see about_Splatting.

Something like the following should work:

# Note: It is $CSObject as a whole that is a [pscustomobject] instance,
#       whereas the value of its .CommonArgs property is assumed to be
#       a *hashtable* (the one to use for splatting).
$CSObject = [pscustomobject] @{
  CommonArgs = $AzLogin  # assumes that $AzLogin is a *hashtable*
}

# Need a separate variable containing just the hashtable
# in order to be able to use it for splatting.
$varForSplatting = $CSObject.CommonArgs

Connect-Azaccount @varForSplatting -errorAction Stop

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.