Thanks for all the replies. I got past my original question with the following (can't remember where I found it):
catch [System.Exception] {
Write-Host $_.Exception.Message
}
Once I got that, it helped me solve the other issues I was having and it works now. Here is part of the code as it is now:
foreach ($db in $dbs)
{
$dbname = $db.Name
$fileGroups = $db.FileGroups
ForEach ($fg in $fileGroups)
{
If ($fg)
{
$mdfInfo = $fg.Files | Select Name, FileName, size, UsedSpace
$logInfo = $db.LogFiles | Select Name, FileName, Size, UsedSpace
$date = get-date
$cmd.commandtext = "INSERT INTO dbo.db_sizes (Server, DatabaseName, DataFileName, DataSize, DataUsedSpace, LogName, LogFileName, LogSize, LogUsedSpace, SDate) VALUES('{0}','{1}','{2}','{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}')" -f
$instance, $mdfInfo.Name, $mdfInfo.FileName, ($mdfInfo.size / 1000), ($mdfInfo.UsedSpace / 1000), $logInfo.Name, $logInfo.FileName, ($logInfo.size / 1000), ($logInfo.UsedSpace / 1000), $date
Try {
$cmd.executenonquery()
}
catch [System.Exception] {
Write-Host $_.Exception.Message
}
}
}
}