script do powershell para criar um relatório de troca

1

Eu tenho um script escrito no PowerShell e tenho algum problema. Quando executo este script, tenho algum erro na saída:

You cannot call a method on a null-valued expression.
At C:\scripts\exchange_rep_work_cp.ps1:133 char:82
+ $mbx | add-member -membertype noteproperty -value $stats.TotalItemSize.Value.ToMB <<<< () -name TotalSize
+ CategoryInfo : InvalidOperation: (ToMB:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.

Este erro é repetido algumas vezes. Esta é a linha de código (133):

130 $tmp = ""
131 $Mailboxes | foreach {
132 #Name_of_company has a diffrent domain and domain controller
133 if ($Company -eq "Name_of_company"){
134 $stats = get-mailboxstatistics -DomainController zzzz.xxx.yyyyyy.local -id $_ | where >135 {$.ObjectClass –eq “Mailbox”} }
136 else {
137 $stats = get-mailboxstatistics -id $
| where {$.ObjectClass –eq “Mailbox”}}
138 $MBx = new-object system.object
139 $tmp = $
.EmailAddresses | foreach { if ($_ -notmatch "yyyyyy.local") { $.AddressString } }
140: $Country = $
.Customattribute1
141: $Types = $_.Customattribute2
142: $mbx | add-member -membertype noteproperty -value $stats.Displayname -name DisplayName
143: $mbx | add-member -membertype noteproperty -value $stats.TotalItemSize.Value.ToMB() -name TotalSize

Por favor, dê-me alguns conselhos sobre o que está errado

Atenciosamente

    
por spy86 24.09.2013 / 12:55

2 respostas

1

Ele diz que você está tentando chamar o método ToMB em uma string chamada $stats.TotalItemSize.Value . Este é um [string] e não tem esse método.

    
por 15.01.2014 / 07:00
0

Você executa o script em um computador com o Shell de Gerenciamento do Exchange instalado? Isso também inclui alguns tipos de dados que o Exchange usa para representar tamanhos e, sem eles, esses erros são bastante comuns. Um teste válido seria verificar se o erro está presente ao executá-lo em um servidor Exchange.

Além disso, seria interessante ver seu código para se conectar à instância remota do Exchange.

EDITAR: Seu código está faltando alguns sublinhados aqui e ali, então, em alguns casos, o objeto stats nunca será preenchido. Aqui está uma breve explicação:

$a = "" | select Name, Value
$a.Name = "Trond"
$a.value = "Yes"

$array = @()
$array += $a

#error
$array | where {$.Name -eq "Trond"}

#success
$array | where {$_.Name -eq "Trond"}

Então, resumindo: esta linha:

$stats = get-mailboxstatistics -id $ | where {$.ObjectClass –eq “Mailbox”}}

Nunca produzirá um objeto "$ stats", o que suspeito ser o motivo do seu código falhar. Se você estiver usando o PowerShell 3.0 ISE, você pode ver que o ISE não "entende" sua filtragem e mostra que não colorem o cifrão vermelho.

    
por 24.09.2013 / 14:17