Get in touch with us!

Where() method doesn’t work in Azure automation classes

At this moment Azure Automation is using Powershell version 5.0.10514.2 and for me this was a bit problematic. Because I was using a newer version, I know I have only myself to blame for all the testing and headache but here is a fun fact.

.where() methods doesn’t seems to work in classes in the current ps version used in Azure Automation.

This is my test script
#Create custom object
[object[]]$list = $null
$list += New-Object -TypeName psobject -Property @{Name="t1";ID=([guid]::NewGuid()).tostring()}
$list += New-Object -TypeName psobject -Property @{Name="t2";ID=([guid]::NewGuid()).tostring()}
$list += New-Object -TypeName psobject -Property @{Name="t3";ID=([guid]::NewGuid()).tostring()}

#Diplay object
Write-Output $list | select Name,ID 

#Create test class
class test {
    [string]$name
    [string]$id
    test ($name) {
        $this.name = $name
    }
    [test] UpdateID ($obj) {
        $this.id = $obj.where({$psitem.name -eq $this.name}).ID
        return $this
    }
    [test] UpdateIDidx ($obj) {
        $index=$obj.name.indexof($this.name)
        $this.id = $obj[$index].ID
        return $this
    }
}
Write-Output "Test Methods"
Write-Output ([test]::new("t2").UpdateID($list)) | select Name,ID #Test Where Method

Write-Output ([test]::new("t2").UpdateIDidx($list))  | select Name,ID  #Test indexof Method

Write-Output "PS Version"
Write-Output $PSVersionTable["PSVersion"].ToString()
And this is the result
On my computer

In Azure Automation

 

Do you know why or am I doing it wrong? Feel free to comment.

Submit a Comment

Your email address will not be published. Required fields are marked *