Creating Registry Keys & Values; Replace #If with #Switch Statements #PowerShell

How to Create Different Registry Keys with varying Registry Values and Value Data with #PowerShell and #Switch Statement + Logging

Sample script as below:

<#

HKCU:\SOFTWARE\Microsoft\Office\16.0\Word\Options
"AUTOSAVE-PATH"

HKCU:\SOFTWARE\Microsoft\Office\16.0\PowerPoint\Options
"PathToAutoRecoveryInfo"

HKCU:\SOFTWARE\Microsoft\Office\16.0\Excel\options
"AutoRecoverPath"

#>

Function LogIt
{
  Param (
  [Parameter(Mandatory=$True)]
  $Message,
  [Parameter(Mandatory=$True)]
  $Component,
  [Parameter(Mandatory=$True)]
  $Type )

  switch ($Type)
  {
    0 { $Type = "SUCCESS" }
    1 { $Type = "INFO" }
    2 { $Type = "WARNING" }
    3 { $Type = "ERROR" }
    4 { $Type = "VERBOSE" }
  }

  if (($Type -eq "VERBOSE") -and ($Global:Verbose))
  {
    $ToLog = "{0} `$$<{1}><{2} {3}><thread={4}>" -f ($Type + " : " + $Message), ($Global:ScriptName + " : " + $Component), (Get-Date -Format "MM-dd-yyyy"), (Get-Date -Format "HH:mm:ss.ffffff"), $pid
    $ToLog | Out-File -Append -Encoding UTF8 -FilePath ("filesystem::{0}" -f $Global:LogFile)
    Write-Host $Message
  }
  elseif ($Type -ne "VERBOSE")
  {
    $ToLog = "{0} `$$<{1}><{2} {3}><thread={4}>" -f ($Type + " : " + $Message), ($Global:ScriptName + " : " + $Component), (Get-Date -Format "MM-dd-yyyy"), (Get-Date -Format "HH:mm:ss.ffffff"), $pid
    $ToLog | Out-File -Append -Encoding UTF8 -FilePath ("filesystem::{0}" -f $Global:LogFile)
    Write-Host $Message
  }
  if (($Type -eq 'WARNING') -and ($Global:ScriptStatus -ne 'ERROR')) { $Global:ScriptStatus = $Type }
  if ($Type -eq 'ERROR') { $Global:ScriptStatus = $Type }
  if ($Type -eq 'SUCCESS') { $Global:ScriptStatus = $Type }

  if ((Get-Item $Global:LogFile).Length/1KB -gt $Global:MaxLogSizeInKB)
  {
    $log = $Global:LogFile
    Remove-Item ($log.Replace(".log", ".lo_"))
    Rename-Item $Global:LogFile ($log.Replace(".log", ".lo_")) -Force
  }
}

$component = "O365Recovery"
$VerboseLogging = "True"
[bool]$Global:Verbose = [System.Convert]::ToBoolean($VerboseLogging)
#$Global:LogFile = "$env:ProgramData\xyz\Logs\$component.log"
$Global:LogFile = "$env:Temp\$component.log"
$Global:MaxLogSizeInKB = 10240
$Global:ScriptName = 'O365Recovery.ps1' 
$Global:ScriptStatus = 'Success'
$userName = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name

## Setup logging

LogIt -message " ******** Logging Started for User $username ******** " -component $component -type 1


$apps = 'Word','PowerPoint','Excel'

LogIt -message " ******** Setting Vars for $apps ******** " -component $component -type 1

LogIt -message " ******** Starting Loop through Apps from $apps ******** " -component $component -type 1

Foreach ($app in $apps) {

LogIt -message " ******** Setting RegPath for $app ******** " -component $component -type 1

$path = "HKCU:\SOFTWARE\Microsoft\Office\16.0\$app\Options"

LogIt -message " ******** Processing Switch Statement Mapping for RegPath $path  ******** " -component $component -type 1

switch ( $path )
    {
    "HKCU:\SOFTWARE\Microsoft\Office\16.0\Word\Options" { $result = 'AUTOSAVE-PATH' ; BREAK   }
    "HKCU:\SOFTWARE\Microsoft\Office\16.0\PowerPoint\Options" { $result = 'PathToAutoRecoveryInfo' ; BREAK  }
    "HKCU:\SOFTWARE\Microsoft\Office\16.0\Excel\Options" { $result = 'AutoRecoverPath' ; BREAK  }
    }

LogIt -message " ******** Creating RegPath $path from the Switch Statement Mapping ******** " -component $component -type 1

If (!(Test-Path -Path "$path")) {

LogIt -message " ******** RegPath $path doesnt Exist. Creating RegPath $path ******** " -component $component -type 1

New-Item -Path $path -Force | Out-Null}
Set-ItemProperty -Path "$path" -Name "$result" -Type ExpandString -Value "%HOMESHARE%\OfficeRecover\$app" -Force | Out-Null

LogIt -message " ******** Successfully Created $path and $result for $app ******** " -component $component -type 0

}

LogIt -message " ******** Successfully Created RegPath Keys and Registry values for $apps ******** " -component $component -type 0

LogIt -message " ******** Logging Finished for User $username ******** " -component $component -type 0

Hope this helps in creating your Registry Keys and Registry Values with #PowerShell

Cheers

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: