2016-08-12 400 views
-1

沒問題。只是分享。道歉,如果這是錯誤的場地和/或方法,但我已經從這些論壇採取了很多,只是想稍微回過頭來,以防其他人喜歡試圖學習PS並在PS中做一些基本上有用的事情。道歉,但我必須通過編輯器運行腳本。反正這裏是:Powershell腳本來監控DFSR積壓

################################################################################################### 
# File: DFSR-check-3-show-backlog.ps1 
# Desc: Simple monitor of DFSR backlog. 
# 
# Vers Date  Who Description 
# ---- ----  --- ----------- 
# v0.01 11-Aug-2016 sdo Initial draft, based on commands from BM. 
# v0.02 12-Aug-2016 sdo Use "Out-String" to trim the blank lines of the table. 
# v0.03 12-Aug-2016 sdo Extract count from verbose output if "100" items are returned. 
# v0.04 12-Aug-2016 sdo Write to a log file. 
# v0.05 12-Aug-2016 sdo Only display when different or every 100 entries. 
# v0.06 12-Aug-2016 sdo Same layout and counter as other two scripts. 
# v0.07 12-Aug-2016 sdo If the return backlog value is "", make it "0". 
# v0.08 12-Aug-2016 sdo If display is "0,0", make it "-", which is easier to see activity. 
# v0.09 12-Aug-2016 sdo Round anything > 100 to units of 100. 
# v0.10 12-Aug-2016 sdo Use a function so that display updates less often. 
################################################################################################### 


################################################################################################### 
# Functions... 

Function fn_count($p1) { 
    $return = [string]$p1 
    if ($return -eq "") {Return "0"} 
    if (fn_is_numeric($return)) { 
    $number = [int]$return 
    switch ($number) { 
    {$_ -ge 100} {$return = $(([math]::Round($_/100)) * 100) ; $return=[string]$return+"+" ; Return $return } 
    {$_ -ge 1} {Return "<100" } 
    {$_ -eq 0} {Return "0" } 
    } 
    } 
    Return $return 
} 

Function fn_display_header { 
    "" 
    "   Disp Cnt AppAxx   AppBxxxWorking AppCxxxx  AppKxx   AppTxxxFiles" 
    "   ===== ===== ======   ============== ========  ======   ============" 
} 

Function fn_is_numeric($p1) { 
    Return ($($p1.Trim()) -Match "^[-+]?([0-9]*\.[0-9]+|[0-9]+\.?)$") 
} 


################################################################################################### 
# Main code... 

$script_spec = $PSCommandPath 
$script_path = [System.IO.Path]::GetDirectoryName(   $script_spec  ) 
$script_name = [System.IO.Path]::GetFileNameWithoutExtension($script_spec  ) 
$script_log = [System.IO.Path]::ChangeExtension(   $script_spec, ".log") 

$Host.UI.RawUI.WindowTitle = $script_name 

$line = "" 
$z_count = 0 
$z_lines = 0 


fn_display_header 
fn_display_header | Out-File $script_log -Append 

