Je débute en PowerShell, et donc pour ne pas commencer trop compliqué j'ai voulu écrire un petit script qui me ressortait des informations utiles concernant des PC/Serveurs que je possède.
Mon script fonctionne, mais s'affiche une erreur que je ne comprend pas totalement.

Voici tout d'abord le code :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
get-content C:\Users\TEST\Documents\Script\Audit\List1.txt |
 `
foreach {
if (test-connection $_ -quiet -count 1)
{
    $model = get-wmiObject Win32_ComputerSystem -comp $_ ;
    $uint32 = get-wmiObject Win32_ComputerSystem -comp $_ ;         
    $os = get-wmiobject Win32_OperatingSystem -comp $_ ;
    $idate = get-wmiobject Win32_OperatingSystem -comp $_ ;
    $serial = get-wmiobject Win32_OperatingSystem -comp $_ ;
    $mac = gwmi win32_networkadapter -comp $_ | ? {$_.macaddress};
    $ip = get-WmiObject Win32_NetworkAdapterConfiguration -comp $_ | Where {$_.Ipaddress.length -gt 1};
    $ipsubnet = get-WmiObject Win32_NetworkAdapterConfiguration -comp $_ | Where {$_.IpSubnet.length -gt 1};
    $domain = get-wmiObject Win32_ComputerSystem -comp $_ ;
    $maxspeed = Get-WmiObject -Class Win32_NetworkAdapter | Where-Object {$_.Speed -ne $null}
    $Gateway = Get-WmiObject Win32_NetworkAdapterConfiguration | ? {$_.DefaultIPGateway}
   
    $result = New-object PSObject
    $result | Add-member NoteProperty Nom $model.name
    $result | Add-member NoteProperty Modèle $model.model
    $result | Add-member NoteProperty Fabricant $model.manufacturer
    $result | Add-member NoteProperty Mémoire ([math]::round($model.totalphysicalmemory/1GB))
    $result | Add-member NoteProperty Processors $uint32.numberofprocessors
    $result | Add-member NoteProperty OS $os.caption
    $result | Add-member NoteProperty IDate ([System.Management.ManagementDateTimeConverter]::ToDateTime($idate.installdate))
    $result | Add-member NoteProperty Serial $serial.serialnumber
    $result | Add-member Noteproperty Mac $mac.macaddress
    $result | Add-member Noteproperty IP $ip.ipaddress[0]
    $result | Add-member Noteproperty IPSubnet $ipsubnet.IpSubnet[0]
    $result | Add-member Noteproperty Domain $domain.Domain
    $result | Add-member Noteproperty MaxSpeed $maxspeed.Speed
    $result | Add-Member Noteproperty Gateway $Gateway.DefaultIPGateway
    

    $result >> C:\Users\TEST\Documents\Script\Audit\results.txt

}

else

    {Write-host "$_ ne répond pas"}

}

(Il est sûrement dégueulasse mais veuillez m'en excuser je n'ai pas encore regarder comment bien ranger tout ça promis je le ferais )


Donc, lorsque je le lance, dans mon "results.txt" j'obtiens mes résultats sous cette forme là :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Nom        : 01SVISIO-PROD1
Modèle     : PowerEdge 1950
Fabricant  : Dell Inc.
Mémoire    : 8
Processors : 2
OS         : Microsoft Windows Server 2008 R2 Standard 
IDate      : 30/01/2014 08:53:13
Serial     : 55041-266-0082486-*****
Mac        : {00:22:19:57:75:0A, 20:41:53:59:4E:FF}
IP         : 10.1.**.**
IPSubnet   : 255.255.254.0
Domain     : test.local
MaxSpeed   : {100000000, 100000}
Gateway    : {10.1.10.254}
(Note : les * sont rajoutés volontairement à l'écriture de ce poste, de même pour le remplacement de "test.local")

Aucun problème jusque là, mais si je rajoute certains serveurs, voici l'erreur :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Indexation impossible dans un tableau Null.
Au caractère C:\Users\TEST\Documents\Script\Audit\Infos.ps1:28 : 5
+     $result | Add-member Noteproperty IP $ip.ipaddress[0]
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation : (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray
 
Indexation impossible dans un tableau Null.
Au caractère C:\Users\TEST\Documents\Script\Audit\Infos.ps1:29 : 5
+     $result | Add-member Noteproperty IPSubnet $ipsubnet.IpSubnet[0]
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation : (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray
Donc je ne comprend pas car sur certains serveurs, ça s'affiche correctement et d'autre le champ IP ou IPSubnet n'apparaît pas dans mes résultats

Merci d'avance pour vos lumières,

Cordialement.