Public/Invoke-InDirectory.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
function Invoke-InDirectory {
  # https://gist.github.com/chriskuech/a32f86ad2609719598b073293d09ca03#file-tryfinally-2-ps1
  Param(
    [Parameter(Mandatory,ValueFromPipeline)]
    [ValidateScript({
          if(-not (Test-Path -Path $_ -PathType Container)) {
            throw 'Folder does not exist'
          }
          return $true
    })]
    [String[]]$Path,
    [Parameter(Mandatory)]
    [ValidateNotNullOrEmpty()]
    [scriptblock]$ScriptBlock
  )
  process {
    foreach($Loc in $Path) {
      try {
        Push-Location -Path $Loc
        & $ScriptBlock
      }
      finally {
        Pop-Location
      }
    }
  }
}