Function 更改PowerShell中foreach函数中的变量名

Function 更改PowerShell中foreach函数中的变量名,function,powershell,foreach,Function,Powershell,Foreach,我有一个脚本,其中一部分需要运行三次不同的时间,因此我想我应该尝试扩展我有限的PowerShell知识,通过使用函数调用相同的代码,而不是一次又一次地复制和粘贴,并制作一个比所需更长的脚本 我希望在函数中重复使用的代码: $users = Get-Content users.txt foreach ($user in $users){ # Get some information from Exchange about the user $dn = (Get-MailboxSta

我有一个脚本,其中一部分需要运行三次不同的时间,因此我想我应该尝试扩展我有限的PowerShell知识,通过使用函数调用相同的代码,而不是一次又一次地复制和粘贴,并制作一个比所需更长的脚本

我希望在函数中重复使用的代码:

$users = Get-Content users.txt
foreach ($user in $users){
    # Get some information from Exchange about the user
    $dn = (Get-MailboxStatistics -id $user).displayname
    $ic = (Get-MailboxStatistics -id $user).itemcount

    # Make a hash table where user=itemcount
    $firstrun += @{"$dn"="$ic"} # Each time the script runs, we
                                # need a different hash table

    # Kick off some Exchange maintenance on the user. (Removed
    # to keep the post shorter)
    # Item count should lower after a few seconds.
}
当代码重复第二次和第三次时,我希望创建一个新的哈希表(“secondrun”和“thirdrun”)。我的第一个问题是每次更改函数中哈希表名称的名称-可以这样做吗

我也开始怀疑哈希表是否是适合这份工作的工具,或者是否有更好的工具?关于更多的背景,在我有了第二个哈希表之后,我想做一个比较:

foreach ($user in $users){
    $c1 = $firstrun.get_item($user)
    $c2 = $secondrun.get_item($user)

    # If the item count hasn't substantially dropped
    if ($c2 -ge $c1){
        # Do some different Exchange tasks on the user (removed
        # to keep the post shorter)
    }
}
最后是第三次运行,它将创建第三个哈希表(同样,user=itemcount)。然后,我将使用每个哈希表中的值向文本文件输出某种报告


我想在这个阶段我有两个主要的问题——函数中哈希表的变量名不断变化,而且在函数运行后维护哈希表也有困难——试图像全局变量一样声明它们似乎不起作用。我对如何更好地解决这些问题持开放态度。

如果我是你,我会怎么做?我假设哈希表的名称是什么并不重要。如果是这样的话,您可以抓取当前日期时间并用它来命名您的哈希表,如

$HashTblName = "HashTbl_$($(get-date).ToString("yyyyMMddhhmmssff"))"

如果我明白你的意思,你就在做以下事情:

  • 填充将用户集映射到其项目计数的哈希表
  • 做一些修剪项目的事情
  • 重新生成哈希表
  • 比较步骤1和3中生成的哈希表;再按名单行事
  • 重新生成哈希表
  • 根据所有三个表生成报告
  • 从上面的列表中可以看出,您确实希望生成一个函数来生成哈希表并返回它:

    function Get-UsersItemCount
    {
        $ht = @{}
        $users = Get-Content users.txt
        foreach ($user in $users){
            # Get some information from Exchange about the user
            $dn = (Get-MailboxStatistics -id $user).displayname
            $ic = (Get-MailboxStatistics -id $user).itemcount
    
            # Make a hash table where user=itemcount
            $ht += @{"$dn"="$ic"}
        }
    
        $ht # Returns the hashtable
    }
    
    现在可以调用此函数三次:

    $firstrun = Get-UsersItemCount
    
    # Do first run stuff
    $secondrun = Get-UsersItemCount
    
    # Do second run stuff
    $thirdrun = Get-UsersItemCount
    
    # Generate your report
    

    您可以只使用一个哈希表,使值成为一个数组,每个过程有一个元素:

    $ht = @{}
    
    $users = Get-Content users.txt
    foreach ($user in $users){
        # Get some information from Exchange about the user
        $stats = Get-MailboxStatistics $user |
                   select -expand itemcount
        $ht[user] += @($stats)}
    }
    
    # Kick off some Exchange maintenance on the user. (Removed to
    # keep post shorter)
    # Item count should lower after a few seconds.
    
    foreach ($user in $users){
        # Get some information from Exchange about the user
        $stats = Get-MailboxStatistics $user |
                   select -expand itemcount
        $ht[user] += @($stats)
    
        # If the item count hasn't substantially dropped
        if ($ht[$user][1] -ge $ht[$user][0])
            # Do some different Exchange tasks on the user (removed
            # to keep the post shorter)
    }
    

    有趣-这看起来也是有效的选择。我还刚刚从您那里学到了比较数组中元素的方法-谢谢!