Functions/Scripting/Profiling/New-PSProfile.ps1
Function New-PSProfile { [cmdletbinding()] Param ( # Module Prefix Definition [Parameter(Mandatory=$true)] [string] $ModulePrefix, # Module Prefix Definition [Parameter(Mandatory=$true)] [string] $ModuleCompany, # Scope for Profile [Parameter(Mandatory=$false)] [ValidateSet("Global","User")] [string] $ProfileScope = "User", # Module Scope Definition [Parameter(Mandatory=$false)] [ValidateSet("Global","Program","User")] [string] $ModuleScope = "User", # Credential Cache Type [Parameter(Mandatory=$false)] [ValidateSet("MCM","ESS")] [string] $CacheType = "MCM", # TLS Protocols Enabled [Parameter(Mandatory=$false)] [ValidateSet("TLS12","TLS11","TLS")] [string[]] $TLSAllowed = "TLS12" ) Process { # Normalize TLS Protocols into a [Typed] String $TLSString = "[array]("+$(($TLSAllowed | foreach {"`"$_`""}) -join ",")+")" # Define Profile Path based on scope input $ProfilePath = switch($ProfileScope){"User"{$profile.CurrentUserAllHosts};"Global"{$profile.AllUsersAllHosts}} # Fill out script of profile $ProfileScript = '#---------------- Custom Definitions --------------------# $Global:PS_ProfileAutoload = $True $Global:PS_ModulePrefix = "{0}" $Global:PS_ModuleCompany = "{1}" $Global:PS_UserContext = [pscustomobject]([ordered]@{{ username = "$env:USERNAME" domain = "$env:USERDNSDOMAIN" domainAlias = "$env:USERDOMAIN" o365 = "" }}) $Global:PS_ModuleScope = "{2}" $Global:PS_CredentialCacheType = "{3}" $Global:PS_TLSAllowed = {4} #------------- Automated Configurations ----------------# $Global:PS_RootPath_User = "C:\Users\$env:USERNAME\Documents\WindowsPowerShell" $Global:PS_RootPath_Program = "C:\Program Files\WindowsPowerShell" $Global:PS_RootPath_Global = "C:\Windows\System32\WindowsPowerShell" $Global:PS_RootPath = switch ($Global:PS_ModuleScope) {{ "User" {{$Global:PS_RootPath_User}} "Program" {{$Global:PS_RootPath_Program}} "Global" {{$Global:PS_RootPath_Global}} }} $Global:PS_ModulesPath = "$Global:PS_RootPath\Modules" $Global:PS_ProfilePath = "$Global:PS_RootPath\profile.ps1" if($Global:PS_CredentialCacheType -eq "ESS") {{ $Global:PS_CredentialPath = "$Global:PS_RootPath_User\Credentials" if(!(Test-path $PS_CredentialPath)) {{$null = New-Item -Path $Global:PS_CredentialPath -Force -ItemType Directory}} }} if ($Global:PS_TLSAllowed){{[System.Net.ServicePointManager]::SecurityProtocol = foreach ($TLS in $Global:PS_TLSAllowed){{[System.Net.SecurityProtocolType]::$TLS}}}} Write-Host "User $($Global:PS_UserContext.username) Context Loaded" -ForegroundColor Cyan ' -f $ModulePrefix,$ModuleCompany,$ModuleScope,$CacheType,$TLSString # Output Profile Data to Path $ProfileScript | Out-File $ProfilePath if (test-path $ProfilePath){write-Host "Created new powershell profile for $env:USERNAME at $ProfileScope Scope" -ForegroundColor Cyan} } } |