Public/New-PSConfigFile.ps1
<#PSScriptInfo .VERSION 1.1.4 .GUID ec11b3b9-d13e-43c9-8a6d-d42a65016554 .AUTHOR Pierre Smit .COMPANYNAME iOCO Tech .COPYRIGHT .TAGS ps .LICENSEURI .PROJECTURI .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES Created [25/09/2021_02:52] Initial Script Creating Updated [05/10/2021_08:30] Spit into more functions Updated [08/10/2021_20:51] Getting ready to upload Updated [14/10/2021_19:32] Added PSDrive Script Updated [13/11/2021_16:30] Added Alias Script .PRIVATEDATA #> <# .DESCRIPTION To store your settings #> <# .SYNOPSIS Creates a new config file .DESCRIPTION Creates a new config file. If a config file already exists in that folder, it will be renamed. It will also create a log file in the same directory. Log file will be used on every execution. .PARAMETER ConfigDir Directory to create config file .EXAMPLE New-PSConfigFile -ConfigDir C:\Temp\jdh #> Function New-PSConfigFile { [Cmdletbinding(SupportsShouldProcess = $true)] param ( [parameter(Mandatory)] [ValidateScript( { (Test-Path $_) -and ((Get-Item $_).Attributes -eq 'Directory') })] [System.IO.DirectoryInfo]$ConfigDir ) # TODO Add rotation of the logs function DafaultSettings { $Userdata = @() $SetLocation = @() $SetVariable = @() $Execute = @() $PSAlias = @() $OSDetails = Get-ComputerInfo $Userdata = New-Object PSObject -Property @{ Computer = $env:COMPUTERNAME WindowsProductName = $OSDetails.WindowsProductName WindowsEditionId = $OSDetails.WindowsEditionId WindowsInstallationType = $OSDetails.WindowsInstallationType WindowsInstallDateFromRegistry = $OSDetails.WindowsInstallDateFromRegistry OsArchitecture = $OSDetails.OsArchitecture OsProductType = $OSDetails.OsProductType OsStatus = $OSDetails.OsStatus DomainName = $env:USERDNSDOMAIN Userid = $env:USERNAME CreatedOn = (Get-Date -Format yyyy/MM/dd_HH:MM) } $SetLocation = New-Object PSObject -Property @{} $SetVariable = New-Object PSObject -Property @{ Default = 'Default' } $Execute = New-Object PSObject -Property @{ Default = 'Default' } $PSDrive = New-Object PSObject -Property @{ Default = 'Default' } $PSAlias = New-Object PSObject -Property @{ Default = 'Default' } #main New-Object PSObject -Property @{ Userdata = $Userdata PSDrive = $PSDrive PSAlias = $PSAlias SetLocation = $SetLocation SetVariable = $SetVariable Execute = $Execute } } $Fullpath = Get-Item $ConfigDir if ($pscmdlet.ShouldProcess('Target', 'Operation')) { $check = Test-Path -Path (Join-Path $Fullpath -ChildPath \PSCustomConfig.json) -ErrorAction SilentlyContinue if (-not($check)) { Write-Output 'Config File does not exit, creating default settings.' $data = DafaultSettings $data | ConvertTo-Json -Depth 5 | Out-File (Join-Path $Fullpath -ChildPath \PSCustomConfig.json) -Verbose -Force } else { Write-Warning 'File exists, renaming file now' Rename-Item (Join-Path $Fullpath -ChildPath \PSCustomConfig.json) -NewName "PSCustomConfig_$(Get-Date -Format ddMMyyyy_HHmm).json" $data = DafaultSettings $data | ConvertTo-Json -Depth 5 | Out-File (Join-Path $Fullpath -ChildPath \PSCustomConfig.json) -Verbose -Force } } } |