CreatePSProfiles.psm1
|
#Region '.\Public\New-Profile.ps1' -1 using namespace System using namespace System.IO function New-Profile { [CmdletBinding()] param( [ValidateNotNullOrEmpty()] [ValidateSet( "AllUsersCurrentHost", "AllUsersAllHosts", "CurrentUserAllHosts", "CurrentUserCurrentHost", IgnoreCase = $false )] [string] $Type = "CurrentUserCurrentHost" ) begin { Write-Verbose "Starting creating Profile file...`n" $target = switch ($Type) { { $_ -in @("AUCH", "AllUsersCurrentHost") } { $PROFILE.AllUsersCurrentHost } { $_ -in @("AUAH", "AllUsersAllHosts") } { $PROFILE.AllUsersAllHosts } { $_ -in @("CUAH", "CurrentUserAllHosts") } { $PROFILE.CurrentUserAllHosts } { $_ -in @("CUCH", "CurrentUserCurrentHost") } { $PROFILE } default { $PROFILE } } $directory = [Path]::GetDirectoryName($target) $fileName = [Path]::GetFileName($target) Write-Verbose "Profile file name:`n""$($fileName)""`n" Write-Debug "[New-Profile] [Initialization] Target Profile Path:`n""$($target)""`n" } process { try { $stream = $null if (![File]::Exists($target)) { Write-Verbose "Checking and creating directories...`n" if (![String]::IsNullOrWhiteSpace($directory)) { $dirInfo = [Directory]::CreateDirectory($directory) Write-Debug "[New-Profile] [Profile Not Exist] [Directories Creating] Directory:`n""$($dirInfo.FullPath)""`n" } Write-Verbose "Creating Profile ""$($fileName)""...`n" $stream = [File]::Create($target) $stream.Dispose() $stream = $null $fileInfo = [FileInfo]::new($target) if ($null -ne $fileInfo) { Write-Verbose "Profile created:`n""$($fileInfo.FullName)""`n" Write-Debug "[New-Profile] [Profile Creation] Profile:`n$($fileInfo.FullName)`n" return $fileInfo } else { $exception = [System.IO.IOException]::new( "Failed to verify or retrieve the file information after creating the profile at ""$($target)""." ) $errorRecord = New-Object System.Management.Automation.ErrorRecord -ArgumentList @( $exception, "ProfileCreationFailed", [System.Management.Automation.ErrorCategory]::InvalidOperation, $target ) $PSCmdlet.WriteError($errorRecord) return } } else { $fileInfo = [FileInfo]::new($target) Write-Verbose "Profile ""$($fileName)"" has existed`n" Write-Debug "[New-Profile] [Profile Existed] Profile:`n$($fileInfo.FullName)`n" return $fileInfo } } catch { $PSCmdlet.ThrowTerminatingError($_) } finally { if ($null -ne $stream) { $stream.Dispose() } } } end { Write-Verbose "Profile creation finished`n" } } #EndRegion '.\Public\New-Profile.ps1' 78 |