This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Get-Module Pester, A, B | Remove-Module | |
| Import-Module C:\Projects\Pester_main\Pester.psd1 | |
| New-Module A { } | Import-Module | |
| New-Module B { | |
| function Greet ($Name) { Hello -Name $Name } | |
| function Hello ($Name) { "Hello, $Name" } | |
| Export-ModuleMember -Function WrapWrapWrap, Greet | |
| } | Import-Module |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # in module scope | |
| $script:pester | |
| function describe () { | |
| $script:pester.EnterBlock("describe") | |
| } | |
| function it () { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $p1 = 10 | |
| $p2 = 20 | |
| $f = { | |
| "-$p1- -$p2-" | |
| } | |
| & { | |
| param($p1) | |
| # outputs the scriptblock, instead of 10 | |
| # we have a naming conflict of names |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # InvokeWith context is useful when we have something like | |
| # ``` | |
| # $num = 1 | |
| # 1,2,3 | Assert-All -filter { $num -eq $_ } | |
| # ``` | |
| # Where Assert-All is defined in a module where we invoke the filter script. The script needs to | |
| # stay bounded to the original scope to keep $name resolvable, but we also need to push the | |
| # $_ variable into the script block when invoking it. | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function A { | |
| [CmdletBinding()] | |
| param() | |
| if ($null -eq $Option) { throw } | |
| } | |
| Describe "A" { | |
| it "works" { | |
| $option = "abc" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # variable in parent scope | |
| $a = 1 | |
| function f1 ($a) { | |
| # $a is shadowed by the parameter result is -- | |
| "-$a-" | |
| } | |
| f1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #requires -RunAsAdministrator | |
| param ( | |
| [Parameter(Mandatory)] | |
| $Path) | |
| $ErrorActionPreference = 'stop' | |
| if (-not (Test-Path $Path)) {throw "Path $Path does not exist!" } | |
| # import it for current user |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $MyObject = [PSCustomObject]@{ Value = 15; Value2 = 13; Value3 = "j" } | |
| $Fields = @{ | |
| Value = "special" | |
| Value2 = "Different" | |
| } | |
| $hashtable = @{} | |
| foreach ($property in $MyObject.PSObject.Properties) { | |
| if ($Fields.ContainsKey($property.Name)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| "a" | Set-Content "c:\temp\abc.txt" -Force | |
| Remove-Item "c:\temp\bbc.txt" | |
| Get-Item "C:\temp\abc.txt" | | |
| Rename-Item -NewName { $_.Name -replace "a","b"} | |
| Get-Content "c:\temp\bbc.txt" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # command to be tested | |
| $commandName = 'Get-Command' | |
| # get all examples from the help | |
| $examples = Get-Help $commandName -Examples | |
| # make a describe block that will contain tests for this | |
| Describe "Examples from $commandName" { | |
| $examples.Examples.Example | foreach { | |
| # examples have different format, | |
| # at least the ones I used that MS provided |