Bonjour,

Mon but est simplement de lire certaines cellules d'un fichier Excel donné.
Voici mon script :

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
$file = "d:\My Scripts\Result.xlsx"

$row = 1

$Excel = New-Object -Com "Excel.Application"

$Book = $Excel.Workbooks.open($file) | out-null

$CurrentSheet = $Book.Sheets.Item(1)    # ligne 9

do
{
$ServerName = $CurrentSheet.Cells.Item($row,1).value      # ligne 13
$ServerName
$row++
}
While ($row -le 10)

$Excel.close
Problème : a l’exécution, j'obtiens les erreurs suivante :

You cannot call a method on a null-valued expression.
At D:\My Scripts\DisplayHost.ps1:9 char:34
+ $CurrentSheet = $Book.Sheets.Item <<<< (1)
+ CategoryInfo : InvalidOperation: (Item:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At D:\My Scripts\DisplayHost.ps1:13 char:39
+ $ServerName = $CurrentSheet.Cells.Item <<<< ($row,1).value
+ CategoryInfo : InvalidOperation: (Item:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Toute aide est la bienvenue.
Merci d'avance.