In the future I would suggest getting code to run interactively from regular Powershel console before trying to create a SQL Agent job. There are a couple of issues:
I don't know if this formatting of your forum post, but the entire script you posted is on a single line. This will not work as there are considerations for needing line breaks, having certain keywords by themselve or using a semi-colon or continuation character. It's much easier to use line break as follows (this works):
function Out-DataTable
{
param($Properties="*")
Begin
{
$dt = new-object Data.datatable
$First = $true
}
Process
{
$DR = $DT.NewRow()
foreach ($item in $_ | Get-Member -type *Property $Properties ){
$name = $item.Name
if ($first) {
$Col = new-object Data.DataColumn
$Col.ColumnName = $name
$DT.Columns.Add($Col) }
$DR.Item($name) = $_.$name
}
$DT.Rows.Add($DR)
$First = $false
}
End
{
return @(,($dt))
}
}
$dataTable = get-Wmiobject -class Win32_LogicalDisk | Select Name, VolumeName, Size, FreeSpace | Out-DataTable
$connectionString = "Data Source=QCDEVPDS01\MSSQLSERVERDEV;Integrated Security=true;Initial Catalog=SS_DBA_Dashboard;"
$bulkCopy = new-object ("Data.SqlClient.SqlBulkCopy") $connectionString
$bulkCopy.DestinationTableName = "t_OS_Drives"
$bulkCopy.WriteToServer($dataTable)
The second issue I see you are using Format-Table (FT) instead of select-object (select).
The output of format table cannot be piped to your out-datatable function.
Format table returns a differrent type