Bonjour,
Je suis sur que cette question a déjà été posée, mais je ne retrouve pas de post sur le site.
J'ai fait un script powershell, quand je le lance "à la main" mon fichier Excel est correctement généré pas de soucis, mais après l'avoir encapsulé dans une tâche planifié, cela ne fonctionne plus...
Voici mon code :
Code PowerShell : 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
$log = "E:\infoBaL.log"
(Get-Date).ToString("dd/MM/yyyy - hh:mm:ss") > $log
"--- Début du script ---" >> $log 
"Initialisation des variables..." >> $log
 
$expediteur = "moi@moi.fr"
$destinataire = "moi@moi.fr"
$serveur = "monserveur.de.mail"
$objetmail = "Info boites mail "
$texte = "Bonjour, `n Voici les informations sur les boîtes mails (voir fichier en PJ). `n Bonne journée ! "
$objet = $objetmail + " - " + (Get-Date).ToString("dd/MM/yyyy")
 
#init date et fichier de sorti
$dateTxt = (Get-Date).ToString("yyyyMMdd")
$fichier = "E:\infoBaL_"+$dateTxt+".xlsx"
 
 
(Get-Date).ToString("dd/MM/yyyy - hh:mm:ss") >> $log
"Début de construction du fichier Excel..." >> $log 
 
# constrcution d'un excel
$excel = new-object -comobject Excel.Application 
$excel.visible = $False
$excel.DisplayAlerts = $False
 
#création d'une feuille dans excel
$work = $excel.Workbooks.Add()
$workbook = $work.Worksheets.Item(1)
$work.SaveAs($fichier)
$work >> $log
 
# creation des entetes de colonnes
$workbook.Cells.Item(1,1) = 'Nom - Prenom'
$workbook.Cells.Item(1,1).Font.Bold=$True
$workbook.Cells.Item(1,1).Interior.ColorIndex = 23
$workbook.Cells.Item(1,2) = 'Mail'
$workbook.Cells.Item(1,2).Font.Bold=$True
$workbook.Cells.Item(1,2).Interior.ColorIndex = 23
$workbook.Cells.Item(1,3) = 'Total Size (MB)'
$workbook.Cells.Item(1,3).Font.Bold=$True
$workbook.Cells.Item(1,3).Interior.ColorIndex = 23
$workbook.Cells.Item(1,4) = 'Quota (MB)'
$workbook.Cells.Item(1,4).Font.Bold=$True
$workbook.Cells.Item(1,4).Interior.ColorIndex = 23
$workbook.Cells.Item(1,5) = 'Utilisation (%)'
$workbook.Cells.Item(1,5).Font.Bold=$True
$workbook.Cells.Item(1,5).Interior.ColorIndex = 23
$row=2
 
 
#creation du tableau de test avec entete (et initialisation a 0 donc increment de 1 du compteur juste apres):
$table = ,@(0, 'Nom Prenom', 'Mail', 'Total Size (MB)', 'Quota')
$table_cpt ++
 
 
(Get-Date).ToString("dd/MM/yyyy - hh:mm:ss") >> $log
"Entete de fichier Excel terminé." >> $log 
" --- MailBox CODIR elargi ---" >> $log
"Recupération des mailbox puis parcours et insertion dans le fichier Excel..." >> $log
 
#recuperation de la liste des BaL :
$listeBal= Get-Content ("E:\ListeBaL.20200320.txt")
 
#parcours de BaL 
foreach($bal in $listeBal) 
{ 
 # Write-Host $bal 
 $mailbox = Get-Mailbox -Identity $bal
 $table += ,@($table_cpt, $mailbox.Name, $bal, (Get-MailboxStatistics $bal).TotalItemSize.Value.ToMB(), $mailbox.ProhibitSendQuota)
 $table_cpt ++
 
 #insertion Excel
 $col=1
  $workbook.Cells.Item($row,$col)=$mailbox.Name
 $col=2
  $workbook.Cells.Item($row,$col)=$bal
 $col=3
  $size = (Get-MailboxStatistics $bal).TotalItemSize.Value.ToMB()
  $workbook.Cells.Item($row,$col)=$size
 $col=4
 
 $quota = $mailbox.ProhibitSendQuota.Split(" ")
 if($quota[1] -eq "GB")
 {
   $quotaMB=($quota[0] -as [float])*1024
 }
 else
 {
   $quotaMB=$quota[0]
 }
 $workbook.Cells.Item($row,$col)=$quotaMB
 
 $col=5
  $utilisation=($size -as [float])*100/($quotaMB -as [float])
  $workbook.Cells.Item($row,$col)=$utilisation
  if( $utilisation -ge 75)
  {
    $workbook.Cells.Item($row,$col).Interior.ColorIndex = 3
  }
 #Ecriture dans le fichier de log de la BaL traitée
 $mailbox.Name + " : " + $bal >> $log
 (Get-Date).ToString("dd/MM/yyyy - hh:mm:ss") >> $log
 
 $row ++
 
}
 
 
$work.Save()
$work.Close()
$excel.Quit()
 
(Get-Date).ToString("dd/MM/yyyy - hh:mm:ss") >> $log
"Fin d'insertion dans le fichier Excel." >> $log
 
# tests d'affichage du resultat :
$table | Out-GridView
# 
# foreach($ligne in $table)
# {
#   Write-host ($ligne)
# }
# 
# $table | Export-csv -path $fichier
# INFO : l'export-csv du $table ne fonctionne pas...
 
(Get-Date).ToString("dd/MM/yyyy - hh:mm:ss") >> $log
"Envoi email..." >> $log
 
 
  $message = new-object System.Net.Mail.MailMessage $expediteur, $destinataire, $objet, $texte
  $client = new-object System.Net.Mail.SmtpClient $serveur
  $attachment = new-object System.Net.Mail.Attachment $fichier 
  $message.Attachments.Add($attachment)
  $client.Send($message)
 
 
(Get-Date).ToString("dd/MM/yyyy - hh:mm:ss") >> $log
"--- FIN ---" >> $log