Skip to content

Instantly share code, notes, and snippets.

@RaRaRatchet
Last active November 26, 2023 15:36
Show Gist options
  • Select an option

  • Save RaRaRatchet/196a041ef3599d2dee52ce124b4b2c9d to your computer and use it in GitHub Desktop.

Select an option

Save RaRaRatchet/196a041ef3599d2dee52ce124b4b2c9d to your computer and use it in GitHub Desktop.
Logging
## Set logging variables:
$script_log_dir = "C:\Logs"
$script_name = $MyInvocation.MyCommand.Definition | Split-Path -Leaf
$script_short_name = $script_name.split(".")[0]
$script_path = $MyInvocation.MyCommand.Definition | Split-Path -Parent
$script_registry_hive = "HKLM:\Software\HG3"
$script_logfile = $script_log_dir + "\" + $script_short_name + ".log"
$script_logfile_with_date = $script_log_dir + "\" + $script_short_name + "-" + (Get-Date -Format yyyy-MM-dd) + ".log"
$script_logging_enabled = $false
## Create Logdir:
if(!(Test-Path -path "$script_log_dir")) {
New-Item -Path $script_log_dir -ItemType Directory | Out-Null
}
## Start Logging:
if($script_logging_enabled) {
Start-Transcript -Path $script_logfile -Append
}
## Create registry Location:
if(!(Test-Path -Path $script_registry_hive)) {
Write-Host "Creating registry hive: $script_registry_hive"
New-Item -Path $script_registry_hive | Out-Null
}
finally {
if($script_logging_enabled) {
Stop-Transcript
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment