This commit is contained in:
Freddy Kristiansen 2020-02-19 17:10:30 +01:00
Родитель 8197f4a2f6
Коммит a592d8917a
2 изменённых файлов: 40 добавлений и 27 удалений

Просмотреть файл

@ -191,29 +191,42 @@ $task = Register-ScheduledTask -TaskName $taskName `
-User $vmadminUsername `
-Password $plainPassword
$startupAction = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -WindowStyle Hidden -ExecutionPolicy UnRestricted -File c:\demo\SetupVm.ps1"
$startupTrigger = New-ScheduledTaskTrigger -AtStartup
$startupTrigger.Delay = "PT1M"
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -RunOnlyIfNetworkAvailable -DontStopOnIdleEnd
Register-ScheduledTask -TaskName "SetupVm" `
-Action $startupAction `
-Trigger $startupTrigger `
-Settings $settings `
-RunLevel "Highest" `
-User $vmAdminUsername `
-Password $plainPassword | Out-Null
if ($WindowsInstallationType -eq "Server") {
Log "Restarting computer and start SetupVm"
Shutdown -r -t 60
Log "Launch SetupVm"
$onceAction = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -WindowStyle Hidden -ExecutionPolicy UnRestricted -File c:\demo\setupVm.ps1"
Register-ScheduledTask -TaskName SetupVm `
-Action $onceAction `
-RunLevel Highest `
-User $vmAdminUsername `
-Password $plainPassword | Out-Null
if (Get-ScheduledTask -TaskName SetupVm -ErrorAction Ignore) {
schtasks /DELETE /TN SetupVm /F | Out-Null
}
Start-ScheduledTask -TaskName SetupVm
Log "Launch SetupVm"
$onceAction = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -WindowStyle Hidden -ExecutionPolicy UnRestricted -File c:\demo\setupVm.ps1"
Register-ScheduledTask -TaskName SetupVm `
-Action $onceAction `
-RunLevel Highest `
-User $vmAdminUsername `
-Password $plainPassword | Out-Null
Start-ScheduledTask -TaskName SetupVm
}
else {
if (Get-ScheduledTask -TaskName SetupStart -ErrorAction Ignore) {
schtasks /DELETE /TN SetupStart /F | Out-Null
}
$startupAction = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -WindowStyle Hidden -ExecutionPolicy UnRestricted -File c:\demo\SetupVm.ps1"
$startupTrigger = New-ScheduledTaskTrigger -AtStartup
$startupTrigger.Delay = "PT1M"
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -RunOnlyIfNetworkAvailable -DontStopOnIdleEnd
Register-ScheduledTask -TaskName "SetupVm" `
-Action $startupAction `
-Trigger $startupTrigger `
-Settings $settings `
-RunLevel "Highest" `
-User $vmAdminUsername `
-Password $plainPassword | Out-Null
Log "Restarting computer and start SetupVm"
Shutdown -r -t 60
}

Просмотреть файл

@ -7,10 +7,6 @@ $WindowsProductName = $ComputerInfo.WindowsProductName
try {
if (Get-ScheduledTask -TaskName SetupVm -ErrorAction Ignore) {
schtasks /DELETE /TN SetupVm /F | Out-Null
}
function Log([string]$line, [string]$color = "Gray") {
("<font color=""$color"">" + [DateTime]::Now.ToString([System.Globalization.DateTimeFormatInfo]::CurrentInfo.ShortTimePattern.replace(":mm",":mm:ss")) + " $line</font>") | Add-Content -Path "c:\demo\status.txt"
}
@ -299,6 +295,10 @@ if (Get-ScheduledTask -TaskName SetupStart -ErrorAction Ignore) {
schtasks /DELETE /TN SetupStart /F | Out-Null
}
if (Get-ScheduledTask -TaskName SetupVm -ErrorAction Ignore) {
schtasks /DELETE /TN SetupVm /F | Out-Null
}
if ($RunWindowsUpdate -eq "Yes") {
Log "Installing Windows Updates"
install-module PSWindowsUpdate -force