1

スクリプトの一部を 3 回実行する必要があるため、コピー アンド ペーストを何度も繰り返し、より長いコードを作成するのではなく、関数を使用して同じコードを呼び出すことで、限られた 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.
}

コードが 2 回目と 3 回目を繰り返すときに、新しいハッシュ テーブルが作成されるようにします ("secondrun" と "thirdrun")。私の最初の問題は、関数内のハッシュテーブル名の名前を毎回変更することです-これはできますか?

また、ハッシュ テーブルがこの仕事に適したツールなのか、それとももっと優れたツールがあるのだろうかと考え始めました。もう少し背景として、2 番目のハッシュ テーブルを取得した後、比較を行います。

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

そして最後に、単純に 3 番目のハッシュ テーブルを作成する 3 番目の実行があります (ここでも、user=itemcount)。次に、各ハッシュ テーブルの値を使用して、何らかのレポートをテキスト ファイルに出力します。

この段階では、2 つの主な問題があると思います。関数内のハッシュ テーブルの変数名が変更されていることと、関数の実行後にハッシュ テーブルを維持するのが難しいことです。グローバル変数のようにそれらを宣言しようとしても、動作するようです。これを改善する方法についてのアイデアをお待ちしています。

4

3 に答える 3

2

あなたの言っていることが理解できれば、あなたは次のことをしています。

  1. ユーザーのセットをアイテム数にマップするハッシュ テーブルを設定します。
  2. アイテムを剪定する何かをする
  3. ハッシュ テーブルを再生成する
  4. ステップ 1 と 3 で生成されたハッシュ テーブルを比較します。再びリストに基づいて行動します。
  5. ハッシュ テーブルを再生成する
  6. 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
}

これで、この関数を 3 回呼び出すことができます。

$firstrun = Get-UsersItemCount

# Do first run stuff
$secondrun = Get-UsersItemCount

# Do second run stuff
$thirdrun = Get-UsersItemCount

# Generate your report
于 2013-03-12T00:34:00.570 に答える
1

1 つのハッシュ テーブルを使用して、値を配列にし、パスごとに 1 つの要素を使用できます。

$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)
}
于 2013-03-12T00:54:16.620 に答える
0

私があなただったらどうしますか?ハッシュテーブルの名前が何であるかは問題ではないと仮定します。その場合は、現在の日時を取得し、それを使用してハッシュ テーブルに名前を付けることができます。

$HashTblName = "HashTbl_$($(get-date).ToString("yyyyMMddhhmmssff"))"
于 2013-03-12T00:31:26.767 に答える