while ($true) { 
    $prev = $line 

    $z_count++ 
    if (($z_count % 100) -eq 0) {$prev = ""} 


################################################################################################### 
# Establish whether DFSR is up/enabled/available, or unknown... 

    $set = $(DFSRDiag backlog /rgname:AppAxx   /rfname:AppAxx   /sendingmember:ZZZPAAACSFT001 /receivingmember:ZZZPAAACSFT002) 
    if ($LastExitCode -eq 0) {$AppAxx_Diag = "ok"} else {$AppAxx_Diag = "UNKNOWN"} 

    $set = $(DFSRDiag backlog /rgname:AppBxxxWorking /rfname:AppBxxxWorking /sendingmember:ZZZPAAACSFT001 /receivingmember:ZZZPAAACSFT002) 
    if ($LastExitCode -eq 0) {$AppBxxxWorking_Diag = "ok"} else {$AppBxxxWorking_Diag = "UNKNOWN"} 

    $set = $(DFSRDiag backlog /rgname:AppCxxxx  /rfname:AppCxxxx  /sendingmember:ZZZPAAACSFT001 /receivingmember:ZZZPAAACSFT002) 
    if ($LastExitCode -eq 0) {$AppCxxxx_Diag = "ok"} else {$AppCxxxx_Diag = "UNKNOWN"} 

    $set = $(DFSRDiag backlog /rgname:AppKxx   /rfname:AppKxx   /sendingmember:ZZZPAAACSFT001 /receivingmember:ZZZPAAACSFT002) 
    if ($LastExitCode -eq 0) {$AppKxx_Diag = "ok"} else {$AppKxx_Diag = "UNKNOWN"} 

    $set = $(DFSRDiag backlog /rgname:AppTxxxFiles /rfname:AppTxxxFiles /sendingmember:ZZZPAAACSTA003 /receivingmember:ZZZPAAACSTA004) 
    if ($LastExitCode -eq 0) {$AppTxxxFiles_Diag = "ok"} else {$AppTxxxFiles_Diag = "UNKNOWN"} 


################################################################################################### 
# Get DFSR back-log counts, from both sides... or report the "unknown" from the diagnostics... 

    if ($AppAxx_Diag -eq "ok") { 
    $verbose = $($set = $(Get-DFSRBackLog -Verbose -SourceComputerName ZZZPAAACSFT001 -DestinationComputerName ZZZPAAACSFT002 -GroupName AppAxx -ErrorAction SilentlyContinue | Select FullPathname)) 4>&1 
    if ($?) {$count = [string]$set.Count} else {$count = "ERROR"} 
    if ($count -ne "ERROR") { 
     if ($count -ne "100") { $count = fn_count($count) } else { 
     $verbose = [string]$verbose ; $count = $verbose.Split(" ")[-1] ; $count = fn_count($count) } } 
    $AppAxx_Display = $count 

    $verbose = $($set = $(Get-DFSRBackLog -Verbose -SourceComputerName ZZZPAAACSFT002 -DestinationComputerName ZZZPAAACSFT001 -GroupName AppAxx -ErrorAction SilentlyContinue | Select FullPathname)) 4>&1 
    if ($?) {$count = [string]$set.Count} else {$count = "ERROR"} 
    if ($count -ne "ERROR") { 
     if ($count -ne "100") { $count = fn_count($count) } else { 
     $verbose = [string]$verbose ; $count = $verbose.Split(" ")[-1] ; $count = fn_count($count) } } 
    $AppAxx_Display = $AppAxx_Display + "," + $count 
    } else { 
    $AppAxx_Display = $AppAxx_Diag 
    } 


    if ($AppBxxxWorking_Diag -eq "ok") { 
    $verbose = $($set = $(Get-DFSRBackLog -Verbose -SourceComputerName ZZZPAAACSFT001 -DestinationComputerName ZZZPAAACSFT002 -GroupName AppBxxxWorking -ErrorAction SilentlyContinue | Select FullPathname)) 4>&1 
    if ($?) {$count = [string]$set.Count} else {$count = "ERROR"} 
    if ($count -ne "ERROR") { 
     if ($count -ne "100") { $count = fn_count($count) } else { 
     $verbose = [string]$verbose ; $count = $verbose.Split(" ")[-1] ; $count = fn_count($count) } } 
    $AppBxxxWorking_Display = $count 

    $verbose = $($set = $(Get-DFSRBackLog -Verbose -SourceComputerName ZZZPAAACSFT002 -DestinationComputerName ZZZPAAACSFT001 -GroupName AppBxxxWorking -ErrorAction SilentlyContinue | Select FullPathname)) 4>&1 
    if ($?) {$count = [string]$set.Count} else {$count = "ERROR"} 
    if ($count -ne "ERROR") { 
     if ($count -ne "100") { $count = fn_count($count) } else { 
     $verbose = [string]$verbose ; $count = $verbose.Split(" ")[-1] ; $count = fn_count($count) } } 
    $AppBxxxWorking_Display = $AppBxxxWorking_Display + "," + $count 
    } else { 
    $AppBxxxWorking_Display = $AppBxxxWorking_Diag 
    } 


    if ($AppCxxxx_Diag -eq "ok") { 
    $verbose = $($set = $(Get-DFSRBackLog -Verbose -SourceComputerName ZZZPAAACSFT001 -DestinationComputerName ZZZPAAACSFT002 -GroupName AppCxxxx -ErrorAction SilentlyContinue | Select FullPathname)) 4>&1 
    if ($?) {$count = [string]$set.Count} else {$count = "ERROR"} 
    if ($count -ne "ERROR") { 
     if ($count -ne "100") { $count = fn_count($count) } else { 
     $verbose = [string]$verbose ; $count = $verbose.Split(" ")[-1] ; $count = fn_count($count) } } 
    $AppCxxxx_Display = $count 

    $verbose = $($set = $(Get-DFSRBackLog -Verbose -SourceComputerName ZZZPAAACSFT002 -DestinationComputerName ZZZPAAACSFT001 -GroupName AppCxxxx -ErrorAction SilentlyContinue | Select FullPathname)) 4>&1 
    if ($?) {$count = [string]$set.Count} else {$count = "ERROR"} 
    if ($count -ne "ERROR") { 
     if ($count -ne "100") { $count = fn_count($count) } else { 
     $verbose = [string]$verbose ; $count = $verbose.Split(" ")[-1] ; $count = fn_count($count) } } 
    $AppCxxxx_Display = $AppCxxxx_Display + "," + $count 
    } else { 
    $AppCxxxx_Display = $AppCxxxx_Diag 
    } 


    if ($AppKxx_Diag -eq "ok") { 
    $verbose = $($set = $(Get-DFSRBackLog -Verbose -SourceComputerName ZZZPAAACSFT001 -DestinationComputerName ZZZPAAACSFT002 -GroupName AppKxx -ErrorAction SilentlyContinue | Select FullPathname)) 4>&1 
    if ($?) {$count = [string]$set.Count} else {$count = "ERROR"} 
    if ($count -ne "ERROR") { 
     if ($count -ne "100") { $count = fn_count($count) } else { 
     $verbose = [string]$verbose ; $count = $verbose.Split(" ")[-1] ; $count = fn_count($count) } } 
    $AppKxx_Display = $count 

    $verbose = $($set = $(Get-DFSRBackLog -Verbose -SourceComputerName ZZZPAAACSFT002 -DestinationComputerName ZZZPAAACSFT001 -GroupName AppKxx -ErrorAction SilentlyContinue | Select FullPathname)) 4>&1 
    if ($?) {$count = [string]$set.Count} else {$count = "ERROR"} 
    if ($count -ne "ERROR") { 
     if ($count -ne "100") { $count = fn_count($count) } else { 
     $verbose = [string]$verbose ; $count = $verbose.Split(" ")[-1] ; $count = fn_count($count) } } 
    $AppKxx_Display = $AppKxx_Display + "," + $count 
    } else { 
    $AppKxx_Display = $AppKxx_Diag 
    } 


    if ($AppTxxxFiles_Diag -eq "ok") { 
    $verbose = $($set = $(Get-DFSRBackLog -Verbose -SourceComputerName ZZZPAAACSTA003 -DestinationComputerName ZZZPAAACSTA004 -GroupName AppTxxxFiles -ErrorAction SilentlyContinue | Select FullPathname)) 4>&1 
    if ($?) {$count = [string]$set.Count} else {$count = "ERROR"} 
    if ($count -ne "ERROR") { 
     if ($count -ne "100") { $count = fn_count($count) } else { 
     $verbose = [string]$verbose ; $count = $verbose.Split(" ")[-1] ; $count = fn_count($count) } } 
    $AppTxxxFiles_Display = $count 

    $verbose = $($set = $(Get-DFSRBackLog -Verbose -SourceComputerName ZZZPAAACSTA004 -DestinationComputerName ZZZPAAACSTA003 -GroupName AppTxxxFiles -ErrorAction SilentlyContinue | Select FullPathname)) 4>&1 
    if ($?) {$count = [string]$set.Count} else {$count = "ERROR"} 
    if ($count -ne "ERROR") { 
     if ($count -ne "100") { $count = fn_count($count) } else { 
     $verbose = [string]$verbose ; $count = $verbose.Split(" ")[-1] ; $count = fn_count($count) } } 
    $AppTxxxFiles_Display = $AppTxxxFiles_Display + "," + $count 
    } else { 
    $AppTxxxFiles_Display = $AppTxxxFiles_Diag 
    } 


################################################################################################### 
# Build the table for display... 

    if ($AppAxx_Display   -eq "0,0") {$AppAxx_Display   = "-"} 
    if ($AppBxxxWorking_Display -eq "0,0") {$AppBxxxWorking_Display = "-"} 
    if ($AppCxxxx_Display  -eq "0,0") {$AppCxxxx_Display  = "-"} 
    if ($AppKxx_Display   -eq "0,0") {$AppKxx_Display   = "-"} 
    if ($AppTxxxFiles_Display -eq "0,0") {$AppTxxxFiles_Display = "-"} 

    $table = @() 
    $table = New-Object PSObject -Property @{ 
    AppAxx   = $AppAxx_Display 
    AppBxxxWorking = $AppBxxxWorking_Display 
    AppCxxxx  = $AppCxxxx_Display 
    AppKxx   = $AppKxx_Display 
    AppTxxxFiles = $AppTxxxFiles_Display 
    } 

    $line = ($table | Format-Table -HideTableHeaders ` 
    @{ expression = { $_.AppAxx   } ; width = 15 } ` 
    ,@{ expression = { $_.AppBxxxWorking } ; width = 15 } ` 
    ,@{ expression = { $_.AppCxxxx  } ; width = 15 } ` 
    ,@{ expression = { $_.AppKxx   } ; width = 15 } ` 
    ,@{ expression = { $_.AppTxxxFiles } ; width = 15 } ` 
    | Out-String).Trim() 


    if ($line -ne $prev) { 
    $z_lines++ 

    if (($z_lines % 10) -eq 0) { 
     fn_display_header 
     fn_display_header | Out-File $script_log -Append 
    } 

    $display = $(Get-Date -Format T) + " " + $("{0:F0}" -f $z_lines).PadLeft(5) + " " + $("{0:F0}" -f $z_count).PadLeft(5) + " " + $line 

    $display 
    $display | Out-File $script_log -Append 
    } 

    Start-Sleep -Seconds 10 
} 

