Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Using powershell to write to an access database from a .csv file

$
0
0

Writing to an access database seems similar in powershell to how I've always did it in vbscript but there are a few minor differences I don't fully understand. 

The script below is how I have been  doing it

Option Explicit
'on error resume next
Dim adoCSVConnection, adoCSVRecordSet, strPathToTextfile
Dim strCSVFile, adoJetConnection, adoJetCommand, strDBPath
Dim objScript, strPathToMDB, strTempDB

Const adCmdText1 = &H0001

' Specify path to CSV file.
strPathToTextFile = "C:\New_Hire_Scripts\NewHireDB"

' Specify CSV file name.
strCSVFile = "Newhiredbimport.csv"

' Specify Access database file.
strDBPath = "C:\New_Hire_Scripts\NewHireDB\All_Users.mdb"

' Open connection to the CSV file.
Set adoCSVConnection = CreateObject("ADODB.Connection")
Set adoCSVRecordSet = CreateObject("ADODB.Recordset")

' Open CSV file with header line.
adoCSVConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & strPathtoTextFile & ";" & _
    "Extended Properties=""text;HDR=YES;FMT=Delimited(,)"""

adoCSVRecordset.Open "SELECT * FROM " & strCSVFile, adoCSVConnection

' Open connection to MS Access database.
Set adoJetConnection = CreateObject("ADODB.Connection")
adoJetConnection.ConnectionString = "DRIVER=Microsoft Access Driver (*.mdb);" _
    & "FIL=MS Access;DriverId=25;DBQ=" & strDBPath & ";"
adoJetConnection.Open

' ADO command object to insert rows into Access database.
'Set adoJetCommand = New ADODB.Command
Set adoJetCommand = CreateObject("ADODB.Command")
Set adoJetCommand.ActiveConnection = adoJetConnection
adoJetCommand.CommandType = adCmdText1

' Read the CSV file.
Do Until adoCSVRecordset.EOF
    ' Insert a row into the Access database.
    adoJetCommand.CommandText = "INSERT INTO Domain_Users" _
        & "([Username], First_Name, Last_Name, Facility, Work_Phone, Position, Display_Name, PIN, Hire_Date, State, Employee_ID, Employment_Status, Termination_Date) " _
        & "VALUES (" _
            & "'" & adoCSVRecordset.Fields("Username").Value & "', " _
            & "'" & adoCSVRecordset.Fields("First_Name").Value & "', " _
            & "'" & adoCSVRecordset.Fields("Last_Name").Value & "', " _
            & "'" & adoCSVRecordset.Fields("Facility").Value & "', " _
            & "'" & adoCSVRecordset.Fields("Work_Phone").Value & "', " _
            & "'" & adoCSVRecordset.Fields("Position").Value & "', " _
            & "'" & adoCSVRecordset.Fields("Display_Name").Value & "', " _
            & "'" & adoCSVRecordset.Fields("PIN").Value & "', " _
            & "'" & adoCSVRecordset.Fields("Hire_Date").Value & "', " _
            & "'" & adoCSVRecordset.Fields("State").Value & "', " _
            & "'" & adoCSVRecordset.Fields("Employee_ID").Value & "', " _
            & "'" & adoCSVRecordset.Fields("Employment_Status").Value & "', " _
            & "'" & adoCSVRecordset.Fields("Termination_Date").Value & "')"
    adoJetCommand.Execute
    On Error Resume Next
    adoCSVRecordset.MoveNext
Loop

adoJetConnection.Close
adoCSVRecordset.Close
adoCSVConnection.Close
Set objScript = Nothing

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

So as shown above vbscript has always worked for me.  That being said I feel I should change to powershell because from my perspective it is a more powerful scripting tool.  So I searched the forums and found a script shown below I have began to modify.  What I want to do is import a text file similar to this one into a database using powershell similar to what I have done in vbscript.

Username,First_Name,Last_Name,Facility,OU,Telephone,Position,DisplayName,EmployeeNUM,HireDate,State,EmployeeID,Employmentstatus,Termination_Date
Test12,Test,User1,TESTFACILITY,8675309,Administrator,Test User1,1111,03 06 2012,NY,2222,NewHire
Test23,Test,User2,TESTFACILITY,8675309,Route Driver,Test User2,2222,03 06 2012,NY,3333,NewHire
Test34,Test,User3,TESTFACILITY,8675309,Billing,Test User3,3333,03 06 2012,NY,4444,NewHire
Test56,Test,User4,TESTFACILITY,8675309,Billing,Test User4,4444,03 06 2012,NY,5555,Termination,1 09 2014
Test78,Test,User5,TESTFACILITY,8675309,Billing,Test User5,5555,03 06 2012,NY,7777,NewHire
Test90,Test,User6,TESTFACILITY,8675309,Sales,Test User6,7777,03 06 2012,NY,1111,NewHire
Test91,Test,User7,TESTFACILITY,8675309,Route Driver,Test User7,7777,03 06 2012,NY,1111,NewHire

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

$filename

="c:\scripts\test.mdb" 


$connString

="Provider=Microsoft.Jet.OLEDB.4.0;" 


$connString

+="Data Source=${filename}" 


$conn

=New-Objectdata.OleDb.OleDbConnection$connString


$conn

.Open();  


$sql

="INSERT INTO Users ( Username, First_Name, Last_Name, Facility, Work_Phone, Position, Display_Name, PIN, Hire_Date, State, Employee_ID, Employment_Status, Termination_Date)" 


$sql

+="SELECT 'JDoe', 'John', 'Doe', 'TestFacility','212-867-5309','admin','John Doe','1923','1-1-1919','NY','4444','NewHire'"


$command

=New-Objectdata.OleDb.OleDbCommand$sql 


$command

.connection=$conn 


$a

=$command.ExecuteNonQuery()

As you can see I'm on track but I want to read the file contents from a .csv file in powershell instead of hard code them.  The new hires and terms we get in a comma delimited text file I auto create or term in AD. The access database merely will hold employee info so if we need to look up an employee id we can for a name change etc...  In the powershell example I named the db test.mdb and created a table named users with the headers specified above. You can guess that our long term plan is upgrading to powershell to gain functionality such as deleting , modifying , etc.. records rather than just add them.  Vbscript is capable of doing those types of things as well but powershell so far has just made everything easier and more reliable.  Slowly but surely I am getting all of my old scripts upgraded so this is not a hurry because for now I have a working solution.  I will continue to work on this though always appreciate advice as a fresh opinion is always helpful.   







Viewing all articles
Browse latest Browse all 21975

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>