The problem I'm having stems from passing a PSCustomObject
as an argument to the Start-Process
cmdlet (I'm essentially starting a new PowerShell process to run a script asynchronously from the calling script). Although the parameter is defined as type PSCustomObject
, it is accepted as a string for some reason, and therefore it looks like I am required to convert it back to a PSCustomObject
to access any attributes.
Here is the necessary part of my calling script:
# Convert JSON object to PowerShell object
$payload = ConvertFrom-Json $body
Write-Host $payload
## Returns exactly the following PsCustomObject:
## @{os=Windows Server Datacenter 2016; vmName=sbtestvm; diskType=Managed; username=testuser;
## password=Pa55w.rd1234; location=West Europe; size=Standard_D1;
## requestType=0; sender=test}
Write-Host $payload.os
## Returns: Windows Server Datacenter 2016
# Fire up new worker shell asynchronously
Start-Process powershell.exe -ArgumentList '-NoExit', "$PSScriptRoot\ServiceBus-AsyncWorker.ps1", "'$payload'" # -Windowstyle Hidden
And my executed script:
Param(
[Parameter(Mandatory=$True)]
[PSCustomObject]$Request
)
# Import RequestHandler module to deal with processing service bus requests
Import-Module $PSScriptRoot\RequestHandler\RequestHandler.psm1
Write-Host $Request
## Returns exactly the same as 'Write-Host $payload' in the calling script
Write-Host $Request.os
## Returns empty string
Write-Host $Request.GetType()
## Returns System.String <--- This is the issue
Long story short: is there a way to prevent this object being automatically parsed as a string in the first place? If not - how can this string be cast back to the relevant object type?