exit 
+0

腳本的功能是...對於五個不同的DFSR羣集對和五個不同的DFSR命名空間,請檢查DFSR(即複製)是否已啓用/開啓/已啓動/已配置,如果是,則查詢兩端DFSR關係並顯示積壓計數器。我已經使用了一個函數來減少顯示/報告/列表/屏幕/顯示更新率,即我對積壓計數的微小變化不感興趣,但我確實想跟蹤基本活動和大量積壓事件,即檢測DFSR實際上在工作。 HTH。 – sdo

+0

如果你想展示你寫的代碼,請使用博客或論壇。如果您必須將其發佈在SO上:如果您發佈有關您的腳本解決的問題的問題,然後將您的腳本(包括一些解釋)作爲自己的答案發布,但只是將代碼發佈爲(非 - )問題顯然是題外話題。 –

+0

好 - 理解 - 不會再發生。 – sdo

回答

0

這不是一個真正的「具體問題」,所以我要表現得好像這是CodeReview.StackExchange代替。

  • $count = fn_count($count) - 功能在PowerShell中調用不使用() - 除非你想傳遞數組作爲參數 - 他們只是像的cmdlet。 $count = fn_count $count
  • fn_is_numeric($p1) - 使用[int]::TryParse()或轉換爲[int]並捕獲錯誤。
  • fn_count($p1) - 有問題的Visual Basic樣式冗餘命名樣式,舊樣式參數聲明,無用參數名稱
  • fn_count - 什麼。該函數在11行中包含字詞return 14次。它會轉換爲字符串,你也可以在通話時進行操作,並且它可以完成將最後兩位數字壓縮到00或者說< 100或0的所有內容。而且它有錯誤 - 將170放入並且它會顯示200 +。
  • [System.IO.Path]::GetDirectoryName()和朋友 - >複製粘貼代碼[IO.FileInfo].Directory
  • 羣衆 - > 5號線的設置和一個ForEach
  • 使用Hashtable中的重複數據,而不是許多單獨命名變量
  • $("{0:F0}" -f $z_lines).PadLeft(5)出現的要做"$z_lines".PadLeft(5)
  • DFSRDiag調用您有10行復制粘貼和5個自定義變量名稱只是爲了將文本'OK'或'UNKNOWN'帶到腳本的下一部分。與下面的複製麪食混合在一起。他們已經很臃腫,但可以因爲清理...

