header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

We have a new sponsor!  Introducting Pragma Systems.  See the home page for details.

problem implementing powershell script in Job Agent
Last Post 03 Feb 2010 09:39 AM by r2d2. 4 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Not Resolved
r2d2User is Offline
New Member
New Member
Posts:5
Avatar

--
22 Jan 2010 11:30 AM  
Hello everybody
I have tried to create a script for SQL Server2008 executed as job.
JobType: PowerShell

Until this point everything worked fine...
I need to read the OS Disk Statuts... so here the Snippet which works just fine:
get-Wmiobject -class Win32_LogicalDisk | ft Name, VolumeName, Size, FreeSpace

Then I tried to write a script to fill the data in a DataTable which will be automatically bulkinserted into sql.
Here my prob begins...
If somebody has a clue please help :/

Code:
function Out-DataTable {<BR>param($Properties="*") <BR>Begin {<BR>$dt = new-object Data.datatable $First = $true } <BR>Process { $DR = $DT.NewRow() foreach ($item in $_ | Get-Member -type *Property $Properties ) <BR>{ $name = $item.Name if ($first) <BR>{ $Col = new-object Data.DataColumn $Col.ColumnName = $name $DT.Columns.Add($Col) } <BR>$DR.Item($name) = $_.$name } <BR>$DT.Rows.Add($DR) $First = $false } <BR>End <BR>{ return @(,($dt)) } } <BR>$dataTable = get-Wmiobject -class Win32_LogicalDisk | ft Name, VolumeName, Size, FreeSpace | Out-DataTable<BR>$connectionString = „Data Source=QCDEVPDS01\MSSQLSERVERDEV;Integrated Security=true;Initial Catalog=SS_DBA_Dashboard;“ <BR>$bulkCopy = new-object ("Data.SqlClient.SqlBulkCopy") $connectionString <BR>$bulkCopy.DestinationTableName = "t_OS_Drives" <BR>$bulkCopy.WriteToServer($dataTable) <BR><BR>Error Message <BR>Executed as user: QCDEVPDS01\SYSTEM. A job step received an error at line 19 in a PowerShell script. The corresponding line is '$DR.Item($name) = $_.$name '. Correct the script and reschedule the job. The error information returned by PowerShell is: 'Exception setting "Item": "Exception calling "set_Item" with "2" argument(s): "Column 'formatEntryInfo' does not belong to table ."" '. Process Exit Code -1. The step failed. 
cmille19User is Offline
New Member
New Member
Posts:65
Avatar

--
22 Jan 2010 03:37 PM  
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
r2d2User is Offline
New Member
New Member
Posts:5
Avatar

--
27 Jan 2010 03:39 PM  
thanx alot for the hint, but it sill doesnt work...
could it be that powershell 1.0 doesnt support that kind of action?


Error yet:
The corresponding line is '$bulkCopy.WriteToServer($dataTable)'.
Exception calling "WriteToServer" with "1" argument(s): "The given value of type String from the data source cannot be converted to type int of the specified target column."
cmille19User is Offline
New Member
New Member
Posts:65
Avatar

--
27 Jan 2010 04:03 PM  
It looks like you we worked past the issues of converting the output to a datatable, but now the issue is in calling the WriteToServer method. Based on the error message your problem is that the dataTable column order does not match your SQL Server table column order and the wrong column are mapped. There are two ways to fix this:

Explicity map columns using the SqlBulkCopy class OR change your SQL table to match the datatable column order. The latter is easier, to do this:
After this line
$dataTable = get-Wmiobject -class Win32_LogicalDisk | Select Name, VolumeName, Size, FreeSpace | Out-DataTable
Run $dataTable | get-member

Recreate or alter your table, t_OS_Drives to match the order you see in the Powershell console. If you have issues doing this post your t_OS_Drives table SQL script and I'll take a look at it.
r2d2User is Offline
New Member
New Member
Posts:5
Avatar

--
03 Feb 2010 09:39 AM  
Thanx for the hint.
it works now, a bit slow but it does what it should.
many thanx again.

You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 footer
footer