Skip to content

Added Pester, ShouldProcess and Calculated Property PS Snippets #1764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 5, 2019
67 changes: 67 additions & 0 deletions snippets/PowerShell.json
Original file line number Diff line number Diff line change
Expand Up @@ -952,5 +952,72 @@
"#endregion"
],
"description": "Region Block for organizing and folding of your code"
},
"IfShouldProcess": {
"prefix": "IfShouldProcess",
"body": [
"if (\\$PSCmdlet.ShouldProcess(\"${1:Target}\", \"${2:Operation}\")) {",
"\t$0",
"}"
],
"description": "Adds ShouldProcess block"
},
"CalculatedProperty": {
"prefix": "Calculated-Property",
"body": [
"@{name='${1:PropertyName}';expression={\\$_.${2:PropertyValue}}}$0"
],
"description": "Creates a PSCustomObject"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought there was already a PSCustomObject snippet in this file. Is there not?

Also, isn't this just a hashtable, not a true PSCustomObject

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woops. Thats my lazy copy paste it's a calculated property for use with select-object.

I'll update when back on the laptop.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the description for the calculated property @TylerLeonhardt

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry can you give me an example of this? I'm having a hard time seeing the scenario.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Linked from the Select-Object docs:

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-object?view=powershell-6#examples

-Property

Specifies the properties to select. These properties are added as NoteProperty members to the output objects. Wildcards are permitted.

The value of the Property parameter can be a new calculated property. To create a calculated, property, use a hash table. Valid keys are:

    Name (or Label) <string>
    Expression <string> or <script block>
Get-Process | Select-Object -Property ProcessName,@{Name="Start Day"; Expression = {$_.StartTime.DayOfWeek}}

ProcessName  StartDay
----         --------
alg          Wednesday
ati2evxx     Wednesday
ati2evxx     Thursday
...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say it's used quite a bit when working with Active Directory.

Get-ADUser -Identity brettmillerb -properties manager | Select-Object -Property @(
    @{name = 'Manager'; expression = { Get-ADUser -identity $_.Manager | Select-Object -ExpandProperty samaccountname }}
)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhh I see now! Thanks for the explanation.

},
"PesterDescribeContextIt": {
"prefix": "Describe-Context-It-Pester",
"body": [
"Describe \"${1:DescribeName}\" {",
"\tContext \"${2:ContextName}\" {",
"\t\tIt \"${3:ItName}\" {",
"\t\t\t${4:Assertion}",
"\t\t}$0",
"\t}",
"}"
],
"description": "Pester Describe block with nested Context & It blocks"
},
"PesterDescribeBlock": {
"prefix": "Describe-Pester",
"body": [
"Describe \"${1:DescribeName}\" {",
"\t$0",
"}"
],
"description": "Pester Describe block"
},
"PesterContextIt": {
"prefix": "Context-It-Pester",
"body": [
"Context \"${1:ContextName}\" {",
"\tIt \"${2:ItName}\" {",
"\t\t${3:Assertion}",
"\t}$0",
"}"
],
"description": "Pester - Context block with nested It block"
},
"PesterContext": {
"prefix": "Context-Pester",
"body": [
"Context \"${1:ContextName}\" {",
"\t$0",
"}"
],
"description": "Pester - Context block"
},
"PesterIt": {
"prefix": "It-Pester",
"body": [
"It \"${1:ItName}\" {",
"\t${2:Assertion}",
"}$0"
],
"description": "Pester - It block"
}
}