1+
12<# PSScriptInfo
23
34.VERSION 0.1.0
67
78.AUTHOR Pierre Smit
89
9- .COMPANYNAME Private
10+ .COMPANYNAME HTPCZA Tech
1011
1112.COPYRIGHT
1213
@@ -31,145 +32,12 @@ Created [18/08/2022_07:54] Initial Script Creating
3132
3233#>
3334
34- <#
35- . SYNOPSIS
36- Adds a default parameter value for a function to the PSConfigFile configuration.
37-
38- . DESCRIPTION
39- This function allows you to specify default parameter values for any PowerShell function. These defaults are stored in your configuration file and will be automatically applied in your session, saving you from repeatedly specifying common parameters. Wildcards can be used to apply defaults to multiple functions or parameters.
40-
41- . PARAMETER Function
42- The name of the function to add a default parameter for. Wildcards are supported to match multiple functions.
43-
44- . PARAMETER Parameter
45- The name of the parameter to set a default value for. Wildcards are supported to match multiple parameters.
46-
47- . PARAMETER Value
48- The value to assign as the default for the specified parameter.
49-
50- . PARAMETER Force
51- If specified, the config file will be deleted before saving the new one. If not specified and a config file exists, it will be renamed as a backup before saving the new version.
52-
53- . EXAMPLE
54- Add-PSDefaultParameterToPSConfigFile -Function Start-PSLauncher -Parameter PSLauncherConfigFile -Value C:\\temp\\PSLauncherConfig.json
55- Sets a default value for the 'PSLauncherConfigFile' parameter of the 'Start-PSLauncher' function.
56-
57- . EXAMPLE
58- Add-PSDefaultParameterToPSConfigFile -Function *-Item -Parameter Path -Value C:\\Data -Force
59- Sets a default 'Path' for all functions ending with '-Item', overwriting the config file if it exists.
60-
61- . NOTES
62- Author: Pierre Smit
63- Website: https://smitpi.github.io/PSConfigFile
64- Use this to streamline your PowerShell workflow with persistent default parameters.
65- #>
66- function Add-PSDefaultParameterToPSConfigFile {
67- [Cmdletbinding (HelpURI = ' https://smitpi.github.io/PSConfigFile/Add-PSDefaultParameterToPSConfigFile' )]
68- [OutputType ([System.Object []])]
69- param (
70- [Parameter (Position = 0 , Mandatory = $true , HelpMessage = ' Name of a function to add, You can use wildcards to apply to more functions.' )]
71- [string ]$Function ,
72- [Parameter (Position = 1 , Mandatory = $true , HelpMessage = ' Name of a parameter to add, You can use wildcards to apply to more parameters.' )]
73- [string ]$Parameter ,
74- [Parameter (Position = 2 , Mandatory = $true , HelpMessage = ' The Value to add.' )]
75- [string ]$Value ,
76- [switch ]$Force
77- )
78-
79- try {
80- $confile = Get-Item $PSConfigFile - ErrorAction stop
81- } catch {
82- Add-Type - AssemblyName System.Windows.Forms
83- $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog - Property @ { Filter = ' XML | *.xml' }
84- $null = $FileBrowser.ShowDialog ()
85- $confile = Get-Item $FileBrowser.FileName
86- }
87-
88- $XMLData = Import-Clixml - Path $confile.FullName
89- $userdata = [PSCustomObject ]@ {
90- Owner = $XMLData.Userdata.Owner
91- CreatedOn = $XMLData.Userdata.CreatedOn
92- PSExecutionPolicy = $XMLData.Userdata.PSExecutionPolicy
93- Path = $XMLData.Userdata.Path
94- Hostname = $XMLData.Userdata.Hostname
95- PSEdition = $XMLData.Userdata.PSEdition
96- OS = $XMLData.Userdata.OS
97- BackupsToKeep = $XMLData.Userdata.BackupsToKeep
98- ModifiedData = [PSCustomObject ]@ {
99- ModifiedDate = [datetime ](Get-Date )
100- ModifiedAction = " Add PSDefaultParameter $ ( $Function ) :$ ( $Parameter ) "
101- }
102- }
103- [System.Collections.generic.List [PSObject ]]$PSDefaultObject = @ ()
104- if ([string ]::IsNullOrEmpty($XMLData.PSDefaults )) {
105- [void ]$PSDefaultObject.Add ([PSCustomObject ]@ {
106- Name = " $ ( $Function ) :$ ( $Parameter ) "
107- Value = $Value
108- })
109- } else {
110- $XMLData.PSDefaults | ForEach-Object {[void ]$PSDefaultObject.Add ($_ )}
111- [void ]$PSDefaultObject.Add ([PSCustomObject ]@ {
112- Name = " $ ( $Function ) :$ ( $Parameter ) "
113- Value = $Value
114- })
115- }
116- $Update = [psobject ]@ {
117- Userdata = $Userdata
118- PSDrive = $XMLData.PSDrive
119- PSFunction = $XMLData.PSFunction
120- PSCreds = $XMLData.PSCreds
121- PSDefaults = ($PSDefaultObject | Where-Object {$_ -notlike $null })
122- SetLocation = $XMLData.SetLocation
123- SetVariable = $XMLData.SetVariable
124- Execute = $XMLData.Execute
125- }
126- try {
127- if ($force ) {
128- Remove-Item - Path $confile.FullName - Force - ErrorAction Stop
129- Write-Host ' Original ConfigFile Removed' - ForegroundColor Red
130- } else {
131- Rename-Item - Path $confile - NewName " Outdated_PSConfigFile_$ ( Get-Date - Format yyyyMMdd_HHmm) _$ ( Get-Random - Maximum 50 ) .xml" - Force
132- Write-Host ' Original ConfigFile Renamed' - ForegroundColor Yellow
133- }
134- $Update | Export-Clixml - Depth 10 - Path $confile.FullName - NoClobber - Encoding utf8 - Force
135- Write-Host ' PSDefault Added: ' - ForegroundColor Green - NoNewline
136- Write-Host " $ ( $Function ) :$ ( $Parameter ) " - ForegroundColor Yellow
137- Write-Host " ConfigFile: $ ( $confile.FullName ) " - ForegroundColor Cyan
138- } catch { Write-Error " Error: `n $_ " }
139- } # end Function
140-
141- <# PSScriptInfo
142-
143- .VERSION 0.1.0
144-
145- .GUID 4a597ee8-f395-4479-9f80-1730b90e0eaf
146-
147- .AUTHOR Pierre Smit
148-
149- .COMPANYNAME Private
150-
151- .COPYRIGHT
152-
153- .TAGS ps
154-
155- .LICENSEURI
35+ <#
15636
157- .PROJECTURI
37+ . DESCRIPTION
38+ Add PSDefaultParameterValues to the config file
15839
159- .ICONURI
160-
161- .EXTERNALMODULEDEPENDENCIES
162-
163- .REQUIREDSCRIPTS
164-
165- .EXTERNALSCRIPTDEPENDENCIES
166-
167- .RELEASENOTES
168- Created [18/08/2022_07:54] Initial Script Creating
169-
170- .PRIVATEDATA
171-
172- #>
40+ #>
17341
17442<#
17543. SYNOPSIS
@@ -277,3 +145,4 @@ function Add-PSDefaultParameterToPSConfigFile {
277145 Write-Host " ConfigFile: $ ( $confile.FullName ) " - ForegroundColor Cyan
278146 } catch { Write-Error " Error: `n $_ " }
279147} # end Function
148+
0 commit comments