<# .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 { param ( [Parameter(Mandatory=$false,Position=0)][System.Int32]$exitcode=0 ) if ($show -ne "qn") { Execpause } $logger.Add("execution time is $((Get-Date).Second-$tStart) seconds, exit") Set-Location -Path $dStart $host.SetShouldExit($exitcode) Exit } function Execerror { param ( [Parameter(Mandatory=$false,Position=0)][System.String]$message="" ) $logger.Add("error: $message") Execquite(1) } $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" Execquite(0) } else { Execerror("Restart this as Administrator!") }