1 | initial version |
To transmit an object as a parameter for a function in PowerShell used for GUI, you can follow the steps below:
function MyFunction {
param(
[Parameter(Position=0,Mandatory=$true)]
[Object]$myObject
)
# Function code here
}
$myObject = New-Object -TypeName PSObject -Property @{
Name = 'John'
Age = 30
Location = 'New York'
}
MyFunction -myObject $myObject
The function can then access the properties of the object using dot notation, for example:
function MyFunction {
param(
[Parameter(Position=0,Mandatory=$true)]
[Object]$myObject
)
Write-Host "Name: $($myObject.Name)"
Write-Host "Age: $($myObject.Age)"
Write-Host "Location: $($myObject.Location)"
}