此:

if ($?) {$count = [string]$set.Count} else {$count = "ERROR"} 
if ($count -ne "ERROR") { 
    if ($count -ne "100") { $count = fn_count($count) } else { 
    $verbose = [string]$verbose ; $count = $verbose.Split(" ")[-1] ; $count = fn_count($count) } } 
$AppAxx_Display = $count 

這樣做的「我的複製粘貼代碼需要很長的感覺,我要去通過REALLYCRAMMINGEVERYTHINGUPUNREADABLY縮短它; fn_count已經將$ p1轉換爲字符串,所以在這裏跳過這樣做,fn_count直接傳遞非數字字符串,因此您可以跳過檢查它是否爲「ERROR」並僅通過它提供它。 (它怎麼可能不是已經?),忘記很多嵌套難以閱讀的嵌套if/else,忘記存儲$計數實際上 - 將一個或其他東西放入fn_count並一次性將結果分配給AppAxx。

  • 的$表/格式表shennanigans:IDK的,但與5號線的設置代碼的foreach循環我認爲它可以大都被壓扁。

  • $Verbose = $($set = $(Get-DFSRBackLog ... | Select FullPathname)) 4>&1未註釋伏都教。我甚至不想去碰它。

不管怎樣,我放棄fn_is_numeric,設置五個服務器及其複製件的Hashtable和轉向的兩大塊複製粘貼到一個循環,掏出雙GET-DFSRBacklog呼叫到一個函數並將fn_count合併到其中,將"0,0"測試移至源代碼中,通過重新使用前面設置的相同哈希表修改$table的內容,刪除了一堆(),奇怪的數字格式,雙線可能是一個,並且通常敲100多行關閉它,並修復了170-> 200+錯誤:

