Tratar con System.DBNull en PowerShell

Dedique más tiempo a extraer datos SQL en PowerShell. Encontrar problemas con [System.DBNull] :: Value y cómo PowerShell se comporta con esto durante las comparaciones.

Aquí hay un ejemplo del comportamiento que veo, junto con soluciones alternativas

#DBNull values don't evaluate like Null...
    if([System.DBNull]::Value){"I would not expect this to display"}
    # The text displays.
    if([string][System.DBNull]::Value){"This won't display, but is not intuitive"}
    # The text does not display.

#DBNull does not let you use certain comparison operators
    10 -gt [System.DBNull]::Value 
    # Could not compare "10" to "". Error: "Cannot convert value "" to type "System.Int32". Error: "Object cannot be cast from DBNull to other types.""

    [System.DBNull]::Value -gt 10
    # Cannot compare "" because it is not IComparable.

    #No real workaround.  Must use test for null workaround in conjunction to avoid comparison altogether:
    [string][System.DBNull]::Value -and [System.DBNull]::Value -gt 10

#Example scenario with a function that uses Invoke-Sqlcmd2 to pull data
    Get-XXXXServer | Where-Object{$_.VCNumCPUs -gt 8}
    #Error for every line where VCNumCPU has DBNull value

    #workaround
    Get-XXXXServer | Where-Object{[string]$_.VCNumCPUs -and $_.VCNumCPUs -gt 8}

¿Me estoy perdiendo algo o no hay una solución 'simple' para esto que permita a las personas con poca experiencia usar las comparaciones de PowerShell como se esperaba?

Presentéuna sugerencia en Connect y tener un temporalsolución alternativa de Dave Wyatt que convierte datarows en psobjects con dbnulls convertidos en nulos, pero estoagrega un poco de sobrecarga. ¿Parece algo que debería manejarse bajo las cubiertas, dado el comportamiento 'suelto' existente de PowerShell?

¿Algún consejo, o he agotado mis opciones por ahora?

¡Gracias!

Respuestas a la pregunta(6)

Su respuesta a la pregunta