PSDependScripts/Command.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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
<# .SYNOPSIS Invoke a PowerShell command .DESCRIPTION Invoke a PowerShell command Converts the provided string into a scriptblock, invokes it in the current session. Beware quoting rules If a terminating error occurs, we write it and continue processing. Use FailOnError to change this. Relevant Dependency metadata: Source: The code to run Parameters: FailOnError: If specified, throw a terminating error if the command errors out. .PARAMETER PSDependAction Only option is to install the module. Defaults to Install Install: Install the dependency .PARAMETER Dependency Dependency to process .EXAMPLE @{ ExampleCommand = @{ DependencyType = 'Command' Source = '$x = hostname; "Running a command on $x"' } } # Run some aribtrary PowerShell code that assigns a variable and uses it in a string # Output: Running a command on WJ-LAB #> [cmdletbinding()] param ( [PSTypeName('PSDepend.Dependency')] [psobject[]]$Dependency, [switch]$FailOnError, [ValidateSet('Install')] [string[]]$PSDependAction = @('Install') # No logic for this ) Write-Verbose "Executing $($Dependency.count) commands" foreach($Depend in $Dependency) { foreach($Command in $Depend.Source) { Write-Verbose "Invoking command [$($Dependency.DependencyName)]:`n$Command" $ScriptBlock = [ScriptBlock]::Create($Command) Try { . $ScriptBlock } Catch { if($FailOnError) { Write-Error $_ continue } else { throw $_ } } } } |