简体   繁体   中英

Return type PSObject not right

I have a situation regarding PSObject. Thing is when I write only the same PSObject in different script it returns what it should.

$returnobject = New-Object -TypeName psobject
$returnobject | Add-Member -MemberType NoteProperty -Name LocalITMail -Value "what"
$returnobject | Add-Member -MemberType NoteProperty -Name LocationSufix -Value "wont you "
$returnobject | Add-Member -MemberType NoteProperty -Name Webproxy -Value "work"
return $returnobject

I get this

在此处输入图像描述

which is good.

But when I call the function from bigger script the return type is not as the form above and I cannot access its properties.

Example:
在此处输入图像描述

What am I doing wrong, is there a way on how to return the PSObject type so I don't get this @{...} output? Thanks.

When working with Class in powershell and calling a function/method the PSObject is not a String. This was my problem here.

@codaamok comment helped me here

Didnt know that powershell mehtod/function allows the return of PSObject

   [PSobject] GetLocation($User, $LocationList) {
    #create return object since we are returning more values. 
    $returnobject = New-Object -TypeName psobject

    foreach ($item in $LocationList) {
        if ($item.zzlocat -eq $user.l -or $item.l -eq $user.l) {
           $returnobject | Add-Member -MemberType NoteProperty -Name LocalITMail -Value $item.Mail 
           $returnobject | Add-Member -MemberType NoteProperty -Name LocationSufix -Value $item.'sAMAccount Abbrevations (5th and 6th characters)' 
           $returnobject | Add-Member -MemberType NoteProperty -Name Webproxy -Value $item.sec_WWGG_WebSecurity_Office 
        }
    }
    return $returnobject
    Write-output "$($this.TimeSTAMP)[Helper]::GetLocation -> Found Location from User" | Out-File $this.Workflow -Append
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM