1. 程式人生 > 其它 >powershell建立計劃任務

powershell建立計劃任務

function Get-ScriptDirectory
{
	Split-Path -Parent $PSCommandPath
}

function Check-IsElevated
{
	$id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
	$p = New-Object System.Security.Principal.WindowsPrincipal($id)
	if ($p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator))
	{
		return $true
		}
	else
	{
		return $false
	}
}

$pt = Get-ScriptDirectory
$arg = "-exec bypass -WindowStyle Hidden -file " + $PSCommandPath

if(-not (Check-IsElevated))
{
		Start-Process "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"  -ArgumentList $arg -Verb runAs -Wait
		return
}
$diablerecover = Join-Path -Path $pt -ChildPath "\file\diablerecover.bat"
Start-Process "C:\Windows\System32\reg.exe" -ArgumentList "ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f" -WindowStyle Hidden
Start-Process $diablerecover -WindowStyle Hidden
$Evcemqx = Get-Service | Where-Object { $_.Name -eq "emqx" }
if ($Evcemqx.Length -eq 0)
{
	$emqxcmd = Join-Path -Path $pt -ChildPath "emqx\bin\emqx.cmd"
	Start-Process $emqxcmd "install" -Wait -WindowStyle Hidden
	Set-Service emqx -StartupType Automatic
	Start-Service emqx
}
$MqttDir = Join-Path -Path $pt -ChildPath "Release"
$Mqtt = Join-Path -Path $pt -ChildPath "\file\autorun.bat"
$startvm = Join-Path -Path $pt -ChildPath "\file\startvm.bat"

$User = [Security.Principal.WindowsIdentity]::GetCurrent()

$Scheduler = New-Object -ComObject Schedule.Service

$Scheduler.Connect($env:COMPUTERNAME)

$Task = $Scheduler.NewTask(0)

[string]$TaskName = "StartHook"

$RegistrationInfo = $Task.RegistrationInfo

$RegistrationInfo.Description = $TaskName

$RegistrationInfo.Author = $User.Name



$Action = $Task.Actions.Create(0)

$Action.Path = $Mqtt
$Action.WorkingDirectory = $MqttDir


$Triggers = $Task.Triggers.Create(9)


$Settings = $Task.Settings


$Settings.Enabled = $True

$Settings.StartWhenAvailable = $True

$Settings.Hidden = $False


$Task.Principal.RunLevel = 1



$RootFolder = $Scheduler.GetFolder("\")

$RootFolder.RegisterTaskDefinition($TaskName, $Task, 6, $Null, $Null, 3)




$Task1 = $Scheduler.NewTask(0)

[string]$TaskName1 = "StartVM"

$RegistrationInfo = $Task1.RegistrationInfo

$RegistrationInfo.Description = $TaskName1

$RegistrationInfo.Author = $User.Name



$Action = $Task1.Actions.Create(0)

$Action.Path = $startvm



$Triggers = $Task1.Triggers.Create(9)


$Settings = $Task1.Settings


$Settings.Enabled = $True

$Settings.StartWhenAvailable = $True

$Settings.Hidden = $False


$Task.Principal.RunLevel = 1



$RootFolder = $Scheduler.GetFolder("\")

$RootFolder.RegisterTaskDefinition($TaskName1, $Task1, 6, $Null, $Null, 3)


function Get-ScriptDirectory
{
	Split-Path -Parent $PSCommandPath
}

function Check-IsElevated
{
	$id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
	$p = New-Object System.Security.Principal.WindowsPrincipal($id)
	if ($p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator))
	{
		return $true
		}
	else
	{
		return $false
	}
}

$pt = Get-ScriptDirectory
$arg = "-exec bypass -WindowStyle Hidden -file " + $PSCommandPath

if(-not (Check-IsElevated))
{
		Start-Process "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"  -ArgumentList $arg -Verb runAs -Wait
		return
}
$Evcemqx = Get-Service | Where-Object { $_.Name -eq "emqx" }
if ($Evcemqx.Length -gt 0)
{
	$emqxcmd = Join-Path -Path $pt -ChildPath "emqx\bin\emqx.cmd"
	Start-Process $emqxcmd "uninstall" -Wait -WindowStyle Hidden
}
$User = [Security.Principal.WindowsIdentity]::GetCurrent()

$Scheduler = New-Object -ComObject Schedule.Service

$Scheduler.Connect($env:COMPUTERNAME)

$Task = $Scheduler.NewTask(0)

[string]$TaskName = "StartHook"

[string]$TaskName1 = "StartVM"

$RootFolder = $Scheduler.GetFolder("\")

try
{
	
	$Task = $RootFolder.GetTask($TaskName)
	$RootFolder.DeleteTask($TaskName, 0)
}

catch [System.Exception] {
}
catch
{
}
try
{	
	$Task = $RootFolder.GetTask($TaskName1)	
	$RootFolder.DeleteTask($TaskName1, 0)
}

catch [System.Exception] {
}
catch
{
}