用于监控 DFSR 积压的 Powershell 脚本
Powershell script to monitor DFSR backlog
不是问题。只是分享。如果这是错误的 and/or 方法,我深表歉意,但我从这些论坛中学到了很多东西,只是想回馈一点,以防其他人想学习 PS 甚至做点什么在 PS 中基本有用。抱歉,但我不得不 运行 通过编辑器编写脚本。不管怎样,它是:
###################################################################################################
# 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
那不是真正的 "specific question",所以我将把它当作 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 行中出现了 14 次 return
这个词。它转换为字符串,你在调用时也会这样做,它会做一大堆事情来将最后两位数字压缩为 00,或者说 <100 或 0。它有问题 - 输入 170,它会说 200 +.
[System.IO.Path]::GetDirectoryName()
和朋友 -> [IO.FileInfo]
和 .Directory
- 大量复制粘贴代码 -> 5 行设置和一个
ForEach
循环
- 对重复数据使用哈希tables 而不是大量单独命名的变量
$("{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
这有一种 "My copy-pasted code is getting long, I'm going to shorten it by REALLYCRAMMINGEVERYTHINGUPUNREADABLY; fn_count
already casts $p1 to string so skip doing that here, fn_count
passes non-numeric strings straight through so you can skip checking if it's "ERROR" 的感觉,不管它是什么,只要把它填进去就行了。忘记将 verbose 转换为字符串(怎么可能已经不是一个了?),忘记很多嵌套的 if/else
难以阅读对齐,实际上根本忘记存储 $count - 将一个或其他东西放入 fn_count 并将结果一次性分配给 AppAxx。
$table / format-table shennanigans:idk,但是对于 ForEach 循环的 5 行设置代码,我认为它大部分可以被压缩。
这个 $Verbose = $( $set = $( Get-DFSRBackLog ... | Select FullPathname ) ) 4>&1
是未注释的巫术。我什至不想碰它。
无论如何,我删除了 fn_is_numeric
,设置了五个服务器及其复制成员的散列table,并将两大块复制粘贴变成了一个循环,取出了双- Get-DFSRBacklog 调用一个函数并将 fn_count
合并到其中,将 "0,0"
测试移至其源代码,通过重复使用我之前设置的相同散列 table 来修剪 $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
我没有做的是测试它,所以我怀疑它是否会按原样工作。不过,希望它在概念上是合理的。
不是问题。只是分享。如果这是错误的 and/or 方法,我深表歉意,但我从这些论坛中学到了很多东西,只是想回馈一点,以防其他人想学习 PS 甚至做点什么在 PS 中基本有用。抱歉,但我不得不 运行 通过编辑器编写脚本。不管怎样,它是:
###################################################################################################
# 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
那不是真正的 "specific question",所以我将把它当作 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 行中出现了 14 次return
这个词。它转换为字符串,你在调用时也会这样做,它会做一大堆事情来将最后两位数字压缩为 00,或者说 <100 或 0。它有问题 - 输入 170,它会说 200 +.[System.IO.Path]::GetDirectoryName()
和朋友 ->[IO.FileInfo]
和.Directory
- 大量复制粘贴代码 -> 5 行设置和一个
ForEach
循环 - 对重复数据使用哈希tables 而不是大量单独命名的变量
$("{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
这有一种 "My copy-pasted code is getting long, I'm going to shorten it by REALLYCRAMMINGEVERYTHINGUPUNREADABLY; fn_count
already casts $p1 to string so skip doing that here, fn_count
passes non-numeric strings straight through so you can skip checking if it's "ERROR" 的感觉,不管它是什么,只要把它填进去就行了。忘记将 verbose 转换为字符串(怎么可能已经不是一个了?),忘记很多嵌套的 if/else
难以阅读对齐,实际上根本忘记存储 $count - 将一个或其他东西放入 fn_count 并将结果一次性分配给 AppAxx。
$table / format-table shennanigans:idk,但是对于 ForEach 循环的 5 行设置代码,我认为它大部分可以被压缩。
这个
$Verbose = $( $set = $( Get-DFSRBackLog ... | Select FullPathname ) ) 4>&1
是未注释的巫术。我什至不想碰它。
无论如何,我删除了 fn_is_numeric
,设置了五个服务器及其复制成员的散列table,并将两大块复制粘贴变成了一个循环,取出了双- Get-DFSRBacklog 调用一个函数并将 fn_count
合并到其中,将 "0,0"
测试移至其源代码,通过重复使用我之前设置的相同散列 table 来修剪 $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
我没有做的是测试它,所以我怀疑它是否会按原样工作。不过,希望它在概念上是合理的。