From 4de25b4a56ddcaaf66d8588dfeb81dd5534c84d6 Mon Sep 17 00:00:00 2001 From: lksz Date: Sun, 1 Nov 2020 01:26:20 -0400 Subject: [PATCH] Added -OneLine which outputs a PS table --- Invoke-ViaAnsible.ps1 | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/Invoke-ViaAnsible.ps1 b/Invoke-ViaAnsible.ps1 index 3f1425d..3e5c8ce 100644 --- a/Invoke-ViaAnsible.ps1 +++ b/Invoke-ViaAnsible.ps1 @@ -1,4 +1,10 @@ -[CmdletBinding()]param([string[]]$Command, [switch]$NotPowerShell, [string[]]$Remotes) +[CmdletBinding()]param( + [string[]]$Command, + [string[]]$Remotes = 'all', + [string[]]$MoreArgs, + [switch]$NotPowerShell, + [switch]$OneLine +) $script:ansible_cli=@('ansible') if( (Test-Path './ansible.cfg') -or (Test-Path '/opt/ansible/hosts') -and (Test-Path '/opt/ansible/ssh_key_for_ansible') ) { @@ -12,8 +18,29 @@ if( $Command ) { $ansible_cli += "pwsh -encodedcommand $(ConvertTo-Base64 $($Command -join '; '))" } } +if( $OneLine ) { + $ansible_cli += '--one-line' +} $ansible_cli += $Remotes +$ansible_cli += $MoreArgs $local:expr = $('& "'+$($ansible_cli -join '" "')+'"') Write-Verbose $expr -Invoke-Expression $expr +$local:SaveConsoleColor = [Console]::ForegroundColor +Invoke-Expression $expr | ForEach-Object { + if( -not $OneLine ) { return $_ }; + $local:res = $_.Split('|'); + $res = [PSCustomObject]([ordered]@{ + Machine = $res[0].Trim(); + Status = $res[1].Trim(); + RC = "$($res[2])".Trim(); + Output = "$($res[3])".Trim(); + }) + if( $res.Status -match '^\w+!:' ) { + [Console]::ForegroundColor = [ConsoleColor]::Red + } else { + [Console]::ForegroundColor = [ConsoleColor]::Yellow + } + $res +} +[Console]::ForegroundColor = $SaveConsoleColor