$TargetFile = 'URL'
$ShortcutFile = "$env:Public\Desktop\shortcut.lnk"
$LocalIconFile = 'SOME IMAGE PATH HERE'
$LogFolder = 'C:\SomeLogFolder'
$LogType = 'ShortCutImageEdit'
#region FUNCTIONS
FUNCTION New-LogEntry {
<#
.SYNOPSIS
Creates and updates log file for script process.
.DESCRIPTION
Allows for script based logging to flat file.
.PARAMETER Message
String containing text information to log to file.
.PARAMETER Level
Level of message, can be any of the following. "Info", "Error", "Warn", "Start", "End"
.PARAMETER Path
Root folder location for log files.
.PARAMETER Log
Name of the log, usually script purpose. Example SQL-Deployment
.INPUTS
None. You cannot pipe objects to Add-Extension.
.OUTPUTS
Data is output to console using Write-Host as well as to flat file.
.EXAMPLE
PS> New-LogEntry -Message 'Beginning Execution' -Level 'Start'
.EXAMPLE
PS> New-LogEntry -Message "Executed script $File"
.EXAMPLE
PS> New-LogEntry -Message $Error[0] -Level 'Error'
.LINK
NA
#>
[CmdletBinding()]
PARAM
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$Message,
[Parameter(Mandatory = $false)]
[ValidateSet('Info', 'Error', 'Warn', 'Start', 'End')]
[string]$Level = 'Info',
[Parameter(Mandatory = $false)]
[System.IO.FileInfo]$Path = $LogFolder,
[Parameter(Mandatory = $false)]
[string]$Log = $LogType
)
BEGIN {
# Ensure log file exists, if not create it
$RunAs = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
$Today = Get-Date -Format 'yyyyMMdd_HHmm'
$Year = Get-Date -Format 'yyyy'
$Month = Get-Date -Format 'MM'
$FullLogPath = Join-Path -Path $Path -ChildPath "$Log\$Year\$Month\$Log-$Today.log"
IF (!(Test-Path -Path $FullLogPath)) {
New-Item -Path $FullLogPath -ItemType File -Force | Out-Null
Add-Content -Path $FullLogPath -Value '======================================================='
Add-Content -Path $FullLogPath -Value "File Created: [$Log]"
Add-Content -Path $FullLogPath -Value "File Date: [$Today]"
Add-Content -Path $FullLogPath -Value "Log Path: [$FullLogPath]"
Add-Content -Path $FullLogPath -Value "Server: [$env:COMPUTERNAME]"
Add-Content -Path $FullLogPath -Value "RunAs: [$RunAs]"
Add-Content -Path $FullLogPath -Value '======================================================='
Add-Content -Path $FullLogPath -Value ' '
}
}
PROCESS {
# Process supplied log data to file
$LogDate = Get-Date -UFormat '%x %r'
IF ($Level -eq 'Start') {
# Add a blank line to indicate new execution of script
Add-Content -Path $FullLogPath -Value ''
}
Add-Content -Path $FullLogPath -Value "[$Level][$LogDate] $Message"
Write-Host "[$Level][$LogDate] $Message"
}
END {
}
}
#endregion FUNCTIONS
TRY {
New-LogEntry -Message 'Script Start' -Level Start
IF (Test-Path -Path $ShortcutFile) {
IF (Test-Path -Path $LocalIconFile) {
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
$Shortcut.TargetPath = $TargetFile
$mypath = Split-Path -Parent $MyInvocation.MyCommand.Path
$shortcut.IconLocation = $LocalIconFile
$Shortcut.Save()
New-LogEntry -Message "Shortcut icon modified to $LocalIconFile on $ShortcutFile"
} ELSE {
# Icon file is missing
New-LogEntry -Message "Icon File Missing: $LocalIconFile" -Level Error
}
} ELSE {
# Log to file, shortcut path not found.
New-LogEntry -Message "Shortcut Missing: $ShortcutFile" -Level Error
}
New-LogEntry -Message 'Script Completed' -Level End
}
CATCH {
# Terminating error detected, log something like $Error[0]
New-LogEntry -Message $Error[0] -Level Error
}