I have a PowerShell script that needs to execute a second script in a new PowerShell instance, passing in two objects. The problem that happens is that the objects get converted to strings containing the object type in the second script. Here are two sample scripts that illustrate what I'm trying to do. I've tried with Start-Process and with Invoke-Expression. I've also tried splatting the arguments, but that doesn't work at all - nothing is passed.
Script 1:
$hash1 = @{
"key1" = "val1"
"key2" = "val2"
}
$hash2 = @{
"key3" = "val3"
"key4" = "val4"
}
$type1 = $hash1.GetType()
$type2 = $hash2.GetType()
Write-Host "Hash1 type: $type1"
Write-Host "Hash2 type: $type2"
$scriptPath = Split-Path -parent $MyInvocation.MyCommand.Definition
$method = "Start-Process"
Start-Process -FilePath PowerShell "-noExit -command $scriptPath\script2.ps1 -hash1 $hash1 -hash2 $hash2 -method $method"
$method = "Invoke-Expression"
$command = "$scriptPath\script2.ps1 -hash1 $hash1 -hash2 $hash2 -method $method"
Invoke-Expression "cmd /c Start PowerShell -noExit -Command { $command }"
Script 2:
param(
[PSObject]$hash1,
[PSObject]$hash2,
[string]$method
)
$type1 = $hash1.GetType()
$type2 = $hash2.GetType()
Write-Host "Method: $method"
Write-Host "Hash1 type: $type1"
Write-Host "Hash2 type: $type2"
Here's the output from each call:
Method: Start-Process
Hash1 type: string
Hash2 type: string
Method: Invoke-Expression
Hash1 type: string
Hash2 type: string
Here's some background on why I'm trying to do it this way:
I have a script that reads an XML file containing ACL information for multiple directories on a filesystem. I need to set the permissions on each directory, but each one takes time to propagate to all child items. I want to call the second script asynchronously for each directory to reduce runtime. I need each instance of the second script to have its own window so the user can review each one for errors or other messages.
Can anyone help please?