Powershell to SQlite - Need help with Parameters as a variable #124
-
I'm trying to make a function that will throw PS object to SQLite db Still work in progress (lots of tuning needed) but I'm stuck on 1 point: See below:
The commented line for "Invoke-SqlUpdate" works fine, but the $UpdateParams hashtable results in:
The hashable $UpdateParams looks like this:
Any ideas on how to pass the update params as a variable? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I'm using the SQLite example from the SimplySQL Wiki with Get-Process |
Beta Was this translation helpful? Give feedback.
-
Your example is a little complicated (I suspect in order to handle other scenarios), so I'll reframe to show a simpler version that hopefully will make clear how to leverage the module to do this. (that said v2 will make this easier... and i'm almost done testing the changes). #open an inmemory db connection
Open-SQLiteConnection
#create a table
Invoke-SqlUpdate -Query "CREATE TABLE computer_process (id int, name text, path text)" | Out-Null
#get processes to store
$data = Get-Process | Select-Object -first 15 Id, ProcessName, Path
#store data
$query = "INSERT INTO computer_process (id, name, path) VALUES (@id, @name, @path)"
foreach($d in $data) {
Invoke-SqlUpdate -Query $query -Parameters @{id = $d.id; name = $d.processname; path = $d.path} | Out-Null
}
# count rows
"Number of Rows: "
Invoke-SqlScalar "SELECT COUNT(1) FROM computer_process"
#show data
"Data from computer_process:"
Invoke-SqlQuery "SELECT * FROM computer_process"
#drop connection
Close-SqlConnection Now if you want to make this short and leverage some pipelining, you can do this: #open an inmemory db connection
Open-SQLiteConnection
#create a table
Invoke-SqlUpdate -Query "CREATE TABLE computer_process (id int, name text, path text)" | Out-Null
$query = "INSERT INTO computer_process (id, name, path) VALUES (@id, @name, @path)"
Get-Process |
Select-Object -First 15 |
ForEach-Object {
Invoke-SqlUpdate -Query $query -Parameters @{id = $_.id; name = $_.processname; path = $_.path}
} |
Out-Null
# count rows
"Number of Rows: "
Invoke-SqlScalar "SELECT COUNT(1) FROM computer_process"
#show data
"Data from computer_process:"
Invoke-SqlQuery "SELECT * FROM computer_process"
#drop connection
Close-SqlConnection |
Beta Was this translation helpful? Give feedback.
Your example is a little complicated (I suspect in order to handle other scenarios), so I'll reframe to show a simpler version that hopefully will make clear how to leverage the module to do this. (that said v2 will make this easier... and i'm almost done testing the changes).