This is one of my functions I have in a module:
Function Get-DatabaseUser
{
[CmdletBinding()]
Param()
Write-Verbose 'Getting database...'
$Database = Get-Database
Write-Debug('Value of $Database: {0}' -f $Database)
if($Database -eq $null)
{
$ErrorRecord = New-ErrorRecord "Database could not be accessed." NullReferenceException 'DatabaseInaccessible' ObjectNotFound
$PSCmdlet.ThrowTerminatingError($ErrorRecord)
}
Write-Verbose 'Refreshing user list...'
$Database.Users.Refresh()
Write-Verbose 'Getting user list...'
$Users = $Database.Users |
Where-Object { $_.LoginType -eq 'WindowsUser' } |
Select-Object -Property Name, Login
Write-Debug("Number of users: {0}" -f $Users.Count)
Write-Verbose 'Writing user list to output...'
Write-Output($Users)
}
Is there any reason to keep the Write-Debug
statements in my production code, or could these statements be replaced with Write-Verbose
? For example, replacing
Write-Debug("Number of users: {0}" -f $Users.Count)
with
Write-Verbose '3 users found.'
I'm not sure about the internals of PowerShell and if Write-Debug generates any significant overhead, but I'm still wondering if this has any place in a script used in production or purely for debug versions of my scripts/modules.