Author Posts

January 1, 2012 at 12:00 am

by sqlspy at 2012-10-12 04:26:53

Hi,

I'm getting a DBNull error message when trying to load data into a DataTable object. Can anyone suggest a fix? Many Thanks!

$os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $Server

$table = New-Object system.Data.DataTable “$Server”

$col01 = New-Object system.Data.DataColumn BootDevice,([string])
$col02 = New-Object system.Data.DataColumn BuildNumber,([string])
$col03 = New-Object system.Data.DataColumn PAEEnabled,([Boolean])

$table.columns.add($col01)
$table.columns.add($col02)
$table.columns.add($col03)

$row = $table.NewRow();

$row.BootDevice = $os.BootDevice;
$row.BuildNumber = $os.BuildNumber;
$row.PAEEnabled = $os.PAEEnabled;

Exception setting "PAEEnabled": "Cannot set Column 'PAEEnabled' to be null. Please use DBNull instead."
At C:\Users\kdal321\AppData\Local\Temp\2\2134e5f7-6c9c-47d8-804a-b26a0b1b929c.ps1:19 char:6
+ $row. < <<< PAEEnabled = $os.PAEEnabled;
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException

by JeffH at 2012-10-12 04:49:37

The problem is most likely that the PAEEnabled property is empty in WMI. You will need to test it first.


if ($os.PAEEnabled) {
$row.PAEEnabled = $os.PAEEnabled
}
else {
$row.PAEEnabled = $False
}

by sqlspy at 2012-10-12 05:40:57

Perfect, thanks Jeff.