Public/New-PSConfigFile.ps1


<#PSScriptInfo
 
.VERSION 1.1.3
 
.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] Initital 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
 
.PRIVATEDATA
 
#>
 







<#
 
.DESCRIPTION
To store your settings
 
#>


Param()

#.ExternalHelp PSConfigFile-help.xml
Function New-PSConfigFile {
    [Cmdletbinding(SupportsShouldProcess = $true)]
    param (
        [parameter(Mandatory)]
        [ValidateScript( { (Test-Path $_) -and ((Get-Item $_).Attributes -eq 'Directory') })]
        [System.IO.DirectoryInfo]$ConfigDir
    )

    # TODO Add logging
    # TODO Add rotation of the logs
    # TODO Add PSDrives
    function DafaultSettings {
        $Userdata = @()
        $SetLocation = @()
        $SetVariable = @()
        $Execute = @()

        $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                      = [DateTimeOffset]::Now
        }
        $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'
        }
        #main
        New-Object PSObject -Property @{
            Userdata    = $Userdata
            PSDrive     = $PSDrive
            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


        }
    }
Invoke-PSConfigFile -ConfigFile (Join-Path $Fullpath -ChildPath \PSCustomConfig.json)
}