PI Services

Le blog des collaborateurs de PI Services

Windows 11 : Script PowerShell pour activer WinRE en masse

Besoin :

Après un changement d'UPN utilisateur (environnement full Azure AD) sur un Windows 11, le reset de Windows ne fonctionne plus en mentionnant que Windows Recovery Environment (WinRE) est désactivé.

Nous avons besoin d'activer WinRE sur plusieurs devices afin de permettre le reset de Windows 11.

 

Solution :

J'ai utilisé le script PowerShell ci-dessous pour détecter si WinRE est désactivé et l'activer :

function Write-CMLogEntry {
	param (
		[parameter(Mandatory = $true, HelpMessage = "Value added to the log file.")]
		[ValidateNotNullOrEmpty()]
		[string]$Value,
		[parameter(Mandatory = $true, HelpMessage = "Severity for the log entry. 1 for Informational, 2 for Warning and 3 for Error.")]
		[ValidateNotNullOrEmpty()]
		[ValidateSet("1", "2", "3")]
		[string]$Severity,
		[parameter(Mandatory = $false, HelpMessage = "Name of the log file that the entry will written to.")]
		[ValidateNotNullOrEmpty()]
		[string]$FileName = "Enable-WinRE.log"
	)
	# Determine log file location
	$WinTemp = [System.Environment]::GetEnvironmentVariable('TEMP','Machine')
	$LogFilePath = "$WinTemp\$FileName"
	
	# Construct time stamp for log entry
	$Time = -join @((Get-Date -Format "HH:mm:ss.fff"), "+", (Get-WmiObject -Class Win32_TimeZone | Select-Object -ExpandProperty Bias))
	
	# Construct date for log entry
	$Date = (Get-Date -Format "MM-dd-yyyy")
	
	# Construct context for log entry
	$Context = $([System.Security.Principal.WindowsIdentity]::GetCurrent().Name)
	
	# Construct final log entry
	$LogText = "<![LOG[$($Value)]LOG]!><time=""$($Time)"" date=""$($Date)"" component=""Enable-WinRE.log"" context=""$($Context)"" type=""$($Severity)"" thread=""$($PID)"" file="""">"
	
	# Add value to log file
	try {
		Out-File -InputObject $LogText -Encoding Default -FilePath $LogFilePath -ErrorAction Stop
	}
	catch [System.Exception] {
		Write-Warning -Message "Unable to append log entry to $FileName file. Error message: $($_.Exception.Message)"
	}
}

#WinRE Status
if (reagentc /info | findstr "Disabled") {
	reagentc /enable
	Write-CMLogEntry -Value "Enabling Windows Recovery Environment" -Severity 2
}
else {
	Write-CMLogEntry -Value "Windows Recovery Environment is Already Enabled" -Severity 1
}

 

Ajouter un commentaire

Loading