I have a script that is running in PowerShell 7 (for better Unicode support) and I need to run a certain batch of commands in PowerShell 5 due to some DLL incompatibility. This means (afaik) I can't use dot sourcing.
PowerShell 7 calling code: `
$files = powershell.exe -ExecutionPolicy Bypass -File "$PSScriptRoot\FileInfo.ps1" -Path $FolderToProcess -Title $moduleTitle -GUID $guid
FileInfo.ps1
loops through a set of files and returns an array of PSCustomObject
$arr = @()
foreach ($file in $finalFiles) {
$item = [PSCustomObject]@{
i = $file.i
j = $file.j
k = $file.k
}
$arr = $item
}
return $arr
However, what happens when I try to use the $files
variable in the calling script, is that the result of the called script is actually an array of strings and is useless.
How do I either:
A) Tell the calling script that the result of the script should be an array; or
B) Have the 'sending' script not process the result as a string and pass back an array as expected?
CodePudding user response:
You might serialize the object (array) in the callee and deserialize it in the caller.
(As an aside: try to avoid using the increase assignment operator ( =
) to create a collection)
Callee (FileInfo.ps1
)
$arr = foreach ($file in $finalFiles) {
[PSCustomObject]@{
i = $file.i
j = $file.j
k = $file.k
}
}
[System.Management.Automation.PSSerializer]::Serialize($Arr)
Caller (PowerShell 7 calling code)
$PSSerial = powershell.exe -ExecutionPolicy Bypass -File "$PSScriptRoot\FileInfo.ps1" -Path $FolderToProcess -Title $moduleTitle -GUID $guid
$Files = [System.Management.Automation.PSSerializer]::Deserialize($PSSerial)