Scripts/System.ps1
function IsElevated() { <# .SYNOPSIS Checks whether the executing script is elevated which means the script has admin privileges. #> $id = [System.Security.Principal.WindowsIdentity]::GetCurrent() $p = New-Object System.Security.Principal.WindowsPrincipal($id) return $p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator) } function GetReadableDateAndTime() { <# .SYNOPSIS Gets the current date and time in format "yyyy.MM.dd HH:mm:ss". #> return Get-Date -Format "yyyy.MM.dd HH:mm:ss" } function GetReadableTimeSpan([System.TimeSpan]$timeSpan) { <# .SYNOPSIS Returns the given timespan as a readable string. If the given timespan is negative it is regarded as zero. #> [int]$milliseconds = [int]$timeSpan.TotalMilliseconds if($milliseconds -lt 0) { $milliseconds = 0 } if($milliseconds -le 2000) { return "$milliseconds Millisecond(s)" } [int]$seconds = $milliseconds / 1000 if($seconds -le 120) { return "$seconds Seconds" } [int]$minutes = $seconds / 60 if($minutes -le 120) { return "$minutes Minutes" } [int]$hours = $minutes / 60 if($hours -le 48) { return "$hours Hours" } [int]$days = $hours / 24 return "$days Days" } function SetWindowTitle([string]$title) { <# .SYNOPSIS Sets the title of the window in which the script is running. #> $host.UI.RawUI.WindowTitle = $title } function StopProcessByName([string]$processName) { <# .SYNOPSIS Stops all instances of the given process. #> $processes = Get-Process -Name $processName -ErrorAction Ignore if($processes.Count -gt 0) { Write-Output "Process [$processName] has [$($processes.Count)] instances running. Try to stop them . . . " Stop-Process -InputObject $processes -Force $processes = Get-Process -Name $processName -ErrorAction Ignore $stillRunningProcesses = $processes | Where-Object { $_.HasExited -eq $false } $count = $stillRunningProcesses.Count if($count -gt 0) { Write-Output "Not all instances of process [$processName] were stopped." } else { Write-Output "Instances of process [$processName] were successfully stopped." } } else { Write-Output "Instances of process [$processName] were not found." } } |