0

I try to use this script to purge some log files of my exchange server.

I made just one small change and added the following line so that all Exchange objects are available.

add-pssnapin *exchange* -erroraction SilentlyContinue

When I run the script I get an error when the following lines are executed:

# Get a list of all Exchange 2013 servers
$Ex2013 = Get-ExchangeServer | Where {$_.IsE15OrLater -eq $true} | Sort-Object Name
$logger.WriteEventLog("Script started. Script will purge log files on: $($Ex2013)")
# Lets count the steps for a nice progress bar
$i = 1
$max = $Ex2013.Count * 2 # two actions to execute per server

The last line fires an error. As that the Count property isn't available. I have only one server, and if I execute exact those lines in my Exchange Management Shell this code executes without error.

What is the cause for the different behaviour?

PS: I currently have a workaround just to count the objects. And this works always:

$max = 0
foreach ($E15Server In $Ex2013) {
    $max++
}
$max *= 2
xMRi
  • 14,982
  • 3
  • 26
  • 59
  • 1
    IMO, proper way (if you want array, then say about it): `$Ex2013 = @(Get-ExchangeServer | Where {$_.IsE15OrLater -eq $true} | Sort-Object Name)`. – user4003407 Feb 04 '16 at 13:24
  • Thanks I just found an article that explains the problem of 0 1 and many to me. http://chrisoldwood.blogspot.de/2013/11/the-3-faces-of-powershell-collections-0.html – xMRi Feb 04 '16 at 13:47

0 Answers0