2

I am trying to save some date to a PSCustomObject in one function and pass that object to another function so I can access the objects properties and values. I am doing something incorrect as I can not access anything from the object in the second function. What am I doing wrong. I can access the objects properties and values if i don't pass it to another function. Here is an example of what i am trying to do (this is just the test to see how to do it, my actual code will manipulate the date stored in the object). Thanks for any help or insight you can provide.

Function test {
try{
$dateTimeInput = Read-Host -Prompt "Enter date (format 6/1/2025 3:15PM)"

    $myObject = [PSCustomObject]@{
        date1 = Get-Date $dateTimeInput
        date2 = Get-Date $dateTimeInput -Format "MMddyyyy-HHmm"
    }
} Catch{Write-host "no good"
test

    }
    return $myObject
}



Function test2 {
    param(
    $myObject
    )
    Write-Host "111111" $myObject.date1
    Write-Host "222222" $myObject.date2
    }

test
test2 $myObject
0

3 Answers 3

4

Here, I reformatted your code to be more readable.

The problem here is that you try to invoke something like test2 test, so call test function to produce object and pass it to test2 function.

However this is not how it works in powershell, but you're very close! You need to wrap test in () to actually call the test function, so you need to write test2 (test).

You can read more about Grouping operator ( )

Below is full example (reformatted):

Function test {
    try{
        $dateTimeInput = Read-Host -Prompt "Enter date (format 6/1/2025 3:15PM)"

        $myObject = [PSCustomObject]@{
            date1 = Get-Date $dateTimeInput
            date2 = Get-Date $dateTimeInput -Format "MMddyyyy-HHmm"
        }
    } Catch { Write-host "no good" }

    return $myObject
}

Function test2 {
    param(
        $myObject
    )
    Write-Host "date1" $myObject.date1
    Write-Host "date2" $myObject.date2
}

test2 (test)
Sign up to request clarification or add additional context in comments.

Comments

3

The problem with your script is that you didn't define $myObject in the current scope but only in the scope of the Test function. For more background on scopes see: about scopes. Besides, you didn't assign the output of the Test function to any variable or actually pass it to any other function or cmdlet which will eventually write the output to the display.
There are basically two ways to connect the output from one function to another:

Via a variable:

Syntax:

$myResult = test
test2 $myResult

Note that the variable name might differ from the variable used in the Test function scope. In fact none of the variables used in the Test function is available in the parent (from where you invoke the Test function)

Via the PowerShell Pipeline

Syntax:

test | test2

For details see: about pipelines

For this you will need to change your Test2 function to accept the -myObject parameter from the pipeline ([Parameter(ValueFromPipeLine = $true)], for details see: about Functions Advanced Parameters)

Function test2 {
    param(
        [Parameter(ValueFromPipeLine = $true)]$myObject
    )
    Write-Host "111111" $myObject.date1
    Write-Host "222222" $myObject.date2
}

As an important aside: You call the Test function from the catch block within the Test function itself, this might result in an infinitive loop when an error is caught.

Comments

0

1st - you need to assign the output of test to SOMETHING.

2nd - vars that are defined inside a function normally remain ONLY in that function. so you can't [normally] refer to $MyObject the way you did in test2 $MyObject.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.