Powershell : récupération des informations sur le(s) pagefile.sys

Dans l’idée du billet d’hier concernant la résolution de problématiques d’espace disque, j’ai codé rapidement un petit script Powershell pour récupérer les informations système propres au(x) fichiers de pagination.

En passant en argument du script un nom de machine, on récupère le chemin du fichier, sa taille de base, l’utilisation actuelle et maximale de celui-ci.

Param
([string]$Comp)

$pfsys = Get-WmiObject Win32_PagefileUsage -Computer $Comp | select Name, CurrentUsage, PeakUsage, AllocatedBaseSize
foreach ($pf in $pfsys)
{
echo "Location: "$pf.Name
echo "File base size in MB: "$pf.AllocatedBaseSize
echo "Current usage: "$pf.CurrentUsage
echo "Max. usage: "$pf.PeakUsage
}

Powershell : désactivation de TLS 1.0 et 1.1, activation de TLS 1.2

En lien direct avec le billet d’hier, j’ai donc concocté un script permettant d’automatiser les modifications requises en base de registre pour l’activation de TLS 1.2 au profit de TLS 1.0 et 1.1.

Naturellement, le script est à exécuter avec un compte d’utilisateur ayant les droits d’écrire en base de registre.

echo "This script will create or write over some registry keys related to TLS 1.0, TLS 1.1 and TLS 1.2 - make sure to be aware of the effect of those modifications."
$continue = Read-Host "Are you sure you want to execute this script ? Type 'Y' to continue or any other character to abort"
if ($continue -ne 'Y') {break}
$pathtls10 = "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.0"
$pathtls11 = "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.1"
$pathtls12 = "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2"
$pathprtcl = "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\"
echo "Creating TLS keys..."
New-Item -Path $pathprtcl -Name "TLS 1.0" -Force | Out-Null
New-Item -Path $pathprtcl -Name "TLS 1.1" -Force | Out-Null
New-Item -Path $pathprtcl -Name "TLS 1.2" -Force | Out-Null
New-Item -Path $pathtls10 -Name "Server" -Force | Out-Null
New-Item -Path $pathtls10 -Name "Client" -Force | Out-Null
New-Item -Path $pathtls11 -Name "Server" -Force | Out-Null
New-Item -Path $pathtls11 -Name "Client" -Force | Out-Null
New-Item -Path $pathtls12 -Name "Server" -Force | Out-Null
New-Item -Path $pathtls12 -Name "Client" -Force | Out-Null
echo "Adding DWORD properties..."
New-ItemProperty -Path $pathtls10Client -PropertyType DWORD -Name "DisabledByDefault" -Value "0x00000000" -Force | Out-Null
New-ItemProperty -Path $pathtls10Server -PropertyType DWORD -Name "DisabledByDefault" -Value "0x00000000" -Force | Out-Null
New-ItemProperty -Path $pathtls11Client -PropertyType DWORD -Name "DisabledByDefault" -Value "0x00000000" -Force | Out-Null
New-ItemProperty -Path $pathtls11Server -PropertyType DWORD -Name "DisabledByDefault" -Value "0x00000000" -Force | Out-Null
New-ItemProperty -Path $pathtls12Client -PropertyType DWORD -Name "DisabledByDefault" -Value "0x00000000" -Force | Out-Null
New-ItemProperty -Path $pathtls12Server -PropertyType DWORD -Name "DisabledByDefault" -Value "0x00000000" -Force | Out-Null
New-ItemProperty -Path $pathtls10Client -PropertyType DWORD -Name "Enabled" -Value "0x00000000" -Force | Out-Null
New-ItemProperty -Path $pathtls10Server -PropertyType DWORD -Name "Enabled" -Value "0x00000000" -Force | Out-Null
New-ItemProperty -Path $pathtls11Client -PropertyType DWORD -Name "Enabled" -Value "0x00000000" -Force | Out-Null
New-ItemProperty -Path $pathtls11Server -PropertyType DWORD -Name "Enabled" -Value "0x00000000" -Force | Out-Null
New-ItemProperty -Path $pathtls12Client -PropertyType DWORD -Name "Enabled" -Value "0x00000001" -Force | Out-Null
New-ItemProperty -Path $pathtls12Server -PropertyType DWORD -Name "Enabled" -Value "0x00000001" -Force | Out-Null
echo "Done."
$reboot = Read-Host "Server has to reboot in order to apply the modifications made. Type 'Y' if you want to start a reboot countdown of 2 minutes."
if ($reboot -ne 'Y') {break}
shutdown -r -t 120
echo "Server will reboot in 2 minutes."

Si jamais l’accès Terminal Server est impossible au redémarrage, il faut contrôler les réglages de sécurité du serveur RDP et être sûr que le KB3080079 est installé si l’OS est Windows Server 2008 R2. Tout est expliqué dans l’article que j’ai publié hier sur les manipulations d’origine.

