Inventaire powershell

De wiki.nexiat.fr
Aller à la navigation Aller à la recherche
Fiche express
Domaine Inventaire / WMI
Base Get-WmiObject
Voir aussi Get-wmiobject

Exemple de script d'inventaire de parc reposant sur Get-wmiobject : système d'exploitation, numéro de série, modèle et cartes réseau. Chaque information est encapsulée dans une fonction acceptant un ou plusieurs noms de machines.

Script

#Requires -Version 2

function Get-OSInfo
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$False, ValueFromPipeline=$true)]
        [String[]]$ComputerName = '.'
    )
    Process
    {
        foreach ($computer in $ComputerName)
        {
            $infos = Get-WmiObject Win32_OperatingSystem -ComputerName $computer
            $infos | Select-Object -Property `
                @{Name='ComputerName';     Expression={$_.CSName}},
                @{Name='OS';               Expression={$_.Caption}},
                @{Name='ServicePack';      Expression={$_.CSDVersion}},
                @{Name='InstallationDate'; Expression={[System.Management.ManagementDateTimeConverter]::ToDateTime($_.InstallDate)}}
        }
    }
}

function Get-Serial
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$False, ValueFromPipeline=$true)]
        [String[]]$ComputerName = '.'
    )
    Process
    {
        foreach ($computer in $ComputerName)
        {
            Get-WmiObject -Class Win32_BIOS -Namespace "root\CIMV2" -ComputerName $computer |
                Select-Object -Property @{Name='Serial'; Expression={$_.SerialNumber}}
        }
    }
}

function Get-Model
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$False, ValueFromPipeline=$true)]
        [String[]]$ComputerName = '.'
    )
    Process
    {
        foreach ($computer in $ComputerName)
        {
            Get-WmiObject Win32_ComputerSystem -ComputerName $computer |
                Select-Object -Property @{Name='Model'; Expression={$_.Model}}
        }
    }
}

function Get-Network
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$False, ValueFromPipeline=$true)]
        [String[]]$ComputerName = '.'
    )
    Process
    {
        foreach ($computer in $ComputerName)
        {
            Get-WmiObject -Class Win32_NetworkAdapter -ComputerName $computer |
                Format-Table -Property ServiceName, MacAddress, Name, Speed -AutoSize
        }
    }
}

$serveur = 'Serveur1'

# echo $serveur | foreach { Get-OSInfo -ComputerName $_ } | Export-Csv -Path "C:\Temp\inventaire.csv" -Delimiter ";" -NoTypeInformation
# echo $serveur | foreach { Get-Serial -ComputerName $_ } | Format-List
# echo $serveur | foreach { Get-Model  -ComputerName $_ } | Format-List
echo $serveur | foreach { Get-Network -ComputerName $_ }

echo ""
echo "FIN de l'inventaire !"

Voir aussi