Skip to content
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

Added Pester, ShouldProcess and Calculated Property PS Snippets #1764

Merged
merged 7 commits into from
Mar 5, 2019

Conversation

brettmillerb
Copy link
Contributor

PR Summary

Added snippets for the following:

  • Calculated Property for use with Select-Object -Property
  • If Should Process for quickly adding if ($PSCmdlet.ShouldProcess("Target", "Operation")) {...}
  • Pester:
    • Describe Block
    • Describe with nested context & It block
    • Context Block
    • Context with nested It Block
    • It Block

PR Checklist

Note: Tick the boxes below that apply to this pull request by putting an x between the square brackets.
Please mark anything not applicable to this PR NA.

  • PR has a meaningful title
  • Summarized changes
  • PR has tests
  • This PR is ready to merge and is not work in progress
    • If the PR is work in progress, please add the prefix WIP: to the beginning of the title and remove the prefix when the PR is ready

@msftclas
Copy link

msftclas commented Feb 18, 2019

CLA assistant check
All CLA requirements met.

"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.

Copy link
Member

@TylerLeonhardt TylerLeonhardt left a comment

Choose a reason for hiding this comment

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

One tiny nit on the description so it's a little more informative. Otherwise LGTM!

snippets/PowerShell.json Outdated Show resolved Hide resolved
@brettmillerb
Copy link
Contributor Author

I updated the calculated property snippet to include the $_. and not just the PropertyValue of $PsItem

@brettmillerb brettmillerb reopened this Mar 4, 2019
Copy link
Member

@TylerLeonhardt TylerLeonhardt left a comment

Choose a reason for hiding this comment

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

LGTM

@TylerLeonhardt TylerLeonhardt merged commit 5962cb4 into PowerShell:master Mar 5, 2019
@TylerLeonhardt
Copy link
Member

Thanks for contributing @brettmillerb!

@brettmillerb brettmillerb deleted the feature/snippets branch March 5, 2019 13:26
TylerLeonhardt pushed a commit to TylerLeonhardt/vscode-powershell that referenced this pull request Mar 18, 2019
…rShell#1764)

* Added Calculated Property and ShouldProcess Snippets

* Added Pester Describe, Context & It Snippets

* Placeholder Rename for calc property snippet

* Fixed description for Calculated Property

* Calc Property Enhanced Description

Co-Authored-By: brettmillerb <brett@millerb.co.uk>

* Amended the tab stop for calc-property to include .
TylerLeonhardt pushed a commit that referenced this pull request Mar 20, 2019
* Added Calculated Property and ShouldProcess Snippets

* Added Pester Describe, Context & It Snippets

* Placeholder Rename for calc property snippet

* Fixed description for Calculated Property

* Calc Property Enhanced Description

Co-Authored-By: brettmillerb <brett@millerb.co.uk>

* Amended the tab stop for calc-property to include .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants