Skip to content

Commit 5499a59

Browse files
committed
Refactor credential handling and update logic in PSConfigFile scripts
1 parent 664674a commit 5499a59

4 files changed

Lines changed: 8 additions & 736 deletions

File tree

PSConfigFile/Public/Add-CommandToPSConfigFile.ps1

Lines changed: 0 additions & 312 deletions
Original file line numberDiff line numberDiff line change
@@ -154,315 +154,3 @@ function Add-CommandToPSConfigFile {
154154
Write-Host "ConfigFile: $($confile.FullName)" -ForegroundColor Cyan
155155
} catch { Write-Error "Error: `n $_" }
156156
} #end Function
157-
158-
<#PSScriptInfo
159-
160-
.VERSION 1.1.4
161-
162-
.GUID 98459c57-e214-4a9f-b523-efa2329a0340
163-
164-
.AUTHOR Pierre Smit
165-
166-
.COMPANYNAME Private
167-
168-
.COPYRIGHT
169-
170-
.TAGS PowerShell ps
171-
172-
.LICENSEURI
173-
174-
.PROJECTURI
175-
176-
.ICONURI
177-
178-
.EXTERNALMODULEDEPENDENCIES
179-
180-
.REQUIREDSCRIPTS
181-
182-
.EXTERNALSCRIPTDEPENDENCIES
183-
184-
.RELEASENOTES
185-
Created [04/10/2021_19:05] Initial Script Creating
186-
Updated [05/10/2021_08:30] Spit into more functions
187-
Updated [08/10/2021_20:51] Getting ready to upload
188-
Updated [14/10/2021_19:31] Added PSDrive Script
189-
Updated [13/11/2021_16:30] Added Function Script
190-
191-
.PRIVATEDATA
192-
193-
#>
194-
195-
196-
197-
198-
199-
200-
201-
202-
203-
<#
204-
.SYNOPSIS
205-
Adds a named command or script block to the PSConfigFile configuration, to be executed automatically when the config is invoked.
206-
207-
.DESCRIPTION
208-
Use this function to store custom commands or script blocks in your configuration file. These commands will be executed every time the config file is invoked using Invoke-PSConfigFile. This is useful for automating environment setup, running startup tasks, or ensuring certain commands always run in your PowerShell environment.
209-
210-
.PARAMETER ScriptBlockName
211-
The unique name to assign to the script block. This name is used to identify and manage the command within the config file.
212-
213-
.PARAMETER ScriptBlock
214-
The PowerShell command(s) or script block to be executed. Provide as a string. Example: "Get-ChildItem C:\\Logs | Out-File C:\\log.txt"
215-
216-
.PARAMETER Force
217-
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.
218-
219-
.EXAMPLE
220-
Add-CommandToPSConfigFile -ScriptBlockName DriveC -ScriptBlock "Get-ChildItem c:\\"
221-
Adds a script block named 'DriveC' that lists the contents of the C drive every time the config is invoked.
222-
223-
.EXAMPLE
224-
Add-CommandToPSConfigFile -ScriptBlockName Startup -ScriptBlock "Write-Host 'Welcome!'" -Force
225-
Adds a script block named 'Startup' that displays a welcome message, overwriting the config file if it exists.
226-
227-
.NOTES
228-
Author: Pierre Smit
229-
Website: https://smitpi.github.io/PSConfigFile
230-
This function is part of the PSConfigFile module for managing PowerShell configuration automation.
231-
#>
232-
function Add-CommandToPSConfigFile {
233-
[Cmdletbinding(HelpURI = 'https://smitpi.github.io/PSConfigFile/Add-CommandToPSConfigFile')]
234-
param(
235-
[ValidateNotNullOrEmpty()]
236-
[string]$ScriptBlockName,
237-
[ValidateNotNullOrEmpty()]
238-
[string]$ScriptBlock,
239-
[switch]$Force
240-
)
241-
242-
try {
243-
$confile = Get-Item $PSConfigFile -ErrorAction stop
244-
} catch {
245-
Add-Type -AssemblyName System.Windows.Forms
246-
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{ Filter = 'XML | *.xml' }
247-
$null = $FileBrowser.ShowDialog()
248-
$confile = Get-Item $FileBrowser.FileName
249-
}
250-
251-
$XMLData = Import-Clixml -Path $confile.FullName
252-
253-
$userdata = [PSCustomObject]@{
254-
Owner = $XMLData.Userdata.Owner
255-
CreatedOn = $XMLData.Userdata.CreatedOn
256-
PSExecutionPolicy = $XMLData.Userdata.PSExecutionPolicy
257-
Path = $XMLData.Userdata.Path
258-
Hostname = $XMLData.Userdata.Hostname
259-
PSEdition = $XMLData.Userdata.PSEdition
260-
OS = $XMLData.Userdata.OS
261-
BackupsToKeep = $XMLData.Userdata.BackupsToKeep
262-
ModifiedData = [PSCustomObject]@{
263-
ModifiedDate = [datetime](Get-Date)
264-
ModifiedAction = "Added Command: $($ScriptBlockName)"
265-
}
266-
}
267-
268-
$Update = @()
269-
[System.Collections.generic.List[PSObject]]$ExecuteObject = @()
270-
271-
if ([string]::IsNullOrEmpty($XMLData.Execute)) {
272-
$ExecuteObject.Add([PSCustomObject]@{
273-
IndexID = 0
274-
Name = $ScriptBlockName
275-
ScriptBlock = $ScriptBlock
276-
})
277-
} else {
278-
$XMLData.Execute | ForEach-Object {$ExecuteObject.Add($_)}
279-
$IndexID = $ExecuteObject.IndexID | Sort-Object -Descending | Select-Object -First 1
280-
$ExecuteObject.Add([PSCustomObject]@{
281-
IndexID = ($IndexID + 1 )
282-
Name = $ScriptBlockName
283-
ScriptBlock = $ScriptBlock
284-
})
285-
}
286-
$Update = [psobject]@{
287-
Userdata = $Userdata
288-
PSDrive = $XMLData.PSDrive
289-
PSFunction = $XMLData.PSFunction
290-
PSCreds = $XMLData.PSCreds
291-
PSDefaults = $XMLData.PSDefaults
292-
SetLocation = $XMLData.SetLocation
293-
SetVariable = $XMLData.SetVariable
294-
Execute = ($ExecuteObject | Where-Object {$_ -notlike $null})
295-
}
296-
try {
297-
if ($force) {
298-
Remove-Item -Path $confile.FullName -Force -ErrorAction Stop
299-
Write-Host 'Original ConfigFile Removed' -ForegroundColor Red
300-
} else {
301-
Rename-Item -Path $confile -NewName "Outdated_PSConfigFile_$(Get-Date -Format yyyyMMdd_HHmm)_$(Get-Random -Maximum 50).xml" -Force
302-
Write-Host 'Original ConfigFile Renamed' -ForegroundColor Yellow
303-
}
304-
$Update | Export-Clixml -Depth 10 -Path $confile.FullName -NoClobber -Encoding utf8 -Force
305-
Write-Host 'Command Added: ' -ForegroundColor Green -NoNewline
306-
Write-Host "$($ScriptBlockName)" -ForegroundColor Yellow
307-
Write-Host "ConfigFile: $($confile.FullName)" -ForegroundColor Cyan
308-
} catch { Write-Error "Error: `n $_" }
309-
310-
311-
312-
} #end Function
313-
314-
<#PSScriptInfo
315-
316-
.VERSION 1.1.4
317-
318-
.GUID 98459c57-e214-4a9f-b523-efa2329a0340
319-
320-
.AUTHOR Pierre Smit
321-
322-
.COMPANYNAME Private
323-
324-
.COPYRIGHT
325-
326-
.TAGS PowerShell ps
327-
328-
.LICENSEURI
329-
330-
.PROJECTURI
331-
332-
.ICONURI
333-
334-
.EXTERNALMODULEDEPENDENCIES
335-
336-
.REQUIREDSCRIPTS
337-
338-
.EXTERNALSCRIPTDEPENDENCIES
339-
340-
.RELEASENOTES
341-
Created [04/10/2021_19:05] Initial Script Creating
342-
Updated [05/10/2021_08:30] Spit into more functions
343-
Updated [08/10/2021_20:51] Getting ready to upload
344-
Updated [14/10/2021_19:31] Added PSDrive Script
345-
Updated [13/11/2021_16:30] Added Function Script
346-
347-
.PRIVATEDATA
348-
349-
#>
350-
351-
352-
353-
354-
355-
356-
357-
358-
359-
<#
360-
.SYNOPSIS
361-
Adds a named command or script block to the PSConfigFile configuration, to be executed automatically when the config is invoked.
362-
363-
.DESCRIPTION
364-
Use this function to store custom commands or script blocks in your configuration file. These commands will be executed every time the config file is invoked using Invoke-PSConfigFile. This is useful for automating environment setup, running startup tasks, or ensuring certain commands always run in your PowerShell environment.
365-
366-
.PARAMETER ScriptBlockName
367-
The unique name to assign to the script block. This name is used to identify and manage the command within the config file.
368-
369-
.PARAMETER ScriptBlock
370-
The PowerShell command(s) or script block to be executed. Provide as a string. Example: "Get-ChildItem C:\\Logs | Out-File C:\\log.txt"
371-
372-
.PARAMETER Force
373-
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.
374-
375-
.EXAMPLE
376-
Add-CommandToPSConfigFile -ScriptBlockName DriveC -ScriptBlock "Get-ChildItem c:\\"
377-
Adds a script block named 'DriveC' that lists the contents of the C drive every time the config is invoked.
378-
379-
.EXAMPLE
380-
Add-CommandToPSConfigFile -ScriptBlockName Startup -ScriptBlock "Write-Host 'Welcome!'" -Force
381-
Adds a script block named 'Startup' that displays a welcome message, overwriting the config file if it exists.
382-
383-
.NOTES
384-
Author: Pierre Smit
385-
Website: https://smitpi.github.io/PSConfigFile
386-
This function is part of the PSConfigFile module for managing PowerShell configuration automation.
387-
#>
388-
function Add-CommandToPSConfigFile {
389-
[Cmdletbinding(HelpURI = 'https://smitpi.github.io/PSConfigFile/Add-CommandToPSConfigFile')]
390-
param(
391-
[ValidateNotNullOrEmpty()]
392-
[string]$ScriptBlockName,
393-
[ValidateNotNullOrEmpty()]
394-
[string]$ScriptBlock,
395-
[switch]$Force
396-
)
397-
398-
try {
399-
$confile = Get-Item $PSConfigFile -ErrorAction stop
400-
} catch {
401-
Add-Type -AssemblyName System.Windows.Forms
402-
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{ Filter = 'XML | *.xml' }
403-
$null = $FileBrowser.ShowDialog()
404-
$confile = Get-Item $FileBrowser.FileName
405-
}
406-
407-
$XMLData = Import-Clixml -Path $confile.FullName
408-
409-
$userdata = [PSCustomObject]@{
410-
Owner = $XMLData.Userdata.Owner
411-
CreatedOn = $XMLData.Userdata.CreatedOn
412-
PSExecutionPolicy = $XMLData.Userdata.PSExecutionPolicy
413-
Path = $XMLData.Userdata.Path
414-
Hostname = $XMLData.Userdata.Hostname
415-
PSEdition = $XMLData.Userdata.PSEdition
416-
OS = $XMLData.Userdata.OS
417-
BackupsToKeep = $XMLData.Userdata.BackupsToKeep
418-
ModifiedData = [PSCustomObject]@{
419-
ModifiedDate = [datetime](Get-Date)
420-
ModifiedAction = "Added Command: $($ScriptBlockName)"
421-
}
422-
}
423-
424-
$Update = @()
425-
[System.Collections.generic.List[PSObject]]$ExecuteObject = @()
426-
427-
if ([string]::IsNullOrEmpty($XMLData.Execute)) {
428-
$ExecuteObject.Add([PSCustomObject]@{
429-
IndexID = 0
430-
Name = $ScriptBlockName
431-
ScriptBlock = $ScriptBlock
432-
})
433-
} else {
434-
$XMLData.Execute | ForEach-Object {$ExecuteObject.Add($_)}
435-
$IndexID = $ExecuteObject.IndexID | Sort-Object -Descending | Select-Object -First 1
436-
$ExecuteObject.Add([PSCustomObject]@{
437-
IndexID = ($IndexID + 1 )
438-
Name = $ScriptBlockName
439-
ScriptBlock = $ScriptBlock
440-
})
441-
}
442-
$Update = [psobject]@{
443-
Userdata = $Userdata
444-
PSDrive = $XMLData.PSDrive
445-
PSFunction = $XMLData.PSFunction
446-
PSCreds = $XMLData.PSCreds
447-
PSDefaults = $XMLData.PSDefaults
448-
SetLocation = $XMLData.SetLocation
449-
SetVariable = $XMLData.SetVariable
450-
Execute = ($ExecuteObject | Where-Object {$_ -notlike $null})
451-
}
452-
try {
453-
if ($force) {
454-
Remove-Item -Path $confile.FullName -Force -ErrorAction Stop
455-
Write-Host 'Original ConfigFile Removed' -ForegroundColor Red
456-
} else {
457-
Rename-Item -Path $confile -NewName "Outdated_PSConfigFile_$(Get-Date -Format yyyyMMdd_HHmm)_$(Get-Random -Maximum 50).xml" -Force
458-
Write-Host 'Original ConfigFile Renamed' -ForegroundColor Yellow
459-
}
460-
$Update | Export-Clixml -Depth 10 -Path $confile.FullName -NoClobber -Encoding utf8 -Force
461-
Write-Host 'Command Added: ' -ForegroundColor Green -NoNewline
462-
Write-Host "$($ScriptBlockName)" -ForegroundColor Yellow
463-
Write-Host "ConfigFile: $($confile.FullName)" -ForegroundColor Cyan
464-
} catch { Write-Error "Error: `n $_" }
465-
466-
467-
468-
} #end Function

0 commit comments

Comments
 (0)