473,774 Members | 2,218 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Powershell and Updating Databases

Problem is that I want to use PowerShell and a csv file to update a database
(Access, SQL, whatever). I do this now with VBScript. I have made a simple
example of the ps1 script and text file and it works BUT only inserts one
row. If the sample "simpleTest.ps1 " is run from powershell the extraction is
fine. I have commented out the database stuff which works when a instance of
Access "Adatabase" with a "Table1" with all fields set to datatype "text" has
been created, but only one row gets added. I have also tried this with a
do-while and do-until loops with movenext and eof stuff without success.

The csv file - "simpleList.txt " - contains:

a,1,2
b,3,4
c,5,6
d,7,8
e,8,0

The ps1 file - "simpleTest.ps1 " - contains:

#$adOpenStatic = 3
#$adLockOptimis tic = 3
#$objConnection = New-Object -com "ADODB.Connecti on"
#$objRecordSet = New-Object -com "ADODB.Recordse t"
#$objConnection .Open("Provider =Microsoft.Jet. OLEDB.4.0;Data Source =
c:\Adatabase.md b")
#$objRecordSet. Open("DELETE * FROM Table1", $objConnection, $adOpenStatic,
$adLockOptimist ic)
#$objRecordSet. Open("SELECT * FROM Table1", $objConnection, $adOpenStatic,
$adLockOptimist ic)
$toDay = Get-Date -Format yyMMdd
$fileA = "C:\SimpleList. txt"
#$objRecordSet. AddNew()
ForEach ($a in Get-Content $fileA)
{
$i = $a.split(",")
$toDay + " " + $i[0] + " " + $i[1] + " " + $i[2]
# $objRecordSet.F ields.Item("dat e").value = $toDay
# $objRecordSet.F ields.Item("fie ld01").value = $i[0]
# $objRecordSet.F ields.Item("fie ld02").value = $i[1]
# $objRecordSet.F ields.Item("fie ld03").value = $i[2]
# $objRecordSet.U pdate()
}
#$objRecordSet. Close()
#$objConnection .Close()

What am I missing?
Nov 7 '08 #1
1 3082
"leroycarl" <le*******@disc ussions.microso ft.comwrote in message
news:99******** *************** ***********@mic rosoft.com...
Problem is that I want to use PowerShell and a csv file to update a
database
(Access, SQL, whatever). I do this now with VBScript. I have made a
simple
example of the ps1 script and text file and it works BUT only inserts one
row. If the sample "simpleTest.ps1 " is run from powershell the extraction
is
fine. I have commented out the database stuff which works when a instance
of
Access "Adatabase" with a "Table1" with all fields set to datatype "text"
has
been created, but only one row gets added. I have also tried this with a
do-while and do-until loops with movenext and eof stuff without success.

The csv file - "simpleList.txt " - contains:

a,1,2
b,3,4
c,5,6
d,7,8
e,8,0

The ps1 file - "simpleTest.ps1 " - contains:

#$adOpenStatic = 3
#$adLockOptimis tic = 3
#$objConnection = New-Object -com "ADODB.Connecti on"
#$objRecordSet = New-Object -com "ADODB.Recordse t"
#$objConnection .Open("Provider =Microsoft.Jet. OLEDB.4.0;Data Source =
c:\Adatabase.md b")
#$objRecordSet. Open("DELETE * FROM Table1", $objConnection, $adOpenStatic,
$adLockOptimist ic)
#$objRecordSet. Open("SELECT * FROM Table1", $objConnection, $adOpenStatic,
$adLockOptimist ic)
$toDay = Get-Date -Format yyMMdd
$fileA = "C:\SimpleList. txt"
#$objRecordSet. AddNew()
ForEach ($a in Get-Content $fileA)
{
$i = $a.split(",")
$toDay + " " + $i[0] + " " + $i[1] + " " + $i[2]
# $objRecordSet.F ields.Item("dat e").value = $toDay
# $objRecordSet.F ields.Item("fie ld01").value = $i[0]
# $objRecordSet.F ields.Item("fie ld02").value = $i[1]
# $objRecordSet.F ields.Item("fie ld03").value = $i[2]
# $objRecordSet.U pdate()
}
#$objRecordSet. Close()
#$objConnection .Close()

What am I missing?
I think you need to put $objRecordSet.A ddNew() inside the ForEach loop.

Nov 7 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2611
by: Neil Chambers | last post by:
Hi All, I'm looking to see if it's feasible to use the SortObjectCommand included in the Microsoft.Powershell.Commands assembly. I have a Dictionary Dictionary<string, intd = new Dictionary<string, int>(); d = new int;
3
2679
by: =?Utf-8?B?VGhvbWFzIFcuIEJyb3du?= | last post by:
1. Is there a dedicated discussion group for Windows Powershell? I couldn't find one, but that doesn't necessarily mean there isn't :-) 2. I'm just starting in on learning to use Powershell and doing so by slowly (very slowly) attempting to port some existing CMD and Perl scripts. I'm blocked right now on something that should be very simple (I would think)... I want to use SQLCMD to issue a query and parse/format the return string....
6
16576
by: Mesan | last post by:
Does anyone have an idea what it takes to administer DB2 through PowerShell? I want to write some scripts to automate my common DB2 management tasks and I'd really like to be able to use PowerShell for that scripting, but I can't find a way to run DB2 comands. I've tried db2cmd /w db2cmd /w /i db2cmd /i (this one got me the closest) db2clpex (looked promising but not so helpfu)
1
2575
by: Alexander Vasilevsky | last post by:
Hi! How to debug Powershell commandlets? -- http://www.alvas.net - Audio tools for C# and VB.Net developers
5
6784
by: josepharmbruster | last post by:
I am all about using the right tool for the right purposes, which is why I have started reading the GettingStarted guide to PowerShell. I am curious if any other pythoneers have ventured into the world of PowerShell. Mostly, I am interested in grabbing perspectives on the differences noticed from those that have working experience with using both. I dug up one article from Google that talked about comparison but that was about it.
3
1488
by: sridhard2406 | last post by:
Hi I didn't find a forum for powershell so i'm my query here itself. I'm runnung a powershell script and i want to redirect the output to a xml after redirecting the output i need to search a pattern in the outfile and print on the console. is the below is correct filename.ps1 |out-file abc.xml | search-string -pattern "Status"
0
2406
by: =?Utf-8?B?SmVycnk=?= | last post by:
Hi, I just don't get it :( I'm using VB.NET code, within this code I want to call some Powershell code. (To create a user mailbox in Exchange 2007). How can I run Powershell code in vb.net? -- Best regards,
1
3142
by: =?Utf-8?B?YXVsZGg=?= | last post by:
i read the MS kb: Using Exchange Management Shell Commands With Managed Code http://msdn.microsoft.com/en-us/library/bb332449.aspx how ever i'm getting "{"No Windows PowerShell Snap-ins are available for version 1."}" exception when i try to call PSSnapInInfo info = rc.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out snapEx);
1
3542
by: SCRIPT KEEPER | last post by:
Hello, I am a complete noob and just starting off with csharp so I apologize for my basic question. I am wanting to start powershell from inside a batch script and then to pass the powershell args from the batch, so from the batch I can use the name of the compiled csharp app without having to include the "call" command. Something like "Compiled Csharp app.exe" "PowerShell Args" etc..... Thank You for any help.
0
10267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9915
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7463
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6717
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5358
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4014
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3611
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.