Thanks. I can now read but I am trying to write and I am getting this error.
ERROR: Error formatting a string: Index (zero based) must be greater than or equal to zero and less than the size of the argument list..
PowershellAddDB.ps1 (93, 3): ERROR: At Line: 93 char: 3
ERROR: + $sql = "INSERT INTO table(User_Name,IP_Address,Computer ...
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : InvalidOperation: (INSERT INTO Ins...}','{7}','{8}'):String) [], RuntimeException
ERROR: + FullyQualifiedErrorId : FormatError
ERROR:
ERROR: Exception calling "ExecuteNonQuery" with "0" argument(s): "Command text was not set for the command object."
PowershellAddDB.ps1 (98, 3): ERROR: At Line: 98 char: 3
ERROR: + $writecmd.executenonquery() | out-null
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
ERROR: + FullyQualifiedErrorId : OleDbException
ERROR:
ERROR: Error formatting a string: Index (zero based) must be greater than or equal to zero and less than the size of the argument list..
Code: Select all
$InfoParse | ForEach-Object{
$connstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\DB1.accdb;Persist Security Info=True;"
#open a connection
[reflection.assembly]::LoadWithPartialName("System.Data") | Out-Null
$writeconn = New-Object System.Data.OleDb.OleDbConnection
$writeconn.connectionstring = $connstr
$writeconn.open()
# Create a command
$sql = "INSERT INTO table(User_Name,IP_Address,Computer_Name,City,Operating_System,Software_Name,Software_Version,LastBootTime)
VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}')" -f $_.User_Name, $_.IP_Address. $_.Computer_Name, $_.City, $_.Operating_System, $_.Software_Name, $_.Software_Version, $_.LastBootTime
$writecmd = New-Object system.Data.OleDb.OleDbCommand
$writecmd.connection = $writeconn
$writecmd.commandtext = $sql
$writecmd.executenonquery() | out-null
}
$writeconn.close()
}