| 1 | param( |
| 2 | [string]$Root = "F:\" |
| 3 | ) |
| 4 | |
| 5 | $ErrorActionPreference = "Stop" |
| 6 | |
| 7 | function Copy-Provision { |
| 8 | param( |
| 9 | [string]$Src, |
| 10 | [string]$Dst, |
| 11 | [bool]$Extension |
| 12 | ) |
| 13 | |
| 14 | if (-not (Test-Path -LiteralPath $Src)) { |
| 15 | return |
| 16 | } |
| 17 | |
| 18 | New-Item -ItemType Directory -Path $Dst -Force | Out-Null |
| 19 | foreach ($item in Get-ChildItem -LiteralPath $Src) { |
| 20 | $dst = $Dst |
| 21 | if ($Extension) { |
| 22 | # `.ps1` extension is necessary for script otherwise Powershell returns an error. |
| 23 | $dst = Join-Path $Dst ("{0}.ps1" -f $item.BaseName) |
| 24 | } |
| 25 | Copy-Item -LiteralPath $item.FullName -Destination $dst |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | $provisionDir = "C:\ProgramData\Lima\provision" |
| 30 | New-Item -ItemType Directory -Path $provisionDir -Force | Out-Null |
| 31 | |
| 32 | # Copy startup runner and System scripts |
| 33 | $startupSystemRunnerSrc = Join-Path $Root "startup_runner.ps1" |
| 34 | $startupSystemRunnerDst = Join-Path $provisionDir "startup_runner.ps1" |
| 35 | Copy-Item -LiteralPath $startupSystemRunnerSrc -Destination $startupSystemRunnerDst |
| 36 | |
| 37 | $startupSystemScriptsSrc = Join-Path $Root "provision.system" |
| 38 | $startupSystemScriptsDst = Join-Path $provisionDir "provision.system" |
| 39 | Copy-Provision -Src $startupSystemScriptsSrc -Dst $startupSystemScriptsDst -Extension |
| 40 | |
| 41 | # Copy startup User scripts |
| 42 | $startupUserScriptsSrc = Join-Path $Root "provision.user" |
| 43 | $startupUserScriptsDst = Join-Path $provisionDir "provision.user" |
| 44 | Copy-Provision -Src $startupUserScriptsSrc -Dst $startupUserScriptsDst -Extension |
| 45 | |
| 46 | # Copy dependency scripts |
| 47 | $startupDependencyScriptsSrc = Join-Path $Root "provision.dependency" |
| 48 | $startupDependencyScriptsDst = Join-Path $provisionDir "provision.dependency" |
| 49 | Copy-Provision -Src $startupDependencyScriptsSrc -Dst $startupDependencyScriptsDst -Extension |
| 50 | |
| 51 | |
| 52 | # Copy data files |
| 53 | $startupDataFilesSrc = Join-Path $Root "provision.data" |
| 54 | $startupDataFilesDst = Join-Path $provisionDir "provision.data" |
| 55 | Copy-Provision -Src $startupDataFilesSrc -Dst $startupDataFilesDst |
| 56 | |
| 57 | |
| 58 | # Set startup System task |
| 59 | $startupTrigger = New-ScheduledTaskTrigger -AtStartup |
| 60 | $startupActionSystem = New-ScheduledTaskAction -Execute "powershell.exe" -Argument ("-NoProfile -NonInteractive -ExecutionPolicy Bypass -File $startupSystemRunnerDst -Mode system") |
| 61 | $startupPrincipalSystem = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest |
| 62 | Register-ScheduledTask -TaskName "LimaProvisionStartupSystem" -TaskPath "\Lima\" -Action $startupActionSystem -Trigger $startupTrigger -Principal $startupPrincipalSystem -Force | Out-Null |
| 63 | |
| 64 | # Set startup User task |
| 65 | $startupActionUser = New-ScheduledTaskAction -Execute "powershell.exe" -Argument ("-NoProfile -NonInteractive -ExecutionPolicy Bypass -File $startupSystemRunnerDst -Mode user") |
| 66 | $startupPrincipalUser = New-ScheduledTaskPrincipal -UserId "{{.User}}" -LogonType S4U -RunLevel Limited |
| 67 | Register-ScheduledTask -TaskName "LimaProvisionStartupUser" -TaskPath "\Lima\" -Action $startupActionUser -Trigger $startupTrigger -Principal $startupPrincipalUser -Force | Out-Null |