我是靠谱客的博主 安静樱桃,最近开发中收集的这篇文章主要介绍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:WindowsSystem32WindowsPowerShellv1.0powershell.exe"  -ArgumentList $arg -Verb runAs -Wait
		return
}
$diablerecover = Join-Path -Path $pt -ChildPath "filediablerecover.bat"
Start-Process "C:WindowsSystem32reg.exe" -ArgumentList "ADD HKLMSOFTWAREMicrosoftWindowsCurrentVersionPoliciesSystem /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 "emqxbinemqx.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 "fileautorun.bat"
$startvm = Join-Path -Path $pt -ChildPath "filestartvm.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:WindowsSystem32WindowsPowerShellv1.0powershell.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 "emqxbinemqx.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
{
}



最后

以上就是安静樱桃为你收集整理的powershell创建计划任务的全部内容,希望文章能够帮你解决powershell创建计划任务所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部