################################################################################################### 
# File: DFSR-check-3-show-backlog.ps1 
# Desc: Simple monitor of DFSR backlog. 
# 
# Vers Date  Who Description 
# ---- ----  --- ----------- 
# v0.01 11-Aug-2016 sdo Initial draft, based on commands from BM. 
# v0.02 12-Aug-2016 sdo Use "Out-String" to trim the blank lines of the table. 
# v0.03 12-Aug-2016 sdo Extract count from verbose output if "100" items are returned. 
# v0.04 12-Aug-2016 sdo Write to a log file. 
# v0.05 12-Aug-2016 sdo Only display when different or every 100 entries. 
# v0.06 12-Aug-2016 sdo Same layout and counter as other two scripts. 
# v0.07 12-Aug-2016 sdo If the return backlog value is "", make it "0". 
# v0.08 12-Aug-2016 sdo If display is "0,0", make it "-", which is easier to see activity. 
# v0.09 12-Aug-2016 sdo Round anything > 100 to units of 100. 
# v0.10 12-Aug-2016 sdo Use a function so that display updates less often. 
################################################################################################### 


# Functions... ################################################################################### 


Function Test-DFSRAtoB { 
    Param($ComputerA, $ComputerB, $GroupName) 

    # Get the backlog count, either directly or from the verbose log 
    $verbose = $($set = $(Get-DFSRBackLog -Verbose -SourceComputerName $ComputerA -DestinationComputerName $ComputerB -GroupName $GroupName -ErrorAction SilentlyContinue | Select FullPathname)) 4>&1 
    if (!$?) { return "ERROR" } 
    $Count = if ("100" -ne $set.Count) { $set.Count } else { "$verbose".Split(" ")[-1] } 

    # Round the backlog count to (0, <100, nn00+), or return it unchanged if that fails 
    try { 

     if  (100 -le $Count) { $Count -replace '..$', '00+' } 
     elseif ( 1 -le $Count) { "<100" } 
     elseif ( 0 -eq $Count) { "0" } 

    } catch { $Count } 

} 

Function Show-Header { 
    "" 
    "   Disp Cnt AppAxx   AppBxxxWorking AppCxxxx  AppKxx   AppTxxxFiles" 
    "   ===== ===== ======   ============== ========  ======   ============" 
} 


