簡體   English   中英

在PowerShell中更改foreach函數中的變量名稱

[英]Changing variable name in foreach function in PowerShell

我有一個腳本,其中一部分需要運行三次不同的時間,所以我想我會嘗試通過使用函數調用相同的代碼而不是一次又一次地復制和粘貼來擴展我有限的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)。 然后,我將使用每個哈希表中的值將某種報告輸出到文本文件。

我想在這個階段我有兩個主要問題 - 函數中的哈希表有一個變化的變量名,而且在函數運行后我很難維護哈希表 - 試圖將它們聲明為像全局變量一樣似乎工作。 我願意接受有關如何更好地完成任何工作的想法。

如果我理解你在說什么,你就是在做以下事情:

  1. 填充將用戶集映射到其項目計數的哈希表。
  2. 做一些修剪物品的東西
  3. 重新生成哈希表
  4. 比較步驟1和3中生成的哈希表; 再次列入清單。
  5. 重新生成哈希表
  6. 根據所有三個表生成報告

從上面的列表中可以看出,您確實希望生成一個生成哈希表並返回它的函數:

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)
}

如果我是你,我會怎么做? 我假設哈希表的名稱是什么並不重要。 如果是這種情況,您可以獲取當前日期時間並使用它來命名您的哈希表,例如

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM