Public/OSDCloud/Invoke-Exe.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

<#
.SYNOPSIS
Run External Command.
 
.DESCRIPTION
This function calls an external command outside of the powershell script and logs the output.
 
.PARAMETER Executable
Executable that needs to be run.
 
.PARAMETER Arguments
Arguments for the executable. Default is NULL.
 
.EXAMPLE
Invoke-Exe dir c:\
#>

function Invoke-Exe {
  [CmdletBinding(SupportsShouldProcess = $true)]
  param (
      [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
      [string]$Executable,

      [Parameter(ValueFromRemainingArguments = $true, ValueFromPipelineByPropertyName = $true)]
      $Arguments = $null
  )
  Write-Host -ForegroundColor Cyan "Invoke-Exe '$Executable' Arguments '$Arguments'"
  $Out = &$Executable $Arguments 2>&1 | Out-String
  if ($Out.Trim()) {
      $Out.Trim().Split("`n") | ForEach-Object {
          Write-Host -ForegroundColor DarkCyan "$_"
      }
  }
}