################################################################################################### 
# Main code... 

$ScriptFileName = [System.IO.FileInfo]$PSCommandPath 
$ScriptLog = Join-Path $ScriptFileName.Directory "$($ScriptFileName.BaseName).log" 

$Host.UI.RawUI.WindowTitle = $ScriptFileName.Name 

$line = "" 
$z_count = 0 
$z_lines = 0 


Show-Header 
Show-Header| Out-File $ScriptLog -Append 

$Replications = @(
    @{Name='AppAxx';   Member1='ZZZPAAACSFT001'; Member2='ZZZPAAACSFT002'}, 
    @{Name='AppBxxxWorking'; Member1='ZZZPAAACSFT001'; Member2='ZZZPAAACSFT002'}, 
    @{Name='AppCxxxx';  Member1='ZZZPAAACSFT001'; Member2='ZZZPAAACSFT002'}, 
    @{Name='AppKxx';   Member1='ZZZPAAACSFT001'; Member2='ZZZPAAACSFT002'}, 
    @{Name='AppTxxxFiles'; Member1='ZZZPAAACSTA003'; Member2='ZZZPAAACSTA004'} 
) 

while ($true) { 

################################################################################################### 
# Establish whether DFSR is up/enabled/available, or unknown... 
# Get DFSR back-log counts, from both sides... or report the "unknown" from the diagnostics... 

    $DisplayData = @{} 

    $Replications | ForEach { 

    $set = $(DFSRDiag backlog /rgname:$_.Name /rfname:$_.Name /sendingmember:$_.Member1 /receivingmember:$_.Member2) 
    if ($LastExitCode -eq 0) 
    { 
     $AtoBResult = Test-DFSRAtoB $_.Member1 $_.Member2 $_.Name 
     $BtoAResult = Test-DFSRAtoB $_.Member2 $_.Member1 $_.Name 

     $Display = "$AtoBResult, $BtoAResult" 
     $DisplayData[$_.Name] = if ($Display -eq "0,0") { "-" } else { $Display } 
    } 
    else 
    { 
     $DisplayData[$_.Name] = "UNKNOWN" 
    } 

    } 

################################################################################################### 
# Build the table for display... 

    $prev = if (++$z_count % 100) { $line } else { "" } 

    $line = ([PSCustomObject]$DisplayData | Format-Table -HideTableHeaders ` 
    @{ expression = { $_.AppAxx   } ; width = 15 } ` 
    ,@{ expression = { $_.AppBxxxWorking } ; width = 15 } ` 
    ,@{ expression = { $_.AppCxxxx  } ; width = 15 } ` 
    ,@{ expression = { $_.AppKxx   } ; width = 15 } ` 
    ,@{ expression = { $_.AppTxxxFiles } ; width = 15 } ` 
    | Out-String).Trim() 


    if ($line -ne $prev) { 

    if ((++$z_lines % 10) -eq 0) { 
     Show-Header 
     Show-Header | Out-File $ScriptLog -Append 
    } 

    ($display = "$(Get-Date -Format T) $("$z_lines".PadLeft(5)) $("$z_count".PadLeft(5)) $line") 
    $display | Out-File $ScriptLog -Append 
    } 

    Start-Sleep -Seconds 10 
} 

exit 

我沒有做的是測試它,所以我懷疑它會按原樣工作。不過,希望它在概念上是合理的。

+1

令人驚歎!非常感謝您抽出時間仔細分析我糟糕的代碼。哦,是的,你的結果在概念上是合理的。一個noob的第一個劇本與真正應該如何完成的區別是一個很好的例子。多麼美妙的學習練習。 – sdo

+1

我唯一需要做的事情是:i)在調用DFSRdiag時封裝'$ _。Name'(和其他)實體,即我必須使用'/switch:$($_.Name)',並且ii )對於你來說不是很明顯,有時返回的count是一個空字符串「」,所以我添加了兩個if語句將「」更改爲「0」。除了這兩點,這是一個完美的重寫。我會再說一遍,比你非常清楚的一課。非常感謝。 – sdo

+0

re ii ii)實際上在主函數中,Get-DSFRBackLog的返回結果有時是$ Null,所以測試一下,並將$ Count改爲零。 – sdo