Windows scripts update (#40107)
- Add install ChatGPT script. - Update turn on MDM script with some changes from the script above. I tested the turn on MDM script after making these updates.
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
$taskName = "Install ChatGPT Desktop"
|
||||
|
||||
# Encode the script as Base64, so we can use it with a scheduled task
|
||||
$encodedCommand = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes(@'
|
||||
# These variables need to be inside the script block
|
||||
$wingetPath = Get-Command winget.exe -ErrorAction SilentlyContinue
|
||||
$appName = "ChatGPT Desktop"
|
||||
|
||||
Write-Output "Please do not close this window."
|
||||
|
||||
if (-not $wingetPath) {
|
||||
Write-Output "Installing WinGet in order to install $appName..."
|
||||
$filePath = Join-Path $env:TEMP "winget.msixbundle"
|
||||
Invoke-WebRequest -Uri https://aka.ms/getwinget -OutFile $filePath
|
||||
Add-AppxPackage $filePath
|
||||
}
|
||||
|
||||
$args = @(
|
||||
"--id", "9NT1R1C2HH7J"
|
||||
"--source", "msstore"
|
||||
"--silent"
|
||||
"--accept-package-agreements"
|
||||
"--accept-source-agreements"
|
||||
)
|
||||
|
||||
Write-Output "`nInstalling $appName...`n"
|
||||
winget install $args
|
||||
'@))
|
||||
|
||||
|
||||
# Pop up at the top, shows a PowerShell window
|
||||
$action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -EncodedCommand $encodedCommand"
|
||||
|
||||
# `EndBoundary` to automatically delete the task with `DeleteExpiredTaskAfter` below
|
||||
$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date)
|
||||
$trigger.EndBoundary = (Get-Date).AddSeconds(5).ToString("s")
|
||||
|
||||
$currentUser = (Get-CimInstance -ClassName Win32_ComputerSystem).UserName
|
||||
|
||||
# Use `-RunLevel Highest` here so that `Unregister-ScheduledTask` will work later; otherwise it fails with a `PermissionDenied` error
|
||||
$principal = New-ScheduledTaskPrincipal -UserId $currentUser -RunLevel Highest
|
||||
|
||||
# `ExecutionTimeLimit` in case it hangs
|
||||
$settings = New-ScheduledTaskSettingsSet -RunOnlyIfNetworkAvailable -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -ExecutionTimeLimit (New-TimeSpan -Minutes 10) -DeleteExpiredTaskAfter (New-TimeSpan -Seconds 5)
|
||||
|
||||
$task = New-ScheduledTask -Action $action -Trigger $trigger -Principal $principal -Settings $settings
|
||||
|
||||
# If a task already has this name, delete it first
|
||||
if ((Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue)) {
|
||||
Unregister-ScheduledTask -TaskName "$taskName" -Confirm:$false
|
||||
}
|
||||
|
||||
Write-Host "Logged in user is $currentUser."
|
||||
Write-Host "Starting ScheduledTask."
|
||||
|
||||
# Register and start task
|
||||
Register-ScheduledTask "$taskName" -InputObject $task
|
||||
Start-ScheduledTask -TaskName "$taskName"
|
||||
@@ -1,4 +1,5 @@
|
||||
$scriptBlock = @'
|
||||
# Encode the script as Base64, so we can use it with a scheduled task
|
||||
$encodedCommand = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes(@'
|
||||
# Locate the MDM Enrollment Key in the registry
|
||||
$enrollmentKey = Get-Item -Path HKLM:\SOFTWARE\Microsoft\Enrollments\* | Get-ItemProperty | Where-Object {$_.ProviderID -eq 'Fleet'} | Where-Object {$_.EnrollmentState -match '1|6|13'}
|
||||
|
||||
@@ -16,8 +17,8 @@ if ($isMDMTurnedOn) {
|
||||
Unregister-ScheduledTask -TaskName "$taskName" -Confirm:$false
|
||||
Start-Sleep -Seconds 10
|
||||
} else {
|
||||
$Title = "Migrate to Fleet"
|
||||
$Message = "Mobile device management is off. MDM allows your organization to change settings and install software.
|
||||
$title = "Migrate to Fleet"
|
||||
$message = "Mobile device management is off. MDM allows your organization to change settings and install software.
|
||||
|
||||
Turn on MDM by following these steps:
|
||||
|
||||
@@ -29,34 +30,29 @@ Open Fleet Desktop (Fleet icon) in your system tray (^) and select **Refetch** o
|
||||
|
||||
This **Migrate to Fleet** window will pop up every 5 minutes until you finish."
|
||||
|
||||
# Send the message
|
||||
(New-Object -ComObject WScript.Shell).Popup($Message, 0, $Title, 0)
|
||||
# Send the message
|
||||
(New-Object -ComObject WScript.Shell).Popup($message, 0, $title, 0)
|
||||
}
|
||||
'@
|
||||
|
||||
# Encode the script as Base64, so we can use it with a scheduled task
|
||||
$bytes = [System.Text.Encoding]::Unicode.GetBytes($scriptBlock)
|
||||
$encodedCommand = [Convert]::ToBase64String($bytes)
|
||||
'@))
|
||||
|
||||
# Pop up at the top, shows a PowerShell window
|
||||
$action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -EncodedCommand $encodedCommand"
|
||||
|
||||
$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Minutes 5)
|
||||
$userID = Get-CimInstance -ClassName Win32_ComputerSystem | Select-Object -expand UserName
|
||||
$currentUser = (Get-CimInstance -ClassName Win32_ComputerSystem).UserName
|
||||
|
||||
# Use `-RunLevel Highest` here so that `Unregister-ScheduledTask` will work later; otherwise it fails with a `PermissionDenied` error
|
||||
$principal = New-ScheduledTaskPrincipal -UserId "$userID" -RunLevel Highest
|
||||
$principal = New-ScheduledTaskPrincipal -UserId "$currentUser" -RunLevel Highest
|
||||
|
||||
# `ExecutionTimeLimit` is used in case the user didn't close the popup, so that it will take focus again
|
||||
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -RunOnlyIfNetworkAvailable -ExecutionTimeLimit (New-TimeSpan -Minutes 4)
|
||||
$settings = New-ScheduledTaskSettingsSet -RunOnlyIfNetworkAvailable -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -ExecutionTimeLimit (New-TimeSpan -Minutes 4)
|
||||
|
||||
$task = New-ScheduledTask -Action $action -Trigger $trigger -Principal $principal -Settings $settings
|
||||
|
||||
Write-Host "Logged in user is $userID."
|
||||
Write-Host "Logged in user is $currentUser."
|
||||
Write-Host "Starting ScheduledTask."
|
||||
|
||||
# Register and start task
|
||||
$taskName = "Turn on MDM notification"
|
||||
Register-ScheduledTask "$taskName" -InputObject $task
|
||||
Start-ScheduledTask -TaskName "$taskName"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user