forked from bcdady/MyScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNew-ScheduledScript.ps1
182 lines (172 loc) · 6.92 KB
/
New-ScheduledScript.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<#
.SYNOPSIS
This script creates a scheduled task on a local or remote system to execute a Powershell script
based on a number of criteria. It is useful if you have a central server that you create multiple
different scheduled tasks on.
.NOTES
Created on: 1/20/15
Created by: Adam Bertram
Filename: New-ScheduledScript.ps1
Credits: http://blogs.technet.com/b/heyscriptingguy/archive/2015/01/16/use-powershell-to-create-scheduled-task-in-new-folder.aspx
.EXAMPLE
PS> New-ScheduledScript.ps1 -ScriptFilePath C:\callfromvbs.ps1 -LocalScriptFolderPath 'C:\' -TaskTriggerOptions @{'Daily' = $true; 'At' = '3Am'} -TaskName 'Test' -TaskRunAsUser 'lab.local\administrator' -TaskRunAsPassword 'mypassword' -Computername labdc.lab.local
This script would copy the C:\callfromvbs.ps1 to the C:\ of labdc.lab.local. It would then create a scheduled task on labdc.lab.local
named Test that ran daily at 3AM under the lab.local\administrator credentials.
.PARAMETER ScriptFilePath
The remote or local file path of the Powershell script
.PARAMETER ScriptParameters
Any parameters to execute with the script
.PARAMETER LocalScriptFolderPath
If this script is copying a Powershell script from somewhere else, this is the folder path where the
script will be copied to and referenced to run in the scheduled task.
.PARAMETER TaskTriggerOptions
A hashtable of parameters that will be passed to the New-ScheduledTaskTrigger cmdlet. For available options, visit
http://technet.microsoft.com/en-us/library/jj649821.aspx.
.PARAMETER TaskName
The name of the scheduled task
.PARAMETER TaskRunAsUser
The username that the task will run under
.PARAMETER TaskRunAsPassword
The password to the runas user
.PARAMETER Computername
The name of the system in which the scheduled task will be created (if not the local system)
.PARAMETER PowershellRunAsArch
If your task scheduler machine is 64 bit this enforces the script to run under the 32 bit or 64 bit
Powershell host. By default, it will always run as 64 bit.
.LINK
https://gallery.technet.microsoft.com/New-ScheduledScript-A-398a654f
#>
[CmdletBinding()]
[OutputType([bool])]
param (
[Parameter(Mandatory,
ValueFromPipeline,
ValueFromPipelineByPropertyName)]
[ValidatePattern('.*\.ps1$')]
[string]$ScriptFilePath,
[string]$ScriptParameters,
[Parameter(Mandatory)]
[string]$LocalScriptFolderPath,
[Parameter(Mandatory)]
[hashtable]$TaskTriggerOptions,
[Parameter(Mandatory)]
[string]$TaskName,
[Parameter(Mandatory)]
[string]$TaskRunAsUser,
[Parameter(Mandatory)]
[string]$TaskRunAsPassword,
[ValidateScript({ Test-Connection -ComputerName $_ -Quiet -Count 1})]
[string]$Computername = 'localhost',
[ValidateSet('x86', 'x64')]
[string]$PowershellRunAsArch
)
begin {
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest
## http://www.leeholmes.com/blog/2009/11/20/testing-for-powershell-remoting-test-psremoting/
function Test-PsRemoting {
param (
[Parameter(Mandatory)]
$computername
)
try {
$errorActionPreference = "Stop"
$result = Invoke-Command -ComputerName $computername { 1 }
} catch {
return $false
}
## I’ve never seen this happen, but if you want to be
## thorough….
if ($result -ne 1) {
Write-Verbose "Remoting to $computerName returned an unexpected result."
return $false
}
$true
}
function Get-ComputerArchitecture {
if ((Get-CimInstance -ComputerName $Computername Win32_ComputerSystem -Property SystemType).SystemType -eq 'x64-based PC') {
'x64'
} else {
'x86'
}
}
function Get-PowershellFilePath {
## If user wants to run the script under the x86 host and the machine is x64
if (($PowershellRunAsArch -eq 'x86') -and ((Get-ComputerArchitecture) -eq 'x64')) {
if ($Computername -eq 'localhost') {
## If it's the localhost no need to do a WinRM query
"$($PsHome.Replace('System32','SysWow64'))\powershell.exe"
} else {
Invoke-Command -ComputerName $Computername -ScriptBlock { "$($PsHome.Replace('System32','SysWow64'))\powershell.exe" }
}
} else {
## If the machine is 32 bit anyway or if it's 64 bit and the user wants 64 bit just use $PsHome
if ($Computername -eq 'localhost') {
## If it's the localhost no need to do a WinRM query
"$PsHome\powershell.exe"
} else {
Invoke-Command -ComputerName $Computername -ScriptBlock { "$PsHome\powershell.exe" }
}
}
}
function New-MyScheduledTask {
try {
$PowershellFilePath = Get-PowershellFilePath
if ($Computername -ne 'localhost') {
$ScriptBlock = {
$Action = New-ScheduledTaskAction -Execute $using:PowershellFilePath -Argument "-NonInteractive -NoLogo -NoProfile -File $using:ScriptFilePath $using:ScriptParameters"
$TaskTriggerOptions = $using:TaskTriggerOptions
$Trigger = New-ScheduledTaskTrigger @TaskTriggerOptions
$RunAsUser = $using:TaskRunAsUser
$Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings (New-ScheduledTaskSettingsSet);
Register-ScheduledTask -TaskName $using:TaskName -InputObject $Task -User $using:TaskRunAsUser -Password $using:TaskRunAsPassword
}
$Params = @{
'Scriptblock' = $ScriptBlock
'Computername' = $Computername
}
Invoke-Command @Params
} else {
$Action = New-ScheduledTaskAction -Execute $PowershellFilePath -Argument "-NonInteractive -NoLogo -NoProfile -File $ScriptFilePath"
$Trigger = New-ScheduledTaskTrigger @TaskTriggerOptions
$Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings (New-ScheduledTaskSettingsSet);
Register-ScheduledTask -TaskName $TaskName -InputObject $Task -User $TaskRunAsUser -Password $TaskRunAsPassword
}
} catch {
Write-Error "Error: $($_.Exception.Message) - Line Number: $($_.InvocationInfo.ScriptLineNumber)"
$false
}
}
function Get-UncPath ($HostName,$LocalPath) {
$NewPath = $LocalPath -replace (":", "$")
if ($NewPath.EndsWith("\")) {
$NewPath = [Regex]::Replace($NewPath, "\\$", "")
}
"\\$HostName\$NewPath"
}
try {
if (($Computername -ne 'localhost') -and !(Test-PsRemoting -computername $Computername)) {
throw "PS remoting not available on the computer $Computername"
}
## Ensure there's no trailing slash
$LocalScriptFolderPath = $LocalScriptFolderPath.TrimEnd('\')
} catch {
Write-Error "Error: $($_.Exception.Message) - Line Number: $($_.InvocationInfo.ScriptLineNumber)"
exit
}
}
process {
try {
## If we're not creating a task on the local computer or if the script to execute is not on the local computer
## copy to the task scheduler system and change the script to execute to a local path
if ($ScriptFilePath.StartsWith('\\') -or ($Computername -ne 'localhost')) {
Copy-Item -Path $ScriptFilePath -Destination (Get-UncPath -HostName $Computername -LocalPath $LocalScriptFolderPath)
$ScriptFilePath = "$LocalScriptFolderPath\$($ScriptFilePath | Split-Path -Leaf)"
}
New-MyScheduledTask | Out-Null
$true
} catch {
Write-Error "Error: $($_.Exception.Message) - Line Number: $($_.InvocationInfo.ScriptLineNumber)"
$false
}
}