Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function Test-Admin {
- $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
- $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
- }
- If((Test-Admin) -eq $false) {
- Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
- Exit
- }
- $PSScriptName = Split-Path -Path $MyInvocation.MyCommand.Definition -Leaf
- $PSScriptFullPath = $MyInvocation.MyCommand.Definition
- $Scriptpath = Split-Path $MyInvocation.MyCommand.Path -Parent
- $ScriptName = $([io.path]::GetFileNameWithoutExtension($PSScriptName))
- $TaskName = $ScriptName
- $TaskExists = Get-ScheduledTask -TaskName $TaskName -ErrorAction "SilentlyContinue"
- If(-Not $TaskExists) {
- $Trigger = New-ScheduledTaskTrigger -AtLogOn
- $User = "NT AUTHORITY\SYSTEM"
- $Action = New-ScheduledTaskAction -Execute "Powershell.exe" -Argument "-Executionpolicy bypass -Noninteractive -File `"$PSScriptFullPath`""
- Register-ScheduledTask -TaskName $TaskName -Trigger $Trigger -User $User -Action $Action -RunLevel "Highest" –Force | Out-Null
- Add-Content -Path "C:\Temp\whoami" -Value "$(whoami)"
- Restart-Computer -Force
- }
- Else {
- Add-Content -Path "C:\Temp\whoami" -Value "$(whoami)"
- Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false
- Remove-Item -Path $PSScriptFullPath -Force
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement