Restructured Documentation
Automatic Documentation Deployment / Sync Docs to https://kb.bunny-lab.io (push) Successful in 8s
Automatic Documentation Deployment / Sync Docs to https://kb.bunny-lab.io (push) Successful in 8s
This commit is contained in:
@@ -0,0 +1,335 @@
|
||||
---
|
||||
tags:
|
||||
- PowerShell
|
||||
- Scripting
|
||||
---
|
||||
|
||||
## Purpose
|
||||
This script is designed to iterate over every computer device within an Active Directory Domain. It then reaches out to those devices over the network and iterates upon every local user profile on those devices, and using CIM, determines which profiles have not been logged into in X number of days. If executed in a non-dry-run nature, it will then delete those profiles (*this does not delete local or domain users, it just cleans up their local profile data on the workstation*).
|
||||
|
||||
!!! note "Windows Servers not Targeted"
|
||||
For safety, this script is designed to not target servers. There is no telling the potential turmoil of clearing profiles in server environments, and to avoid that risk all-together, we just avoid them entirely.
|
||||
|
||||
!!! example "Commandline Arguments"
|
||||
You can execute the script with the following arguments to change the behavior of the script: `.\UserProfileDataPruner.ps1`
|
||||
|
||||
- `-DryRun` : Do not delete local profile data, just report on what (*would*) be deleted.
|
||||
- `-InactiveDays 90` : Adjust the threshold of the pruning cutoff. (*Default = 90 Days*)
|
||||
- `-PilotTestingDevices` : Optional comma-separated list of devices to target (*instead of all eligible workstations in Active Directory*)
|
||||
|
||||
### Script
|
||||
You can find the full script below, save it as `UserProfileDataPruner.ps1`:
|
||||
|
||||
```powershell
|
||||
<#
|
||||
UserProfileDataPruner.ps1
|
||||
Prune stale local user profile data on Windows workstations.
|
||||
|
||||
- Deletes only on-disk profile data via Win32_UserProfile.Delete()
|
||||
- Never deletes user accounts
|
||||
- Skips servers (ProductType != 1)
|
||||
- Parameters:
|
||||
-DryRun -> shows [DRY-RUN]: lines; no deletions
|
||||
-InactiveDays [int] -> default 90
|
||||
-PilotTestingDevices -> optional comma-separated list or string[]; if omitted, auto-discovers all enabled Windows workstations in AD
|
||||
|
||||
Output:
|
||||
[INFO]: Total Hosts Queried: <N>
|
||||
[INFO]: Skipped host(s) due to WinRM/Connectivity/TimeDifference/SPN issues: <N>
|
||||
[DRY-RUN]/[INFO]: Deleting X profile(s) on "<HOST>" (user1,user2,...) # per host
|
||||
<ASCII summary table at the bottom>
|
||||
#>
|
||||
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[switch]$DryRun,
|
||||
[int]$InactiveDays = 90,
|
||||
[string[]]$PilotTestingDevices
|
||||
)
|
||||
|
||||
begin {
|
||||
function Write-Info([string]$msg){ Write-Host "[INFO]: $msg" }
|
||||
function Write-Dry ([string]$msg){ Write-Host "[DRY-RUN]: $msg" }
|
||||
|
||||
function Convert-LastUse {
|
||||
param([object]$raw)
|
||||
if ($null -eq $raw) { return $null }
|
||||
if ($raw -is [datetime]) {
|
||||
$dt = [datetime]$raw
|
||||
if ($dt.Kind -eq [System.DateTimeKind]::Utc) { return $dt } else { return $dt.ToUniversalTime() }
|
||||
}
|
||||
if ($raw -is [int64] -or $raw -is [uint64] -or $raw -is [int] -or ($raw -is [string] -and $raw -match '^\d+$')) {
|
||||
try { return [DateTime]::FromFileTimeUtc([int64]$raw) } catch { return $null }
|
||||
}
|
||||
if ($raw -is [string] -and $raw -match '^\d{14}\.\d{6}[-+]\d{3}$') {
|
||||
try { $d = [System.Management.ManagementDateTimeConverter]::ToDateTime($raw); return $d.ToUniversalTime() }
|
||||
catch { return $null }
|
||||
}
|
||||
if ($raw -is [string]) {
|
||||
$tmp = $null
|
||||
if ([DateTime]::TryParse($raw, [ref]$tmp)) { return $tmp.ToUniversalTime() }
|
||||
}
|
||||
return $null
|
||||
}
|
||||
|
||||
function Try-TranslateSid($sid) {
|
||||
try {
|
||||
(New-Object System.Security.Principal.SecurityIdentifier($sid)).Translate([System.Security.Principal.NTAccount]).Value
|
||||
} catch { $null }
|
||||
}
|
||||
|
||||
function Test-HostOnline {
|
||||
param([string]$Computer)
|
||||
try { Test-WSMan -ComputerName $Computer -ErrorAction Stop | Out-Null; return $true }
|
||||
catch { return $false }
|
||||
}
|
||||
|
||||
function Show-AsciiTable {
|
||||
param([hashtable]$Data)
|
||||
# Keep order if [ordered] was used
|
||||
$keys = @($Data.Keys)
|
||||
$values = $keys | ForEach-Object { [string]$Data[$_] }
|
||||
$wKey = [Math]::Max(4, ($keys | ForEach-Object { $_.ToString().Length } | Measure-Object -Maximum).Maximum)
|
||||
$wVal = [Math]::Max(5, ($values | ForEach-Object { $_.ToString().Length } | Measure-Object -Maximum).Maximum)
|
||||
$sep = '+' + ('-'*($wKey+2)) + '+' + ('-'*($wVal+2)) + '+'
|
||||
Write-Host $sep
|
||||
foreach ($k in $keys) {
|
||||
$v = [string]$Data[$k]
|
||||
Write-Host ('| {0} | {1} |' -f $k.PadRight($wKey), $v.PadRight($wVal))
|
||||
}
|
||||
Write-Host $sep
|
||||
}
|
||||
|
||||
# Normalize any comma-separated single string into an array
|
||||
if ($PilotTestingDevices -and $PilotTestingDevices.Count -eq 1 -and $PilotTestingDevices[0] -match ',') {
|
||||
$PilotTestingDevices = $PilotTestingDevices[0].Split(',') | ForEach-Object { $_.Trim() } | Where-Object { $_ }
|
||||
}
|
||||
|
||||
# Targets: use provided list, else discover workstations from AD
|
||||
$Targets = @()
|
||||
if ($PilotTestingDevices -and ($PilotTestingDevices | Where-Object { -not [string]::IsNullOrWhiteSpace($_) })) {
|
||||
$Targets = $PilotTestingDevices | ForEach-Object { $_.Trim() } | Where-Object { $_ } | Select-Object -Unique
|
||||
} else {
|
||||
try { Import-Module ActiveDirectory -ErrorAction Stop }
|
||||
catch { throw "ActiveDirectory module not found. Install RSAT or specify -PilotTestingDevices." }
|
||||
|
||||
# Discover enabled Windows workstations (exclude servers)
|
||||
$ad = Get-ADComputer -Filter * -Properties OperatingSystem, DNSHostName, Enabled
|
||||
$Targets = $ad |
|
||||
Where-Object {
|
||||
$_.Enabled -and $_.DNSHostName -and
|
||||
($_.OperatingSystem -like 'Windows*') -and
|
||||
($_.OperatingSystem -notmatch 'Server')
|
||||
} |
|
||||
Select-Object -ExpandProperty DNSHostName
|
||||
}
|
||||
|
||||
if (-not $Targets -or $Targets.Count -eq 0) { throw "No eligible Windows workstations to query." }
|
||||
|
||||
$CutoffUtc = [DateTime]::UtcNow.AddDays(-$InactiveDays)
|
||||
$Throttle = 25
|
||||
|
||||
# ---------- Remote blocks ----------
|
||||
|
||||
$RemoteEnumerateProfiles = {
|
||||
param([datetime]$CutoffUtc)
|
||||
|
||||
function Convert-LastUse {
|
||||
param([object]$raw)
|
||||
if ($null -eq $raw) { return $null }
|
||||
if ($raw -is [datetime]) {
|
||||
$dt = [datetime]$raw
|
||||
if ($dt.Kind -eq [System.DateTimeKind]::Utc) { return $dt } else { return $dt.ToUniversalTime() }
|
||||
}
|
||||
if ($raw -is [int64] -or $raw -is [uint64] -or $raw -is [int] -or ($raw -is [string] -and $raw -match '^\d+$')) {
|
||||
try { return [DateTime]::FromFileTimeUtc([int64]$raw) } catch { return $null }
|
||||
}
|
||||
if ($raw -is [string] -and $raw -match '^\d{14}\.\d{6}[-+]\d{3}$') {
|
||||
try { $d = [System.Management.ManagementDateTimeConverter]::ToDateTime($raw); return $d.ToUniversalTime() }
|
||||
catch { return $null }
|
||||
}
|
||||
if ($raw -is [string]) {
|
||||
$tmp = $null
|
||||
if ([DateTime]::TryParse($raw, [ref]$tmp)) { return $tmp.ToUniversalTime() }
|
||||
}
|
||||
return $null
|
||||
}
|
||||
|
||||
function Try-TranslateSid($sid) {
|
||||
try { (New-Object System.Security.Principal.SecurityIdentifier($sid)).Translate([System.Security.Principal.NTAccount]).Value }
|
||||
catch { $null }
|
||||
}
|
||||
|
||||
# Skip non-workstations
|
||||
try {
|
||||
$hasCIM = [bool](Get-Command -Name Get-CimInstance -ErrorAction SilentlyContinue)
|
||||
$os = if ($hasCIM) { Get-CimInstance Win32_OperatingSystem -ErrorAction Stop }
|
||||
else { Get-WmiObject Win32_OperatingSystem -ErrorAction Stop }
|
||||
if ($os.ProductType -ne 1) { return } # not a workstation
|
||||
} catch { return }
|
||||
|
||||
try {
|
||||
$hasCIM = [bool](Get-Command -Name Get-CimInstance -ErrorAction SilentlyContinue)
|
||||
$profiles = if ($hasCIM) {
|
||||
Get-CimInstance -ClassName Win32_UserProfile -ErrorAction Stop
|
||||
} else {
|
||||
Get-WmiObject -Class Win32_UserProfile -ErrorAction Stop
|
||||
}
|
||||
|
||||
$profiles = $profiles | Where-Object {
|
||||
$_.Special -eq $false -and
|
||||
$_.Loaded -eq $false -and
|
||||
$_.LocalPath -like 'C:\Users\*'
|
||||
}
|
||||
|
||||
foreach ($p in $profiles) {
|
||||
$sid = $p.SID
|
||||
$nameGuess = Split-Path $p.LocalPath -Leaf
|
||||
$acc = Try-TranslateSid $sid
|
||||
$accName = if ($acc -and ($acc -like '*\*')) { ($acc -split '\\',2)[1] } else { $nameGuess }
|
||||
$luUtc = Convert-LastUse ($p.PSObject.Properties['LastUseTime'].Value)
|
||||
|
||||
# Optional fast size (not always present)
|
||||
$sizeBytes = $null
|
||||
$szProp = $p.PSObject.Properties['Size']
|
||||
if ($szProp -and $szProp.Value -ne $null) { try { $sizeBytes = [int64]$szProp.Value } catch { } }
|
||||
|
||||
$stale = ($null -eq $luUtc) -or ($luUtc -lt $CutoffUtc)
|
||||
|
||||
[PSCustomObject]@{
|
||||
Computer = $env:COMPUTERNAME
|
||||
SID = $sid
|
||||
AccountName = $accName
|
||||
AccountFQN = $acc
|
||||
LocalPath = $p.LocalPath
|
||||
LastUseUtc = $luUtc
|
||||
Eligible = $stale
|
||||
SizeBytes = $sizeBytes
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
Write-Error ($_.Exception.Message)
|
||||
}
|
||||
}
|
||||
|
||||
$RemoteDeleteProfiles = {
|
||||
param([string[]]$SIDs)
|
||||
$results = @()
|
||||
$hasCIM = [bool](Get-Command -Name Get-CimInstance -ErrorAction SilentlyContinue)
|
||||
foreach ($sid in $SIDs) {
|
||||
try {
|
||||
if ($hasCIM) {
|
||||
$obj = Get-CimInstance -ClassName Win32_UserProfile -Filter ("SID='{0}'" -f $sid) -ErrorAction Stop
|
||||
if (-not $obj -or $obj.Loaded -or $obj.Special) { $results += [pscustomobject]@{SID=$sid;Deleted=$false;Code='SKIP';Message='Not found or loaded/special'}; continue }
|
||||
$rv = Invoke-CimMethod -InputObject $obj -MethodName Delete -ErrorAction Stop
|
||||
if ($rv.ReturnValue -eq 0) { $results += [pscustomobject]@{SID=$sid;Deleted=$true; Code=0; Message='OK'} }
|
||||
else { $results += [pscustomobject]@{SID=$sid;Deleted=$false;Code=$rv.ReturnValue;Message='Delete returned non-zero'} }
|
||||
} else {
|
||||
$obj = Get-WmiObject -Class Win32_UserProfile -Filter ("SID='{0}'" -f $sid) -ErrorAction Stop
|
||||
if (-not $obj -or $obj.Loaded -or $obj.Special) { $results += [pscustomobject]@{SID=$sid;Deleted=$false;Code='SKIP';Message='Not found or loaded/special'}; continue }
|
||||
$rv = $obj.Delete()
|
||||
if ($rv.ReturnValue -eq 0) { $results += [pscustomobject]@{SID=$sid;Deleted=$true; Code=0; Message='OK'} }
|
||||
else { $results += [pscustomobject]@{SID=$sid;Deleted=$false;Code=$rv.ReturnValue;Message='Delete returned non-zero'} }
|
||||
}
|
||||
} catch {
|
||||
$results += [pscustomobject]@{SID=$sid;Deleted=$false;Code='EXC';Message=$_.Exception.Message}
|
||||
}
|
||||
}
|
||||
return $results
|
||||
}
|
||||
|
||||
$SkippedHosts = New-Object System.Collections.Generic.HashSet[string] # names we couldn’t query or errored on
|
||||
}
|
||||
|
||||
process {
|
||||
# Reachability (WSMan) filter
|
||||
$TotalHostsQueried = $Targets.Count
|
||||
$reachable = @()
|
||||
foreach ($c in $Targets) {
|
||||
if (Test-HostOnline $c) { $reachable += $c } else { $null = $SkippedHosts.Add([string]$c) }
|
||||
}
|
||||
|
||||
if (-not $reachable) {
|
||||
Write-Info ("Total Hosts Queried: {0}" -f $TotalHostsQueried)
|
||||
Write-Info ("Skipped host(s) due to WinRM/Connectivity/TimeDifference/SPN issues: {0}" -f $SkippedHosts.Count)
|
||||
throw "No reachable hosts via WinRM."
|
||||
}
|
||||
|
||||
# Inventory
|
||||
$remoteErrors = @()
|
||||
$inv = Invoke-Command -ComputerName $reachable -ThrottleLimit 25 `
|
||||
-ScriptBlock $RemoteEnumerateProfiles -ArgumentList $CutoffUtc `
|
||||
-ErrorAction Continue -ErrorVariable +remoteErrors
|
||||
|
||||
foreach ($e in $remoteErrors) {
|
||||
if ($e.PSComputerName) { $null = $SkippedHosts.Add([string]$e.PSComputerName) }
|
||||
}
|
||||
|
||||
$rows = $inv | Where-Object { $_ -and $_.SID -and $_.LocalPath -like 'C:\Users\*' }
|
||||
$eligibleRows = $rows | Where-Object { $_.Eligible -eq $true }
|
||||
|
||||
# Plan per host
|
||||
$plan = @{}
|
||||
foreach ($r in $eligibleRows) {
|
||||
if (-not $plan.ContainsKey($r.Computer)) {
|
||||
$plan[$r.Computer] = [PSCustomObject]@{
|
||||
SIDs = New-Object System.Collections.Generic.List[string]
|
||||
Names = New-Object System.Collections.Generic.List[string]
|
||||
Size = [int64]0
|
||||
}
|
||||
}
|
||||
$plan[$r.Computer].SIDs.Add($r.SID)
|
||||
$plan[$r.Computer].Names.Add($r.AccountName)
|
||||
if ($r.SizeBytes -ne $null) { $plan[$r.Computer].Size += [int64]$r.SizeBytes }
|
||||
}
|
||||
|
||||
# Host-level counters first
|
||||
Write-Info ("Total Hosts Queried: {0}" -f $TotalHostsQueried)
|
||||
Write-Info ("Skipped host(s) due to WinRM/Connectivity/TimeDifference/SPN issues: {0}" -f $SkippedHosts.Count)
|
||||
|
||||
# Per-host summary lines
|
||||
$hostKeys = $plan.Keys | Sort-Object
|
||||
foreach ($h in $hostKeys) {
|
||||
$sids = $plan[$h].SIDs | Sort-Object -Unique
|
||||
$names = $plan[$h].Names | Where-Object { $_ } | Sort-Object -Unique
|
||||
$list = '(' + ($names -join ',') + ')'
|
||||
if ($DryRun) { Write-Dry ("Deleting {0} profile(s) on ""{1}"" {2}" -f $sids.Count, $h, $list) }
|
||||
else { Write-Info("Deleting {0} profile(s) on ""{1}"" {2}" -f $sids.Count, $h, $list) }
|
||||
}
|
||||
|
||||
# Execute deletes when not DryRun
|
||||
if (-not $DryRun -and $hostKeys.Count -gt 0) {
|
||||
foreach ($h in $hostKeys) {
|
||||
$sids = $plan[$h].SIDs | Sort-Object -Unique
|
||||
try {
|
||||
$null = Invoke-Command -ComputerName $h -ThrottleLimit 1 `
|
||||
-ScriptBlock $RemoteDeleteProfiles -ArgumentList (,$sids) `
|
||||
-ErrorAction Stop
|
||||
} catch {
|
||||
Write-Info ("Host ""{0}"": delete attempt failed: {1}" -f $h, $_.Exception.Message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# ---------- Bottom summary table ----------
|
||||
$analyzed = $rows.Count
|
||||
$evaluated = $rows.Count
|
||||
$eligibleCount = $eligibleRows.Count
|
||||
$fleetBytes = ($eligibleRows | Where-Object { $_.SizeBytes -ne $null } | Measure-Object -Property SizeBytes -Sum).Sum
|
||||
$fleetGB = if ($fleetBytes -and $fleetBytes -gt 0) { "{0} GB" -f ([Math]::Round($fleetBytes / 1GB, 2)) } else { "N/A" }
|
||||
|
||||
$summary = [ordered]@{
|
||||
"Local User Profiles Analyzed" = "$analyzed"
|
||||
"User Profiles Evaluated" = "$evaluated"
|
||||
("User Profiles Not Logged in for {0}+ Days" -f $InactiveDays) = "$eligibleCount"
|
||||
"Estimated Data To Remove (if executed)" = "$fleetGB"
|
||||
}
|
||||
|
||||
Show-AsciiTable -Data $summary
|
||||
}
|
||||
|
||||
end { }
|
||||
```
|
||||
|
||||
## Related Documentation
|
||||
- [Related Windows and Linux Documentation](<../../../reference/Windows and Linux/index.md>) — Find the connected deployments, procedures, and references for this subject.
|
||||
@@ -0,0 +1,42 @@
|
||||
---
|
||||
tags:
|
||||
- Windows
|
||||
- PowerShell
|
||||
- Scripting
|
||||
---
|
||||
|
||||
## Purpose
|
||||
Sometimes when you try to run Windows Updates, you may run into issues where updates just fail to install for seemingly nebulous reasons. You can run the following commands (in order) to try to resolve the issue.
|
||||
|
||||
!!! info "Run Commands from (CMD) Commandline, not powershell."
|
||||
|
||||
```powershell
|
||||
# Imaging integrity Rrepair tools
|
||||
DISM /Online /Cleanup-Image /RestoreHealth
|
||||
sfc /scannow
|
||||
DISM /Online /Cleanup-Image /StartComponentCleanup
|
||||
|
||||
# Stop all Windows Update services (in order) to unlock underlying files and folders.
|
||||
net stop usosvc # usosvc may refuse to stop on some systems; failure is non-fatal and you can proceed
|
||||
net stop wuauserv
|
||||
net stop bits
|
||||
net stop cryptsvc
|
||||
|
||||
# Purge the Windows Update cache folders and recreate them
|
||||
rd /s /q %windir%\SoftwareDistribution
|
||||
rd /s /q %windir%\System32\catroot2
|
||||
mkdir %windir%\SoftwareDistribution
|
||||
mkdir %windir%\System32\catroot2
|
||||
|
||||
# Start all Windows Update services (in order)
|
||||
net start cryptsvc
|
||||
net start bits
|
||||
net start wuauserv
|
||||
net start usosvc
|
||||
```
|
||||
|
||||
!!! info "Attempt Windows Updates"
|
||||
At this point, you can try re-running Windows Updates and seeing if the device makes it past the errors and installs the updates successfully or not. If not, **panic**.
|
||||
|
||||
## Related Documentation
|
||||
- [Related Windows and Linux Documentation](<../../../reference/Windows and Linux/index.md>) — Find the connected deployments, procedures, and references for this subject.
|
||||
@@ -0,0 +1,43 @@
|
||||
---
|
||||
tags:
|
||||
- PowerShell
|
||||
- Scripting
|
||||
---
|
||||
|
||||
## Purpose
|
||||
Start the `cagservice` RMM agent service on the domain servers selected by the script. The current implementation starts that service; it does not reboot computers or restart every service.
|
||||
|
||||
!!! warning "Under Connstruction"
|
||||
This document is under construction and not generalized for general purpose use yet. Manual work needs to be done to repurpose this script for general usage.
|
||||
|
||||
```powershell
|
||||
# Clear the screen before running the script
|
||||
Clear-Host
|
||||
Write-Host "Starting Domain-Wide Service Restart" -ForegroundColor Green
|
||||
|
||||
# Main Script -------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
# Get a list of all servers from Active Directory
|
||||
Write-Host "Retrieving server list from Active Directory..."
|
||||
$servers = Get-ADComputer -Filter * -Property OperatingSystem | Where-Object {
|
||||
$_.OperatingSystem -like "*Server*"
|
||||
} | Select-Object -ExpandProperty Name
|
||||
|
||||
# Loop through all servers and start the 'cagservice' service
|
||||
foreach ($server in $servers) {
|
||||
Write-Host ("Attempting to start 'cagservice' on " + $server + "...")
|
||||
try {
|
||||
Invoke-Command -ComputerName $server -ScriptBlock {
|
||||
Start-Service -Name cagservice -ErrorAction Stop
|
||||
Write-Host "'cagservice' started successfully on $env:COMPUTERNAME"
|
||||
} -ErrorAction Stop
|
||||
} catch {
|
||||
Write-Host ("Failed to start 'cagservice' on " + $server + ": " + $_.Exception.Message) -ForegroundColor Red
|
||||
}
|
||||
}
|
||||
|
||||
Write-Host "Script execution completed." -ForegroundColor Green
|
||||
```
|
||||
|
||||
## Related Documentation
|
||||
- [Related Windows and Linux Documentation](<../../../reference/Windows and Linux/index.md>) — Find the connected deployments, procedures, and references for this subject.
|
||||
@@ -0,0 +1,522 @@
|
||||
---
|
||||
tags:
|
||||
- Windows 11
|
||||
- Windows
|
||||
- PowerShell
|
||||
- Scripting
|
||||
---
|
||||
|
||||
## Purpose
|
||||
You may need to upgrade a device to Windows 11 using an ISO stored on a UNC Network Share, the script below handles that.
|
||||
|
||||
!!! note "Environment Variables"
|
||||
You may need to consider a few environment variables to be set when running the script. This can be done by hardcoding them into the script, or setting them in the Powershell session before executing the script within the same session.
|
||||
|
||||
### Environment Variables
|
||||
| **Variable** | **Type** | **Default Value** | **Additional Notes** |
|
||||
| :--- | :--- | :--- | :--- |
|
||||
|usrReboot|Boolean|true|Configure whether to reboot the device immediately once it is ready to install Windows 11.|
|
||||
|usrImagePath|String|Supply URI here|The network URI of the Windows 11 ISO to download using BITS. Windows 11 ISO links can be generated using a newly-made link on the Microsoft website.
|
||||
|usrOverrideChecks|Boolean|false|Override blocking issues?|
|
||||
|usrShowOOBE|Selection|`Skip Out-of-Box Experience`|Display or skip the post-install Out-of-Box Experience dialogue (Alternative is `Show Out-of-Box Experience`).|
|
||||
|
||||
```powershell
|
||||
function generateSHA256 ($executable, $storedHash) {
|
||||
$fileBytes = [io.File]::ReadAllBytes("$executable")
|
||||
$bytes = [Security.Cryptography.HashAlgorithm]::Create("SHA256").ComputeHash($fileBytes)
|
||||
$varCalculatedHash=-Join ($bytes | ForEach {"{0:x2}" -f $_})
|
||||
if ($storedHash -match $varCalculatedHash) {
|
||||
write-host "+ Filehash verified for file $executable`: $storedHash"
|
||||
} else {
|
||||
write-host "! ERROR: Filehash mismatch for file $executable."
|
||||
write-host " Expected value: $storedHash"
|
||||
write-host " Received value: $varCalculatedHash"
|
||||
write-host " Please report this error."
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
|
||||
function verifyPackage ($file, $certificate, $thumbprint1, $thumbprint2, $name, $url) { #special two-thumbprint edition
|
||||
$varChain = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Chain
|
||||
try {
|
||||
$varChain.Build((Get-AuthenticodeSignature -FilePath "$file").SignerCertificate) | out-null
|
||||
} catch [System.Management.Automation.MethodInvocationException] {
|
||||
write-host "- ERROR: $name installer did not contain a valid digital certificate."
|
||||
write-host " This could suggest a change in the way $name is packaged; it could"
|
||||
write-host " also suggest tampering in the connection chain."
|
||||
write-host "- Please ensure $url is whitelisted and try again."
|
||||
write-host " If this issue persists across different devices, please file a support ticket."
|
||||
}
|
||||
|
||||
$varIntermediate=($varChain.ChainElements | ForEach-Object {$_.Certificate} | Where-Object {$_.Subject -match "$certificate"}).Thumbprint
|
||||
|
||||
if ($varIntermediate -ne $thumbprint1 -and $varIntermediate -ne $thumbprint2) {
|
||||
write-host "- ERROR: $file did not pass verification checks for its digital signature."
|
||||
write-host " This could suggest that the certificate used to sign the $name installer"
|
||||
write-host " has changed; it could also suggest tampering in the connection chain."
|
||||
write-host `r
|
||||
if ($varIntermediate) {
|
||||
write-host ": We received: $varIntermediate"
|
||||
write-host " We expected: $thumbprint1"
|
||||
write-host " -OR- : $thumbprint2"
|
||||
write-host " Please report this issue."
|
||||
}
|
||||
write-host "- Installation cannot continue. Exiting."
|
||||
exit 1
|
||||
} else {
|
||||
write-host "+ Digital Signature verification passed."
|
||||
}
|
||||
}
|
||||
|
||||
function quitOr {
|
||||
if ($env:usrOverrideChecks -match 'true') {
|
||||
write-host "! This is a blocking error and should abort the process; however, the usrOverrideChecks"
|
||||
write-host " flag has been enabled, and the error will thus be ignored."
|
||||
write-host " Support will not be able to assist with issues that arise as a consequence of this action."
|
||||
} else {
|
||||
write-host "! This is a blocking error; the operation has been aborted."
|
||||
Write-Host " If you do not believe the error to be valid, you can re-run this Component with the"
|
||||
write-host " `'usrOverrideChecks`' flag enabled, which will ignore blocking errors and proceed."
|
||||
write-host " Support will not be able to assist with issues that arise as a consequence of this action."
|
||||
Stop-Process -name setupHost -ErrorAction SilentlyContinue
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
|
||||
function makeHTTPRequest ($tempHost) { #makeHTTPRequest v5: make an HTTP request and ensure a status code (any) is returned
|
||||
$tempRequest = [System.Net.WebRequest]::Create($tempHost)
|
||||
try {
|
||||
$tempResponse=$tempRequest.getResponse()
|
||||
$TempReturn=($tempResponse.StatusCode -as [int])
|
||||
} catch [System.Exception] {
|
||||
$tempReturn=$_.Exception.Response.StatusCode.Value__
|
||||
}
|
||||
|
||||
if ($tempReturn -match '200') {
|
||||
write-host "- Confirmed file at $tempHost is ready for download."
|
||||
} else {
|
||||
write-host "! ERROR: No file was found at $temphost."
|
||||
write-host " If you are downloading from Microsoft, this may mean a bad URL was entered;"
|
||||
write-host " bear in mind that ISO links generated from Microsoft.com are only valid for"
|
||||
write-host " 24 hours before needing to be re-calculated."
|
||||
write-host " Generate new links at https://www.microsoft.com/software-download/windows11."
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
|
||||
#===================================================================================================================================================
|
||||
|
||||
#kernel data
|
||||
[int]$varKernel=(get-wmiObject win32_operatingSystem buildNumber).buildNumber
|
||||
|
||||
#user text
|
||||
write-host `r
|
||||
write-host "Windows 11 Updater: Update Windows 10 2004+ to the latest build of Windows 11"
|
||||
write-host "==============================================================================="
|
||||
write-host "`: Upgrading from: Build $varKernel /" (get-WMiObject -Class win32_operatingSystem).caption
|
||||
|
||||
############################################### LANGUAGE INFORMATION ###############################################
|
||||
|
||||
#language table (for ISO download) :: 2021 seagull/datto, inc.
|
||||
$varLCID=(Get-ItemProperty hklm:\system\controlset001\control\nls\language -name InstallLanguage).InstallLanguage
|
||||
$arrLCID=@{
|
||||
"0401"=[PSCustomObject]@{Title="Arabic"; Localisation="Arabic"; MSKeyword="Arabic"; DattoKeyword="US"}
|
||||
"0416"=[PSCustomObject]@{Title="Brazilian Portuguese"; Localisation="Brazilian Portuguese"; MSKeyword="BrazilianPortuguese"; DattoKeyword="US"}
|
||||
"0402"=[PSCustomObject]@{Title="Bulgarian"; Localisation="Bulgarian"; MSKeyword="Bulgarian"; DattoKeyword="US"}
|
||||
"0804"=[PSCustomObject]@{Title="Chinese (Simplified)"; Localisation="Chinese (Simplified)"; MSKeyword="Chinese(Simplified)"; DattoKeyword="US"}
|
||||
"0004"=[PSCustomObject]@{Title="Chinese (Simplified)"; Localisation="Chinese (Simplified)"; MSKeyword="Chinese(Simplified)"; DattoKeyword="US"}
|
||||
"7804"=[PSCustomObject]@{Title="Chinese (Simplified)"; Localisation="Chinese (Simplified)"; MSKeyword="Chinese(Simplified)"; DattoKeyword="US"}
|
||||
"1004"=[PSCustomObject]@{Title="Chinese (Singapore)"; Localisation="Chinese (Simplified)"; MSKeyword="Chinese(Simplified)"; DattoKeyword="US"}
|
||||
"0C04"=[PSCustomObject]@{Title="Chinese (Hong Kong)"; Localisation="Chinese (Traditional)"; MSKeyword="Chinese(Traditional)"; DattoKeyword="US"}
|
||||
"0404"=[PSCustomObject]@{Title="Chinese (Taiwan)"; Localisation="Chinese (Traditional)"; MSKeyword="Chinese(Traditional)"; DattoKeyword="US"}
|
||||
"7C04"=[PSCustomObject]@{Title="Chinese (Traditional)"; Localisation="Chinese (Traditional)"; MSKeyword="Chinese(Traditional)"; DattoKeyword="US"}
|
||||
"041A"=[PSCustomObject]@{Title="Croatian"; Localisation="Croatian"; MSKeyword="Croatian"; DattoKeyword="US"}
|
||||
"0405"=[PSCustomObject]@{Title="Czech"; Localisation="Czech"; MSKeyword="Czech"; DattoKeyword="US"}
|
||||
"0005"=[PSCustomObject]@{Title="Czech"; Localisation="Czech"; MSKeyword="Czech"; DattoKeyword="US"}
|
||||
"0006"=[PSCustomObject]@{Title="Danish"; Localisation="Danish"; MSKeyword="Danish"; DattoKeyword="US"}
|
||||
"0406"=[PSCustomObject]@{Title="Danish"; Localisation="Danish"; MSKeyword="Danish"; DattoKeyword="US"}
|
||||
"0013"=[PSCustomObject]@{Title="Dutch"; Localisation="Dutch"; MSKeyword="Dutch"; DattoKeyword="US"}
|
||||
"0813"=[PSCustomObject]@{Title="Dutch (Belgium)"; Localisation="Dutch"; MSKeyword="Dutch"; DattoKeyword="US"}
|
||||
"0413"=[PSCustomObject]@{Title="Dutch (Netherlands)"; Localisation="Dutch"; MSKeyword="Dutch"; DattoKeyword="US"}
|
||||
"0009"=[PSCustomObject]@{Title="English (Generic)"; Localisation="English"; MSKeyword="English"; DattoKeyword="US"}
|
||||
"0409"=[PSCustomObject]@{Title="English (United States)"; Localisation="English"; MSKeyword="English"; DattoKeyword="US"}
|
||||
"0809"=[PSCustomObject]@{Title="English (UK)"; Localisation="English (International)"; MSKeyword="EnglishInternational"; DattoKeyword="UK"}
|
||||
"0C09"=[PSCustomObject]@{Title="English (Australia)"; Localisation="English (International)"; MSKeyword="EnglishInternational"; DattoKeyword="US"}
|
||||
"1409"=[PSCustomObject]@{Title="English (New Zealand)"; Localisation="English (International)"; MSKeyword="EnglishInternational"; DattoKeyword="US"}
|
||||
"1009"=[PSCustomObject]@{Title="English (Canada)"; Localisation="English (International)"; MSKeyword="EnglishInternational"; DattoKeyword="US"}
|
||||
"1C09"=[PSCustomObject]@{Title="English (South Africa)"; Localisation="English (International)"; MSKeyword="EnglishInternational"; DattoKeyword="US"}
|
||||
"0025"=[PSCustomObject]@{Title="Estonian"; Localisation="Estonian"; MSKeyword="Estonian"; DattoKeyword="US"}
|
||||
"0425"=[PSCustomObject]@{Title="Estonian"; Localisation="Estonian"; MSKeyword="Estonian"; DattoKeyword="US"}
|
||||
"000B"=[PSCustomObject]@{Title="Finnish"; Localisation="Finnish"; MSKeyword="Finnish"; DattoKeyword="US"}
|
||||
"040B"=[PSCustomObject]@{Title="Finnish"; Localisation="Finnish"; MSKeyword="Finnish"; DattoKeyword="US"}
|
||||
"000C"=[PSCustomObject]@{Title="French"; Localisation="French"; MSKeyword="French"; DattoKeyword="US"}
|
||||
"040C"=[PSCustomObject]@{Title="French"; Localisation="French"; MSKeyword="French"; DattoKeyword="US"}
|
||||
"080C"=[PSCustomObject]@{Title="French (Belgium)"; Localisation="French"; MSKeyword="French"; DattoKeyword="US"}
|
||||
"100C"=[PSCustomObject]@{Title="French (Switzerland)"; Localisation="French"; MSKeyword="French"; DattoKeyword="US"}
|
||||
"0C0C"=[PSCustomObject]@{Title="French Canadian"; Localisation="French Canadian"; MSKeyword="FrenchCanadian"; DattoKeyword="US"}
|
||||
"0007"=[PSCustomObject]@{Title="German"; Localisation="German"; MSKeyword="German"; DattoKeyword="US"}
|
||||
"0407"=[PSCustomObject]@{Title="German"; Localisation="German"; MSKeyword="German"; DattoKeyword="US"}
|
||||
"0C07"=[PSCustomObject]@{Title="German (Austria)"; Localisation="German"; MSKeyword="German"; DattoKeyword="US"}
|
||||
"0807"=[PSCustomObject]@{Title="German (Switzerland)"; Localisation="German"; MSKeyword="German"; DattoKeyword="US"}
|
||||
"0008"=[PSCustomObject]@{Title="Greek"; Localisation="Greek"; MSKeyword="Greek"; DattoKeyword="US"}
|
||||
"0408"=[PSCustomObject]@{Title="Greek"; Localisation="Greek"; MSKeyword="Greek"; DattoKeyword="US"}
|
||||
"000D"=[PSCustomObject]@{Title="Hebrew"; Localisation="Hebrew"; MSKeyword="Hebrew"; DattoKeyword="US"}
|
||||
"040D"=[PSCustomObject]@{Title="Hebrew"; Localisation="Hebrew"; MSKeyword="Hebrew"; DattoKeyword="US"}
|
||||
"000E"=[PSCustomObject]@{Title="Hungarian"; Localisation="Hungarian"; MSKeyword="Hungarian"; DattoKeyword="US"}
|
||||
"040E"=[PSCustomObject]@{Title="Hungarian"; Localisation="Hungarian"; MSKeyword="Hungarian"; DattoKeyword="US"}
|
||||
"0010"=[PSCustomObject]@{Title="Italian"; Localisation="Italian"; MSKeyword="Italian"; DattoKeyword="US"}
|
||||
"0410"=[PSCustomObject]@{Title="Italian"; Localisation="Italian"; MSKeyword="Italian"; DattoKeyword="US"}
|
||||
"0810"=[PSCustomObject]@{Title="Italian (Switzerland)"; Localisation="Italian"; MSKeyword="Italian"; DattoKeyword="US"}
|
||||
"0011"=[PSCustomObject]@{Title="Japanese"; Localisation="Japanese"; MSKeyword="Japanese"; DattoKeyword="US"}
|
||||
"0411"=[PSCustomObject]@{Title="Japanese"; Localisation="Japanese"; MSKeyword="Japanese"; DattoKeyword="US"}
|
||||
"0012"=[PSCustomObject]@{Title="Korean"; Localisation="Korean"; MSKeyword="Korean"; DattoKeyword="US"}
|
||||
"0412"=[PSCustomObject]@{Title="Korean"; Localisation="Korean"; MSKeyword="Korean"; DattoKeyword="US"}
|
||||
"0026"=[PSCustomObject]@{Title="Latvian"; Localisation="Latvian"; MSKeyword="Latvian"; DattoKeyword="US"}
|
||||
"0426"=[PSCustomObject]@{Title="Latvian"; Localisation="Latvian"; MSKeyword="Latvian"; DattoKeyword="US"}
|
||||
"0027"=[PSCustomObject]@{Title="Lithuanian"; Localisation="Lithuanian"; MSKeyword="Lithuanian"; DattoKeyword="US"}
|
||||
"0427"=[PSCustomObject]@{Title="Lithuanian"; Localisation="Lithuanian"; MSKeyword="Lithuanian"; DattoKeyword="US"}
|
||||
"0014"=[PSCustomObject]@{Title="Norwegian (Bokm?l)"; Localisation="Norwegian"; MSKeyword="Norwegian"; DattoKeyword="US"}
|
||||
"7C14"=[PSCustomObject]@{Title="Norwegian (Bokm?l)"; Localisation="Norwegian"; MSKeyword="Norwegian"; DattoKeyword="US"}
|
||||
"0414"=[PSCustomObject]@{Title="Norwegian (Bokm?l)"; Localisation="Norwegian"; MSKeyword="Norwegian"; DattoKeyword="US"}
|
||||
"7814"=[PSCustomObject]@{Title="Norwegian (Nynorsk)"; Localisation="Norwegian"; MSKeyword="Norwegian"; DattoKeyword="US"}
|
||||
"0814"=[PSCustomObject]@{Title="Norwegian (Nynorsk)"; Localisation="Norwegian"; MSKeyword="Norwegian"; DattoKeyword="US"}
|
||||
"0015"=[PSCustomObject]@{Title="Polish"; Localisation="Polish"; MSKeyword="Polish"; DattoKeyword="US"}
|
||||
"0415"=[PSCustomObject]@{Title="Polish"; Localisation="Polish"; MSKeyword="Polish"; DattoKeyword="US"}
|
||||
"0816"=[PSCustomObject]@{Title="Portuguese"; Localisation="Portuguese"; MSKeyword="Portuguese"; DattoKeyword="US"}
|
||||
"0018"=[PSCustomObject]@{Title="Romanian"; Localisation="Romanian"; MSKeyword="Romanian"; DattoKeyword="US"}
|
||||
"0418"=[PSCustomObject]@{Title="Romanian"; Localisation="Romanian"; MSKeyword="Romanian"; DattoKeyword="US"}
|
||||
"0818"=[PSCustomObject]@{Title="Moldovan"; Localisation="Romanian"; MSKeyword="Romanian"; DattoKeyword="US"}
|
||||
"0019"=[PSCustomObject]@{Title="Russian"; Localisation="Russian"; MSKeyword="Russian"; DattoKeyword="US"}
|
||||
"0419"=[PSCustomObject]@{Title="Russian"; Localisation="Russian"; MSKeyword="Russian"; DattoKeyword="US"}
|
||||
"0819"=[PSCustomObject]@{Title="Russian (Moldova)"; Localisation="Russian"; MSKeyword="Russian"; DattoKeyword="US"}
|
||||
"701A"=[PSCustomObject]@{Title="Serbian (Latin)"; Localisation="Serbian Latin"; MSKeyword="SerbianLatin"; DattoKeyword="US"}
|
||||
"7C1A"=[PSCustomObject]@{Title="Serbian (Latin)"; Localisation="Serbian Latin"; MSKeyword="SerbianLatin"; DattoKeyword="US"}
|
||||
"181A"=[PSCustomObject]@{Title="Serbian (Latin, BO/HE)"; Localisation="Serbian Latin"; MSKeyword="SerbianLatin"; DattoKeyword="US"}
|
||||
"2C1A"=[PSCustomObject]@{Title="Serbian (Latin, MO)"; Localisation="Serbian Latin"; MSKeyword="SerbianLatin"; DattoKeyword="US"}
|
||||
"241A"=[PSCustomObject]@{Title="Serbian (Latin)"; Localisation="Serbian Latin"; MSKeyword="SerbianLatin"; DattoKeyword="US"}
|
||||
"081A"=[PSCustomObject]@{Title="Serbian (Latin, SR/MO)"; Localisation="Serbian Latin"; MSKeyword="SerbianLatin"; DattoKeyword="US"}
|
||||
"001B"=[PSCustomObject]@{Title="Slovak"; Localisation="Slovak"; MSKeyword="Slovak"; DattoKeyword="US"}
|
||||
"041B"=[PSCustomObject]@{Title="Slovak"; Localisation="Slovak"; MSKeyword="Slovak"; DattoKeyword="US"}
|
||||
"0024"=[PSCustomObject]@{Title="Slovenian"; Localisation="Slovenian"; MSKeyword="Slovenian"; DattoKeyword="US"}
|
||||
"0424"=[PSCustomObject]@{Title="Slovenian"; Localisation="Slovenian"; MSKeyword="Slovenian"; DattoKeyword="US"}
|
||||
"000A"=[PSCustomObject]@{Title="Spanish (Spain)"; Localisation="Spanish"; MSKeyword="Spanish"; DattoKeyword="US"}
|
||||
"040A"=[PSCustomObject]@{Title="Spanish (Spain)"; Localisation="Spanish"; MSKeyword="Spanish"; DattoKeyword="US"}
|
||||
"0C0A"=[PSCustomObject]@{Title="Spanish (Spain)"; Localisation="Spanish"; MSKeyword="Spanish"; DattoKeyword="US"}
|
||||
"2C0A"=[PSCustomObject]@{Title="Spanish (Argentina)"; Localisation="Spanish (Mexico)"; MSKeyword="Spanish(Mexico)"; DattoKeyword="US"}
|
||||
"340A"=[PSCustomObject]@{Title="Spanish (Chile)"; Localisation="Spanish (Mexico)"; MSKeyword="Spanish(Mexico)"; DattoKeyword="US"}
|
||||
"580A"=[PSCustomObject]@{Title="Spanish (Latin America)"; Localisation="Spanish (Mexico)"; MSKeyword="Spanish(Mexico)"; DattoKeyword="US"}
|
||||
"080A"=[PSCustomObject]@{Title="Spanish (M?xico)"; Localisation="Spanish (Mexico)"; MSKeyword="Spanish(Mexico)"; DattoKeyword="US"}
|
||||
"001D"=[PSCustomObject]@{Title="Swedish"; Localisation="Swedish"; MSKeyword="Swedish"; DattoKeyword="US"}
|
||||
"041D"=[PSCustomObject]@{Title="Swedish"; Localisation="Swedish"; MSKeyword="Swedish"; DattoKeyword="US"}
|
||||
"001E"=[PSCustomObject]@{Title="Thai"; Localisation="Thai"; MSKeyword="Thai"; DattoKeyword="US"}
|
||||
"041E"=[PSCustomObject]@{Title="Thai"; Localisation="Thai"; MSKeyword="Thai"; DattoKeyword="US"}
|
||||
"001F"=[PSCustomObject]@{Title="Turkish"; Localisation="Turkish"; MSKeyword="Turkish"; DattoKeyword="US"}
|
||||
"041F"=[PSCustomObject]@{Title="Turkish"; Localisation="Turkish"; MSKeyword="Turkish"; DattoKeyword="US"}
|
||||
"0022"=[PSCustomObject]@{Title="Ukrainian"; Localisation="Ukrainian"; MSKeyword="Ukrainian"; DattoKeyword="US"}
|
||||
"0422"=[PSCustomObject]@{Title="Ukrainian"; Localisation="Ukrainian"; MSKeyword="Ukrainian"; DattoKeyword="US"}
|
||||
}
|
||||
|
||||
#if they're running something we don't understand...
|
||||
if (!($($arrLCID[$varLCID].DattoKeyword))) {
|
||||
$arrLCID[$varLCID]=[PSCustomObject]@{Title="Unknown";Localisation="English";MSKeyword="English";DattoKeyword="US"}
|
||||
}
|
||||
|
||||
#output this information
|
||||
write-host ": Device language: $($arrLCID[$varLCID].Title)"
|
||||
write-host ": Suggested carryover: $($arrLCID[$varLCID].Localisation)"
|
||||
|
||||
############################################### ISO COMPATIBILITY ###############################################
|
||||
|
||||
#define an early SKU list and add to it depending on user choice
|
||||
$arrGoodSKU=@(48,49,98,99,100,101)
|
||||
|
||||
if (($env:usrImagePath -as [string]).Length -lt 2 -or $env:usrImagePath -eq 'Supply URI here') {
|
||||
#nothing
|
||||
write-host "! ERROR: No image path defined."
|
||||
write-host " The Component works by downloading a Windows 11 ISO from the Internet"
|
||||
write-host " (or a local share), mounting it and installing from it."
|
||||
write-host " Without a link to an ISO, nothing can be downloaded."
|
||||
write-host `r
|
||||
write-host " Generate a Windows 11 ISO download link good for 24 hours at:"
|
||||
write-host " https://www.microsoft.com/software-download/windows11"
|
||||
exit 1
|
||||
} elseif ($env:usrImagePath -match 'software-download.microsoft.com') {
|
||||
#microsoft
|
||||
write-host ": ISO Download location: Microsoft servers."
|
||||
write-host " Please be aware that Microsoft's ISO download links expire after 24 hours."
|
||||
write-host " If the download fails, your link may need to be re-generated."
|
||||
makeHTTPRequest $env:usrImagePath
|
||||
|
||||
#compare ISO region to device region
|
||||
$varISOLang=$env:usrImagePath.split('_')[1]
|
||||
write-host ": MS ISO Language: $varISOLang"
|
||||
if ($varISOLang -ne $($arrLCID[$varLCID].MSKeyword)) {
|
||||
write-host "! ERROR: Mismatch between device language and Microsoft ISO."
|
||||
write-host " The languages must match up as closely as possible otherwise the installation will fail."
|
||||
write-host " This error can be overridden if you are certain this will not pose an issue."
|
||||
quitOr
|
||||
}
|
||||
} else {
|
||||
#custom
|
||||
write-host ": ISO location set by user. Edition, Language &c. defined by image."
|
||||
$arrGoodSKU+=4,27,84,161,162 #add valid SKUs beyond our/MS's reach
|
||||
}
|
||||
|
||||
#separate check: check SKU if the user is not supplying their own ISO
|
||||
[int]$varSKU=(Get-WmiObject -Class win32_operatingsystem -Property OperatingSystemSKU).OperatingSystemSKU
|
||||
if ($arrGoodSKU | ? {$_ -eq $varSKU}) {
|
||||
write-host "+ Device Windows SKU ($varSKU) is supported."
|
||||
} else {
|
||||
write-host "! ERROR: Device Windows SKU ($varSKU) not supported."
|
||||
write-host " Windows 11 can only be installed on devices running Windows 10 2004 onward;"
|
||||
write-host " meaning devices with SKUs discontinued by Microsoft are not compatible."
|
||||
write-host " Enterprise, Pro-for-Workstations and Education edition ISOs are not supplied"
|
||||
write-host " from Microsoft and thus, these cannot be updated from a Microsoft URL."
|
||||
write-host " This error can be overridden if you are certain the SKU will not pose an issue."
|
||||
quitOr
|
||||
}
|
||||
|
||||
write-host "`: ISO download path: $env:usrImagePath"
|
||||
|
||||
############################################### HARDWARE COMPAT ###############################################
|
||||
|
||||
#architecture
|
||||
if ((Get-WMIObject -Class Win32_Processor).Architecture -ne 9) {
|
||||
write-host "! ERROR: This device does not have an AMD64/EM64T-capable processor."
|
||||
write-host " Windows 11 will not run on 32-bit devices."
|
||||
write-host " Installation cancelled; exiting."
|
||||
exit 1
|
||||
} elseif ([intptr]::Size -eq 4) {
|
||||
write-host ": 32-bit Windows detected, but device processor is AMD64/EM64T-capable."
|
||||
write-host " An architecture upgrade will be attempted; the device will lose"
|
||||
write-host " the ability to run 16-bit programs, but 32-bit programs will"
|
||||
write-host " continue to work using Windows-on-Windows (WOW) emulation."
|
||||
} else {
|
||||
write-host "+ 64-bit architecture checks passed."
|
||||
}
|
||||
|
||||
#minimum W10-04
|
||||
if ($varKernel -lt 19041) {
|
||||
write-host "! ERROR: Windows 10 version 2004 or higher is required to proceed."
|
||||
quitOr
|
||||
}
|
||||
|
||||
#services pipe timeout
|
||||
REG ADD "HKLM\SYSTEM\CurrentControlSet\Control" /v ServicesPipeTimeout /t REG_DWORD /d "300000" /f 2>&1>$null
|
||||
write-host ": Device service timeout period configured to five minutes."
|
||||
|
||||
write-host "+ Target device OS is Windows 10 2004 or greater."
|
||||
|
||||
#make sure it's licensed (v3)
|
||||
if (!(Get-WmiObject SoftwareLicensingProduct | ? { $_.LicenseStatus -eq 1 } | select -ExpandProperty Description | select-string "Windows" -Quiet)) {
|
||||
write-host "! ERROR: Windows 10 can only be installed on devices with an active Windows licence."
|
||||
quitOr
|
||||
}
|
||||
|
||||
write-host "+ Target device has a valid Windows licence."
|
||||
|
||||
#make sure we have enough disk space - installation plus iso hosting
|
||||
$varSysFree = [Math]::Round((Get-WMIObject -Class Win32_Volume |Where-Object {$_.DriveLetter -eq $env:SystemDrive} | Select -expand FreeSpace) / 1GB)
|
||||
if ($varSysFree -lt 20) {
|
||||
write-host "! ERROR: System drive requires at least 20GB: 13 for installation, 7 for the disc image."
|
||||
quitOr
|
||||
}
|
||||
|
||||
write-host "+ Target device has at least 20GB of free hard disk space."
|
||||
|
||||
#check for RAM
|
||||
if (((Get-WmiObject -class "cim_physicalmemory" | Measure-Object -Property Capacity -Sum).Sum / 1024 / 1024 / 1024) -lt 4) {
|
||||
write-host "! ERROR: This machine may not have enough RAM installed."
|
||||
write-host " Windows 11 requires at least 4GB of system RAM to be installed."
|
||||
write-host " In case of errors, please check this device's RAM."
|
||||
quitOr
|
||||
} else {
|
||||
write-host "+ Device has at least 4GB of RAM installed."
|
||||
}
|
||||
|
||||
#TPM check
|
||||
$varTPM=@(0,0,0) # present :: enabled :: activated
|
||||
if ((Get-WmiObject -Class Win32_TPM -EnableAllPrivileges -Namespace "root\CIMV2\Security\MicrosoftTpm").__SERVER) { # TPM installed
|
||||
$varTPM[0]=1
|
||||
if ((Get-WmiObject -Namespace ROOT\CIMV2\Security\MicrosoftTpm -Class Win32_Tpm).IsEnabled().isenabled -eq $true) { # TPM enabled
|
||||
$varTPM[1]=1
|
||||
if ((Get-WmiObject -Namespace ROOT\CIMV2\Security\MicrosoftTpm -Class Win32_Tpm).IsActivated().isactivated -eq $true) { # TPM activated
|
||||
$varTPM[2]=1
|
||||
} else {
|
||||
$varTPM[2]=0
|
||||
}
|
||||
} else {
|
||||
$varTPM[1]=0
|
||||
$varTPM[2]=0
|
||||
}
|
||||
}
|
||||
|
||||
switch -Regex ($varTPM -as [string]) {
|
||||
'^0' {
|
||||
write-host "! ERROR: This system does not contain a Trusted Platform Module (TPM)."
|
||||
write-host " Windows 11 requires the use of a TPM to install."
|
||||
write-host " Your device may contain a firmware TPM (fTPM) which can be enabled in the BIOS/uEFI settings. More info:"
|
||||
write-host " https://support.microsoft.com/en-us/windows/enable-tpm-2-0-on-your-pc-1fd5a332-360d-4f46-a1e7-ae6b0c90645c"
|
||||
write-host "- Cannot continue; exiting."
|
||||
quitOr
|
||||
} '0 0$' {
|
||||
write-host "! ERROR: Whilst a TPM was detected in this system, the WMI reports that it is disabled."
|
||||
write-host " Please re-enable the use of the TPM and try installing again."
|
||||
write-host "- Cannot continue; exiting."
|
||||
quitOr
|
||||
} default {
|
||||
write-host "! ERROR: Whilst a TPM was detected in this system, the WMI reports that it has been deactivated."
|
||||
write-host " Please re-activate the TPM and try installing again."
|
||||
write-host "- Cannot continue; exiting."
|
||||
quitOr
|
||||
} '1$' {
|
||||
write-host "+ TPM installed and active."
|
||||
} $null {
|
||||
write-host "! ERROR: A fault has occurred during the TPM checking subroutine. Please report this."
|
||||
quitOr
|
||||
}
|
||||
|
||||
# to those who read my scripts: this logic is taken from the "bitlocker & TPM audit" component, which offers a much more in-depth
|
||||
# look at a device's bitlocker/TPM status than is offered here. grab it from the comstore today! -- seagull nov '21
|
||||
}
|
||||
|
||||
############################################### IMAGE TRANSFER ###############################################
|
||||
# Added UNC/SMB support – preserves all original error handling / unattended flow
|
||||
##############################################################################################################
|
||||
|
||||
# We still import BITS for HTTP/S transfers, but SMB copies don’t rely on it.
|
||||
try {
|
||||
Import-Module BitsTransfer -Force -ErrorAction Stop
|
||||
Write-Host ": BitsTransfer module loaded."
|
||||
}
|
||||
catch {
|
||||
Write-Host ": BitsTransfer module unavailable – HTTP/HTTPS downloads will fail."
|
||||
}
|
||||
|
||||
if ($env:usrImagePath -match 'amp;') {
|
||||
$env:usrImagePath = $env:usrImagePath -replace 'amp;'
|
||||
}
|
||||
|
||||
$isoDestPath = "$env:PUBLIC\Win11.iso"
|
||||
$srcPath = $env:usrImagePath
|
||||
|
||||
function Copy-IsoFromUNC {
|
||||
param(
|
||||
[string]$UncPath,
|
||||
[string]$DestPath
|
||||
)
|
||||
|
||||
Write-Host ": Copying ISO from UNC share: $UncPath"
|
||||
if (-not (Test-Path $UncPath)) {
|
||||
Write-Host "! ERROR: UNC path not reachable or permissions denied (`$UncPath`)."
|
||||
Write-Host " Remember Datto RMM executes as NT AUTHORITY\\SYSTEM; the share must allow"
|
||||
Write-Host " read access for the computer account **or** the Everyone group."
|
||||
quitOr
|
||||
}
|
||||
|
||||
# Robocopy provides restartable transfers & built-in hashing.
|
||||
$roboLog = "$env:PUBLIC\\robocopy_win11_iso.log"
|
||||
$cmd = @(
|
||||
'robocopy',
|
||||
('"' + (Split-Path $UncPath -Parent) + '"'),
|
||||
('"' + (Split-Path $DestPath -Parent) + '"'),
|
||||
('"' + (Split-Path $UncPath -Leaf) + '"'),
|
||||
'/NFL','/NDL','/NJH','/NJS','/NP', # quiet output
|
||||
'/R:3','/W:5', # retry logic
|
||||
'/V','/FFT','/Z', # verify, tolerant times, restartable
|
||||
"/LOG:`"$roboLog`""
|
||||
) -join ' '
|
||||
|
||||
Write-Host ": Executing -> $cmd"
|
||||
Invoke-Expression $cmd | Out-Null
|
||||
|
||||
if (-not (Test-Path $DestPath)) {
|
||||
Write-Host "! ERROR: Robocopy did not create $DestPath. Check $roboLog."
|
||||
quitOr
|
||||
}
|
||||
Write-Host "+ ISO copied successfully to $DestPath"
|
||||
}
|
||||
|
||||
################################################################
|
||||
# Decide transfer method #
|
||||
################################################################
|
||||
if ($srcPath -match '^(\\\\|//)') {
|
||||
# --- UNC / SMB path --------------------------------------
|
||||
Copy-IsoFromUNC -UncPath $srcPath -DestPath $isoDestPath
|
||||
}
|
||||
elseif ($srcPath -match '^https?://') {
|
||||
# --- HTTP / HTTPS download (original BITS logic) ----------
|
||||
Write-Host ": Downloading ISO via BITS from $srcPath"
|
||||
Start-BitsTransfer -Source $srcPath -Destination $isoDestPath -DisplayName 'Windows 11 ISO'
|
||||
}
|
||||
else {
|
||||
Write-Host "! ERROR: usrImagePath must begin with http(s):// **or** \\\\server\\share\\Win11.iso"
|
||||
quitOr
|
||||
}
|
||||
|
||||
################################################################
|
||||
# Post-transfer validation #
|
||||
################################################################
|
||||
if (Test-Path $isoDestPath -ErrorAction SilentlyContinue) {
|
||||
Write-Host "+ ISO present at $isoDestPath – transfer verified."
|
||||
} else {
|
||||
Write-Host "! ERROR: ISO not found at $isoDestPath after transfer."
|
||||
Write-Host " For SMB shares, double-check permissions; for HTTP, ensure URL is valid."
|
||||
quitOr
|
||||
}
|
||||
|
||||
#extract the image
|
||||
generateSHA256 7z.dll "DB2897EEEA65401EE1BD8FEEEBD0DBAE8867A27FF4575F12B0B8A613444A5EF7"
|
||||
generateSHA256 7z.exe "A20D93E7DC3711E8B8A8F63BD148DDC70DE8C952DE882C5495AC121BFEDB749F"
|
||||
.\7z.exe x -y "$env:PUBLIC\Win11.iso" `-o"$env:PUBLIC\Win11Extract" -aoa -bsp0 -bso0
|
||||
#verify extraction
|
||||
if (!(test-path "$env:PUBLIC\Win11Extract\setup.exe" -ErrorAction SilentlyContinue)) {
|
||||
write-host "! ERROR: Extraction of Windows 11 ISO failed."
|
||||
write-host " Possible causes/fixes:"
|
||||
write-host " - Download aborted. Check that the ISO can be mounted."
|
||||
write-host " - Inadequate allowlisting. Ensure the ISO is reachable over the network."
|
||||
write-host " - Permission issues. On a UNC share, the ISO must be viewable by LocalSystem."
|
||||
write-host " - Something caused the extraction to fail (very high CPU usage?)"
|
||||
write-host " Operations aborted: cannot proceed."
|
||||
quitOr
|
||||
}
|
||||
|
||||
start-sleep -Seconds 15
|
||||
Remove-Item "$env:PUBLIC\Win11.iso" -Force
|
||||
write-host "+ ISO extracted to $env:PUBLIC\Win11Extract. ISO file deleted."
|
||||
|
||||
#make a cleanup script to remove the win11 folder post-install :: ps2 compat
|
||||
@"
|
||||
@echo off
|
||||
REM This is a cleanup script. For more information, consult your systems administrator.
|
||||
rd `"$env:PUBLIC\Win11Extract`" /s /q
|
||||
del `"$env:PUBLIC\cleanup.bat`" /s /q /f
|
||||
"@ | set-content -path "$env:PUBLIC\cleanup.bat" -Force
|
||||
|
||||
#verify the windows 11 setup.exe -- just to make sure it's legit
|
||||
verifyPackage "$env:PUBLIC\Win11Extract\setup.exe" 'Microsoft Code Signing PCA' "8BFE3107712B3C886B1C96AAEC89984914DC9B6B" "3CAF9BA2DB5570CAF76942FF99101B993888E257" "Windows 11 Setup" "your network location"
|
||||
|
||||
#install
|
||||
start-sleep -Seconds 30
|
||||
if ($env:usrReboot -match 'true') {
|
||||
& "$env:PUBLIC\Win11Extract\setup.exe" /auto upgrade /eula accept /quiet /compat IgnoreWarning /PostOOBE "$env:PUBLIC\cleanup.bat" /showOOBE $env:usrShowOOBE
|
||||
} else {
|
||||
& "$env:PUBLIC\Win11Extract\setup.exe" /auto upgrade /eula accept /quiet /compat IgnoreWarning /PostOOBE "$env:PUBLIC\cleanup.bat" /showOOBE $env:usrShowOOBE /NoReboot
|
||||
}
|
||||
|
||||
#close
|
||||
write-host "================================================================"
|
||||
write-host "`- The Windows 11 Setup executable has been instructed to begin installation."
|
||||
write-host " This Component has performed its job and will retire, but the task is still ongoing`;"
|
||||
write-host " if errors occur with the installation process, logs will be saved automatically in"
|
||||
write-host " $env:WinDir\logs\SetupDiag\SetupDiagResults.xml after the fact."
|
||||
if ($env:usrReboot -match 'true') {
|
||||
write-host " Please be aware that several hours may pass before the device shows visible signs."
|
||||
} else {
|
||||
write-host " Please allow ~4 hours for the setup preparation step to conclude and then reboot the"
|
||||
write-host " device to begin the upgrade process."
|
||||
}
|
||||
```
|
||||
|
||||
## Related Documentation
|
||||
- [Related Windows and Linux Documentation](<../../../reference/Windows and Linux/index.md>) — Find the connected deployments, procedures, and references for this subject.
|
||||
@@ -0,0 +1,16 @@
|
||||
---
|
||||
tags:
|
||||
- Windows and Linux
|
||||
- Scripts
|
||||
- Documentation
|
||||
---
|
||||
|
||||
# Windows and Linux
|
||||
## Purpose
|
||||
Find scripts for windows and linux. Follow the subject guide to choose the relevant environment and connect this material to the other document types.
|
||||
|
||||
## Includes
|
||||
- Windows
|
||||
|
||||
## Follow the Subject
|
||||
[Windows and Linux](<../../reference/Windows and Linux/index.md>) explains the relationships and offers starting points for the documented tasks.
|
||||
Reference in New Issue
Block a user