From 16491c9b02aef19246ee5c90bb98e98d913484c3 Mon Sep 17 00:00:00 2001 From: "pavel.muhortov" Date: Sat, 7 Jan 2023 10:20:34 +0300 Subject: [PATCH] update template --- script.ps1 | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 script.ps1 diff --git a/script.ps1 b/script.ps1 new file mode 100644 index 0000000..62e94dc --- /dev/null +++ b/script.ps1 @@ -0,0 +1,120 @@ +<# +.SYNOPSIS + Checking privileged rights + +.DESCRIPTION + Returning current username if privileged rights are exist + or + returning error, if privileged rights are not exist + +.PARAMETER show + "" - execution with pauses. + "qn" - execution without pauses. + +.PARAMETER conf + Path to configuration file. ".\script.conf" is the default. + +.EXAMPLE + .\script.ps1 + +.EXAMPLE + .\script.ps1 qn .\script.conf +#> + + +param ( + [Parameter(Mandatory=$false,Position=0)][System.String]$show="", + [Parameter(Mandatory=$false,Position=1)][System.String]$conf="" +) + + +class Config { + [System.String] $path + [System.String] $logs + + Config([System.String]$confpath) { + $this.path = $confpath + $this.Read() + } + + [System.Void] hidden Read() { + $ErrorActionPreference = 'SilentlyContinue' + Try{ + $this.logs = (Get-Content $this.path | Where-Object {$_ -like "logs=*"}).Split("=")[1] + } + Catch{ + $this.logs = $null + } + $ErrorActionPreference = 'Continue' + } +} + +class Logger { + [System.String] $path + + Logger([System.String]$logspath) { + $this.path = $logspath + $ErrorActionPreference = 'SilentlyContinue' + Try{ + if (!([string]::IsNullOrEmpty($this.path)) -and !(Test-Path -Path $this.path)) { + New-Item -Path $this.path -ItemType File + } + } + Catch{ + } + $ErrorActionPreference = 'Continue' + } + + [System.Void] Add ([System.String]$message) { + $timenow = (Get-Date).toString("yyyy.MM.dd-HH:mm:ss") + $ErrorActionPreference = 'SilentlyContinue' + try { + Add-Content $this.path -Value "$timenow $message" + } + catch { + } + finally { + Write-Host "$timenow $message" + } + $ErrorActionPreference = 'Continue' + } +} +function Execpause { + Write-Host 'Press any key to continue...' + $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown') +} +function Execquite { + if ($show -ne "qn") { + Execpause + } + $logger.Add("execution time is $((Get-Date).Second-$tStart) seconds, exit") + Set-Location -Path $dStart + Exit +} +function Execerror { + param ( + [Parameter(Mandatory=$false,Position=0)][System.String]$message="" + ) + $logger.Add("error: $message") + Execquite +} + + +$tStart = $(Get-Date).Second +$dStart = $(Get-Location) +Set-Location -Path $PSScriptRoot +if ([string]::IsNullOrEmpty($conf)) { + $conf = $PSCommandPath.Replace('.ps1','.conf') +} +$config = New-Object Config -ArgumentList $conf +$logger = New-Object Logger -ArgumentList $config."logs" + + +$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent()) +if ($currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { + Write-Host "Running as $env:USERNAME" +} +else { + Execerror("Restart this as Administrator!") +} +Execquite