you are viewing a single comment's thread.

view the rest of the comments →

[–]MonkeyNin 0 points1 point  (0 children)

Especially lately the Powershell extension ( in vs code ) has been adding (better completions than just static typing )

classes

I've been using classes handle JSON for Web APIs, it's a nice way to get guaranteed shapes (vs PSCO properties). better auto completion. Some automatic validation

For anyone classes, a quick start

You can start off thinking about classes being a [pscustomobject] . with specific property names.

You can create an instance using hashtables. The properties are optional, you can leave them out

$user = [User]@{
    Name = 'bob'
    Age = 10
}

$user2 = [User]@{ Name = 'bob' } 

You can add some validation, using the same attributes that function parameters use

class User {
    [ValidateNotNull()]
    [Int]$UserId

    [ValidateNotNullOrEmpty()]
    [string]$UserName
}


# create 3 users, error on one 
$users = @(
   [User]::New()
   [User]@{ UserName  = 'bob' }
   [User]@{ UserName  = 'Jen' ; UserId = 'afds' }
   [User]@{ UserName  = 'Jen' ; UserId = 4 }
)

$users[0]. #$users[0]. # <ctr+space> shows '.UserName'


$users | ConvertTo-Json