Powershell piping note property to Write-SqlTableData

Tim Anymouse 41 Reputation points
2021-03-30T11:04:07.713+00:00

Hello,
I'm trying to write some data to an SQL database using Write-SqlTableData.

I've tried the following code
$path = 'D:\';
$pathobject = [PSCustomObject] @{message = "Directories within $path"}
Write-SqlTableData -ServerInstance $server -credential $PSCredential -DatabaseName "log" -SchemaName "dbo" -TableName "log" -Force -InputData $pathobject

I've also tried
$pathobject = [PSCustomObject] @{message = "Directories within $path" } $pathobject| Select-Object -Property message | Write-SqlTableData -ServerInstance $server -credential $PSCredential -DatabaseName "log" -SchemaName "dbo" -TableName "log" -Force

The code seems to work until it is piped to the Write-SqlTableData which doesn't seem to be accepting the note property as input. I initially tried just passing as a string but this didn't work either. No error is given but no data is written.
I looked at the documentation
https://learn.microsoft.com/en-us/powershell/module/sqlserver/write-sqltabledata?view=sqlserver-ps
and this lists System.String[] as an input (bottom of page)

Before I give up and use an alternative command to write to SQL I thought I would ask here. I can find no documentation clearly saying if a note property will be accepted as input.

Kind regards,
Tim

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,432 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,621 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ian Xue 39,286 Reputation points Microsoft Vendor
    2021-03-30T12:59:14.947+00:00

    Hi,

    Try converting $pathobject to an array

    [array]$pathobject = [PSCustomObject] @{message = "Directories within $path" }   
    $pathobject| Select-Object -Property message |   
        Write-SqlTableData -ServerInstance $server -credential $PSCredential -DatabaseName "log" -SchemaName "dbo" -TableName "log" -Force  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.