Increase a Value in a Hashtable

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Dave Wyatt Dave Wyatt 3 years ago.

  • Author
    Posts
  • #14090
    Profile photo of Dominique Hofmann
    Dominique Hofmann
    Participant

    Hello Together
    I'm working on a script, which make a list with all the handy's of our company and and compare the OS of the handys with a list(hashtable) with OS from Apple.
    For every match the Os in the hashtable will be increased to 1.

    And i have no idea how i could make, that the OS in the Hashtable will be increased to 1.

    Here is the script:

    $s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://srv00832/powershell -Authentication Kerberos

    Import-PSSession $s -EA SilentlyContinue

    $Zaehler = 0

    $iPhoneiOS = 0

    $AppleGeraete = 0

    $iPhoneHashiOS = @{"iOS 6.0*" = 0; "iOS 6.1 *" = 0; "iOS 6.1.1 *" = 0; "iOS 6.1.2 *" = 0; "iOS 6.1.3 *" = 0; "iOS 7.0 *" = 0; "iOS 7.0.2 *" = 0; "iOS 7.0.3 *" = 0; "iOS 7.0.4 *" = 0; "iOS 7.1 *" = 0}

    [Array]$ADUser = Get-CASMailbox -ResultSize 20 | Where {$_.HasActiveSyncDevicePartnership -eq $TRUE}

    foreach ($User in $ADUser)

    {

    $Zaehler++

    $AcSyDeStatisticList = Get-ActiveSyncDeviceStatistics -Mailbox $($User.SamAccountName)

    foreach ($AcSyDeStatistic in $AcSyDeStatisticList)

    {

    $LastSyncTime = $($AcSyDeStatistic.LastSuccessSync)

    If ($LastSyncTime -ne $null)

    {

    $LastSyncDiff = $LastSyncTime - $(Get-Date)

    If ($LastSyncDiff -gt $Zeitraum)

    {

    $iPhoneHashiOS | foreach-object {if($AcSyDeStatistic.DeviceOS -ne $iPhoneHashiOS){$iPhoneHashiOS.Add($AcSyDeStatistic.DeviceOS, 1)}

    $iPhoneHashiOS = $iPhoneHashiOS | select -Unique

    if (($($AcSyDeStatistic.DeviceOS) -eq $null) -and ($($AcSyDeStatistic.DeviceUserAgent) -like "Apple*")){$iPhoneiOS++}

    if ($($AcSyDeStatistic.DeviceUserAgent) -like "Apple*"){$AppleGeraete++}

    }

    }

    }

    }
    I hope that someone can help me

    bye Dominique

    P.S: Sorry for my bad english

  • #14092
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Focusing on these two lines of code, since that's where you've gone wrong:

    $iPhoneHashiOS | foreach-object {if($AcSyDeStatistic.DeviceOS -ne $iPhoneHashiOS){$iPhoneHashiOS.Add($AcSyDeStatistic.DeviceOS, 1)}
    $iPhoneHashiOS = $iPhoneHashiOS | select -Unique
    

    Couple of comments on this: One, hashtables in PowerShell don't automatically enumerate their contents with a foreach loop (and when you pipe a hashtable to ForEach-Object, it will only execute once, with the input being the entire hashtable.) When you want to enumerate the contents of a hashtable in PowerShell, you either need to use its Keys property or the GetEnumerator() method. Also, it looks like your hash table keys contain wildcards, so you probably want to use the -like operator when comparing them to your DeviceOS string. Try this:

    foreach ($key in [object[]]$iPhoneHashiOS.Keys)
    {
        if ($AcSyDeStatistic.DeviceOS -like $key)
        {
            $iPhoneHashiOS[$key]++
        }
    }
    

    One thing to notice here is that I casted $iPhoneHashiOS.Keys to [Object[]] ; this is because the Keys property returns an enumerator, not an array. If you change the hashtable in the loop, you get errors. Casting the enumerator to an array is a fairly short way to avoid this problem.

You must be logged in to reply to this topic.