Converting String to PsCustomObject

The problem I am running into is passing PsCustomObject

as an argument to the cmdlet Start-Process

(I am essentially starting a new PowerShell process to run the script asynchronously from the calling script). The thing is, although the parameter is defined as a type PsCustomObject

, it is accepted as a string for some reason, so it seems to me that I need to convert it back to in PsCustomObject

order to access any attributes.

Here is the required part of my script call:

# Convert JSON object to PS 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

      

In short: is there a way to prevent this object from being automatically parsed as a string in the first place? If not - how can this string be returned to the appropriate object type?

+3


source to share


1 answer


Start-Process powershell.exe

a new PowerShell process starts. You cannot pass PowerShell objects across process boundaries.

You can change

Start-Process powershell.exe -ArgumentList '-NoExit', "$PSScriptRoot\ServiceBus-AsyncWorker.ps1", "'$payload'"

      

to

$PSScriptRoot\ServiceBus-AsyncWorker.ps1 $payload

      

to avoid creating a new process, but it will run the script in the same window.



If you need to run the script separately from the console, you can run it as a background job :

$job = Start-Job -ScriptBlock {
    Param($Path, $Data)
    & "$Path\ServiceBus-AsyncWorker.ps1" $Data
} -ArgumentList $PSScriptRoot, $payload

      

Otherwise, you need to pass the argument in serialized form (like the original JSON) when a new process is spawned:

Start-Process powershell.exe -ArgumentList '-NoExit', "$PSScriptRoot\ServiceBus-AsyncWorker.ps1", "`"$body`""

      

and then (re) create the object from the string:

Param(
    [Parameter(Mandatory=$true)]
    [string]$Json
)

# Import RequestHandler module to deal with processing service bus requests
Import-Module $PSScriptRoot\RequestHandler\RequestHandler.psm1

$Request = ConvertFrom-Json $Json
...

      

+2


source







All Articles