Powershell-fonctions

De wiki.nexiat.fr
Aller à la navigation Aller à la recherche
Fiche express
Domaine Exemples de fonctions PowerShell
Thèmes WMI, information système
Voir aussi Get-WmiObject

Recueil de fonctions PowerShell utiles pour l'administration système.

Qui est connecté

function QuiEstConnecte {
    Write-Output ([System.Security.Principal.WindowsIdentity]::GetCurrent()).Name
    # Retourne une chaîne de la forme Domaine\User
}

Get-OSInfo

function Get-OSInfo {
    param ([String]$ComputerName = '.')

    $infos = Get-WmiObject Win32_OperatingSystem -ComputerName $ComputerName

    $infos | Select-Object -Property `
        @{Name='ComputerName';     Expression = { $_.Csname }},
        @{Name='OS';               Expression = { $_.Caption }},
        @{Name='ServicePack';      Expression = { $_.CsdVersion }},
        @{Name='InstallationDate'; Expression = { [System.Management.ManagementDateTimeConverter]::ToDateTime($_.InstallDate) }}
}
Get-OSInfo | Format-Table -AutoSize
Get-OSInfo -ComputerName MachineDistante
'Machine1','Machine2','Machine3' | ForEach-Object { Get-OSInfo -ComputerName $_ }

Dernier démarrage

$LastBootTime = (Get-WmiObject Win32_OperatingSystem).LastBootUpTime
[System.Management.ManagementDateTimeConverter]::ToDateTime($LastBootTime)

Récupérer la clé produit Windows

# Table de conversion base 24
$map = "BCDFGHJKMPQRTVWXY2346789"

# Lecture de la clé de registre
$value = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").DigitalProductId[0x34..0x42]

# Affichage brut (Raw Key, big endian)
$hexa = ""
$value | ForEach-Object { $hexa = $_.ToString("X2") + $hexa }
Write-Output "Raw Key Big Endian: $hexa"

# Calcul du Product Key
$ProductKey = ""
for ($i = 24; $i -ge 0; $i--) {
    $r = 0
    for ($j = 14; $j -ge 0; $j--) {
        $r = ($r * 256) -bxor $value[$j]
        $value[$j] = [math]::Floor([double]($r / 24))
        $r = $r % 24
    }
    $ProductKey = $map[$r] + $ProductKey
    if (($i % 5) -eq 0 -and $i -ne 0) { $ProductKey = "-" + $ProductKey }
}
Write-Output "Product Key: $ProductKey"

Voir aussi