2

I currently have the following script:

$SQLServer = "sqldev1" 
$SQLDBName = "SPDEV_Printing" 
$SqlQuery = "select * from PcBeheerPrinter WHERE PRT_name = 'bwpsc006'" 

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection 
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True" 

$SqlCmd = New-Object System.Data.SqlClient.SqlCommand 
$SqlCmd.CommandText = $SqlQuery 
$SqlCmd.Connection = $SqlConnection 

$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter 
$SqlAdapter.SelectCommand = $SqlCmd 

$DataSet = New-Object System.Data.DataSet 
$SqlAdapter.Fill($DataSet) 

$SqlConnection.Close() 

clear 

$DataSet.Tables[0]

which returns some console output like this:

    PRT_PrintServerName     : pcounter
PRT_Name                : bwpsc006
PRT_PortNameFull        : PCOUNT_bwpsc006
PRT_CAL_SerialNumber    :
PRT_ACTIVE              : 1
PRT_CAL_RespondToPing   : 1
PRT_CAL_NamePinged      : bwpsc006
PRT_CAL_FirstSeendate   : 8/02/2017 20:55:13
PRT_CAL_LastSeendate    : 4/12/2017 11:36:19
PRT_CAL_SNMPPossible    : 1
PRT_CAL_Brand           :
PRT_ShareName           : bwpsc006
PRT_Comment             :
PRT_Datatype            : RAW
PRT_DriverName          : Canon iR-ADV C5045/5051 PCL6
PRT_Location            :
PRT_PrintProcessor      :
PRT_Published           : 0
PRT_Shared              : 0
PRT_NumberOfMissedPings : 0
PRT_LastResponsedate    : 4/12/2017 11:36:19
PRT_RenderingMode       : CSR

My question is: How do I transfer some of these values into PS variables? I need the Name, SerialNumber and some others (these will do as example) to eventually transfer them to a SharePoint list. I'm still a beginner and can't figure it out through googling.

Thanks!

2
  • 1
    $Name = $DataSet.Tables[0].PRT_Name; $SerialNo = $DataSet.Tables[0].PRT_CAL_SerialNumber and so on! Commented Apr 4, 2018 at 11:27
  • Thank you! Feel free to reply this as an answer so I can accept it. Commented Apr 4, 2018 at 11:37

2 Answers 2

2

When using the System.Data.SqlClient.SqlCommand class, most of the objects returned will be of type datatable. Since, $DataSet will be of type datatable, you can directly use all the returned properties like this -

$Name = $DataSet.Tables[0].PRT_Name; $SerialNo = $DataSet.Tables[0].PRT_CAL_SerialNumber and so on.

You can then use the variables $Name and $Serialno as input to your SharePoint List.

Sign up to request clarification or add additional context in comments.

Comments

1

If the data type is object, then you can directly use the select-object like:

$DataSet.Tables[0] | Select-object  PRT_Name,PRT_CAL_SerialNumber

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.