Script directory for a PowerShell profile. https://blog.lksz.me/keeping-track-of-everything/#-mypsscriptroot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.6 KiB

  1. [CmdletBinding()]param(
  2. [string[]]$Command,
  3. [string[]]$Remotes = 'all',
  4. [string[]]$MoreArgs,
  5. [switch]$NotPowerShell,
  6. [switch]$OneLine
  7. )
  8. $script:ansible_cli=@('ansible')
  9. if( (Test-Path './ansible.cfg') -or (Test-Path '/opt/ansible/hosts') -and (Test-Path '/opt/ansible/ssh_key_for_ansible') ) {
  10. $ansible_cli += '-i', '/opt/ansible/hosts', '--private-key=/opt/ansible/ssh_key_for_ansible'
  11. }
  12. if( $Command ) {
  13. $ansible_cli += '-a'
  14. $ansible_cli += 'ANSIBLE_PYTHON_INTERPRETER=auto_silent '
  15. $ansible_cli += '-a'
  16. if( $NotPowerShell ) {
  17. $ansible_cli += $(($Command -join '; ').Replace("'","\`$(printf '\x27')").Replace('"',"\`$(printf '\x22')"))
  18. } else {
  19. $ansible_cli += "pwsh -encodedcommand $(ConvertTo-Base64 $($Command -join '; '))"
  20. }
  21. }
  22. if( $OneLine ) {
  23. $ansible_cli += '--one-line'
  24. }
  25. $ansible_cli += $Remotes
  26. $ansible_cli += $MoreArgs
  27. $local:expr = $('& "'+$($ansible_cli -join '" "')+'"')
  28. Write-Verbose $expr
  29. $local:SaveConsoleColor = [Console]::ForegroundColor
  30. $env:ANSIBLE_DEPRECATION_WARNINGS='false'
  31. Invoke-Expression $expr | ForEach-Object {
  32. if( -not $OneLine ) { return $_ };
  33. $local:res = $_.Split('|');
  34. $res = [PSCustomObject]([ordered]@{
  35. Machine = $res[0].Trim();
  36. Status = $res[1].Trim();
  37. RC = "$($res[2])".Trim();
  38. Output = "$($res[3])".Trim();
  39. })
  40. if( $res.Status -match '^\w+!:' ) {
  41. [Console]::ForegroundColor = [ConsoleColor]::Red
  42. } else {
  43. [Console]::ForegroundColor = [ConsoleColor]::Yellow
  44. }
  45. $res
  46. }
  47. [Console]::ForegroundColor = $SaveConsoleColor