PoshCode Archive  Artifact [8b633db6b2]

Artifact 8b633db6b20f62b643403734dcefce1fa2873bdf88316f5832040395041410f1:

  • File DefaultParameterValues.ps1 — part of check-in [e7333dee85] at 2018-06-10 13:18:19 on branch trunk — For PowerShell 3: Export and Import DefaultParameterValues, optionally overwrite existing ones (user: Joel Bennett size: 1891)

# encoding: ascii
# api: powershell
# title: DefaultParameterValues
# description: For PowerShell 3: Export and Import DefaultParameterValues, optionally overwrite existing ones
# version: 3.0
# type: function
# author: Joel Bennett
# license: CC0
# function: Join-Hashtable
# x-poshcode-id: 2990
# x-archived: 2012-01-26T08:00:54
# x-published: 2012-10-06T20:57:00
#
#
#requires -Version 3.0
function Join-Hashtable {
param(
   [Hashtable]$First,
   [Hashtable]$Second,
   [Switch]$Force
)
   $Firsts = $First.Keys
   $Output = @{} + $First
   
   foreach($key in $Second.Keys) {
      if($Firsts -notcontains $Key) {
         $Output.$Key = $Second.$Key
      } elseif($Force) {
         $Output.$Key = $Second.$Key
      }
   }
   $Output
}

function Export-DefaultParameterValues {
param(
   $Path = "DefaultParameterValues.ps1xml",
   [Switch]$AllUsers
)
   if((Split-Path $Path -Leaf) -eq $Path) {
      if($AllUsers) {
         $Path = Join-Path (Split-Path $Profile.AllUsersAllHosts) $Path
      } else {
         $Path = Join-Path (Split-Path $Profile.CurrentUserAllHosts) $Path
      }
   }
   Export-Clixml -InputObject $PSDefaultParameterValues -Path $Path
}

function Import-DefaultParameterValues {
param(
   $Path = "DefaultParameterValues.ps1xml",
   [Switch]$AllUsers,
   [Switch]$Force
)
   if((Split-Path $Path -Leaf) -eq $Path) {
      if($AllUsers) {
         $Path = Join-Path (Split-Path $Profile.AllUsersAllHosts) $Path
      } else {
         $Path = Join-Path (Split-Path $Profile.CurrentUserAllHosts) $Path
      }
   }
   if($PSDefaultParameterValues.Count -eq 0) {
      $Global:PSDefaultParameterValues = Import-Clixml -Path $Path
   } else {
      $PSD = Import-Clixml -Path $Path

      $Global:PSDefaultParameterValues = Join-Hashtable $PSDefaultParameterValues $PSD -Force:$Force
   }
}