Powershell : reset de Windows Update

Toujours dans la lignée de la campagne de patchs pour Meltdown et Spectre, j’ai développé un script permettant d’effectuer les manipulations nécessaires à remettre le composant Windows Update en bon état de marche. Il arrive que le service Windows Update soit incapable de trouver des patchs à installer ou qu’il bute sur des erreurs lors de l’installation des patchs. Une fois qu’on a écarté les causes système habituelles (pas de contact du serveur WSUS, espace disque insuffisant sur le système, service non démarré…), il peut être nécessaire de supprimer les fichiers de Windows Update pour le forcer à reprendre comme si le serveur était fraîchement installé, sans que cela ne retire les patchs, bien évidemment.

J’ai donc développé un script en Powershell qui va couper les services, renommer – et pourquoi pas – supprimer ces répertoires.

echo "Stopping Cryptographic Service Provider."
net stop cryptsvc
echo "Stopping the Windows Update service."
net stop wuauserv
echo "Checking presence of .old directories."
if ((Test-Path C:\Windows\SoftwareDistribution.old) -eq $true) {
$delSD = Read-Host -Prompt "The script needs to delete C:\Windows\SoftwareDistribution.old which probably is a backup of the SoftwareDistribution folder. Type 'N' if you want to abort."
if ($delSD -eq 'N') {break}
Remove-Item C:\Windows\SoftwareDistribution.old -Force -Recurse
}
if ((Test-Path C:\Windows\System32\catroot2.old) -eq $true) {
$delCR = Read-Host -Prompt "The script needs to delete C:\Windows\System32\catroot2.old which probably is a backup of the catroot2 folder. Type 'N' if you want to abort."
if ($delCR -eq 'N') {break}
Remove-Item C:\Windows\System32\catroot2.old -Force -Recurse
}
echo "Renaming WindowsSoftwareDistribution to SoftwareDistribution.old"
$SD = $true
try {
Rename-Item -Path C:\Windows\SoftwareDistribution -NewName C:\Windows\SoftwareDistribution.old -ErrorAction Stop
}
catch {
echo "Folder doesn't exist, can't rename."
$SD = $false
}
echo "Renaming Windows\System32\catroot2 to catroot2.old"
$CR = $true
try {
Rename-Item -Path C:\Windows\System32\catroot2 -NewName C:\Windows\System32\catroot2.old -ErrorAction Stop
}
catch {
echo "Folder doesn't exist, can't rename."
$CR = $false
if ($SD -eq $false -And $CR -eq $false) {
echo "Both folders are missing. Aborting."
break
}
}
$delYN = Read-Host -Prompt "Do you want to delete the old folders ? The script will only do so if you answer 'Y'"
if ($delYN -eq 'Y')
{
if ($SD -eq $true) {Remove-Item C:\Windows\SoftwareDistribution.old -Force -Recurse}
if ($CR -eq $true) {Remove-Item C:\Windows\System32\catroot2.old -Force -Recurse}
}
echo "Starting the services previously stopped."
net start cryptsvc
net start wuauserv
echo "Done."

Powershell : récupération d’une liste des comptes utilisateurs et ordinateurs modifiés depuis une date donnée

Ne rencontrant pas de problèmes particuliers en ce moment, je prends un peu de temps pour scripter et être un peu plus à l’aise avec PowerShell et essayer de me constituer une petite banque de scripts pouvant être utiles.

Dans ce but, j’ai écrit un script listant les objets comptes utilisateurs et ordinateurs de l’ActiveDirectory qui ont été modifiés depuis la date donnée lors de l’exécution du script. Ce dernier génère deux fichiers, l’un contenant les informations par rapport aux objets « user » et l’autre aux objets « computer », avec leur nom, l’OU conteneur ainsi que le timestamp de dernière modification.

Write-Host "Fill in the date."
$D = Read-Host -Prompt 'Day'
$M = Read-Host -Prompt 'Month'
$Y = Read-Host -Prompt 'Year'
$ChangeDate=New-Object DateTime($Y,$M,$D)
Write-Host "Getting modified users..."
$ModifiedUsers = Get-ADObject -Filter 'whenChanged -gt $ChangeDate -and ObjectClass -eq "user" -and ObjectCategory -eq "person"' -Properties whenChanged | FT Name,DistinguishedName,WhenChanged
$ModifiedUsers > modifiedusers-since-$Y$M$D.txt
Write-Host "Getting modified computers..."
$ModifiedComps = Get-ADObject -Filter 'whenChanged -gt $ChangeDate -and ObjectClass -eq "computer"' -Properties whenChanged | FT Name,DistinguishedName,WhenChanged
$ModifiedComps > modifiedcomps-since-$Y$M$D.txt