Is it possible to trace where output (to the console) is coming from in a Powershell script? I've got a script which is outputting information to me but I'm not sure which line is making the output. Is it possible to, for example, use Set-PSBreakpoint and tell it to break when info is returned to the console?
Cheers
I'm getting hundreds of "False"s returning on their own lines. Here's the part of the code where the output is coming from:
$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)'"
}
}
Also, heres 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}
}