【发布时间】:2019-04-24 17:02:50
【问题描述】:
是否可以在 Powershell 脚本中跟踪输出(到控制台)的来源?我有一个向我输出信息的脚本,但我不确定哪一行正在输出。例如,是否可以使用 Set-PSBreakpoint 并告诉它在信息返回到控制台时中断?
干杯
我收到了数百个“False”在他们自己的线路上返回。 这是输出来自的代码部分:
$ar = Function-which_returns_array_of_objects
$gr = Function-which_returns_array_of_objects
Write-Host "To begin with..."
Write-Host "$($ar.count) assets"
Write-Host "$($gr.count) goods"
foreach($asset in $ar){
if(!(Test-NoNA -string $asset.Serial)){continue}
#See if the particular serial number exists in Goods Received
$found = @()
$gr | Where {$_.SerialNumber -eq $asset.serial} | %{
$found += $_
# and then mark the entry as one that has to be deleted from GR
$_.Delete = "YES"
}
if($found.count -eq 1){
#Serial Number has been found once in GR
#We want to check its PN...
if(Test-NoNA -string $found.PartNumber -and $found.PartNumber -ne $asset.Model){
#add it to the asset if its good and not the same as the model number...
$asset.PartNumber -eq $found.PartNumber
}
}elseif(!$found -or $found.count -eq 0){
#No entries found in GR
#Shouldn't be the case but doesn't actually do any damage as we'd be deleting the GR entry anyway
}elseif($found.count -gt 1){
#More than one match for the SN - probably means a SN like "N/A" has got through the earlier checks
Write-Warning "More than one match for SN: '$($asset.serial)'"
}else{
#Default catcher
Write-Warning "Unknown Error for SN: '$($asset.serial)'"
}
}
还有,这里是 Test-NoNA:
function Test-NoNA($string){
#check that the given string is not blank, N/A, ?, etc. Returns true if string is good
if($string -and $string -ne "" -and $string -ne "N/A" -and $string -ne "NA" -and $string -ne '?' -and $string -isnot [System.DBNull]){return $true}
}
【问题讨论】:
-
请发布输出,以便我们可以大胆猜测您的哪一行仍然保密的代码触发了这些东西。当然,您可以尝试发布一些代码... [grin] 我知道可能无法发布整个脚本 - 但如果可以,请执行所以。也许到 Gist.GitHub?
-
这条线
$asset.PartNumber -eq $found.PartNumber看起来是问题的一部分。您的评论使您似乎打算将-eq设为=。 [咧嘴] -
啊,就是这样!这也解释了脚本的其他一些问题。
-
酷!很高兴知道你把它修好了... [grin]
标签: powershell debugging scripting output powershell-3.0