I am looking to add a basic inventory script to our PC’s and write it to SQL.
I can already add to the SQL DB using something like this but don’t know to “fetch” the primary key of the record I just inserted:
$RetiredUpdatedataSource = "SQLSERVER" $RetiredUpdateuser = "sa" $RetiredUpdatepwd = "MYPASSWORD" $RetiredUpdatedatabase = "RetiredPCs" $RetiredUpdateconnectionString = "Server=$RetiredUpdatedataSource;uid=$RetiredUpdateuser; pwd=$RetiredUpdatepwd;Database=$RetiredUpdatedatabase;Integrated Security=False;" $RetiredUpdatequery = "Insert into tblRetiredPCs (ComputerName, Serial, Model, Location, LastLogin, OU, DateRetired) VALUES ('$PCName', '$RetiredSerial', '$RetiredModel', '$RetiredLocation', '$RetiredlastLoggedIn', '$RetiredOU', '$RetiredDate')" $RetiredUpdateconnection = New-Object System.Data.SqlClient.SqlConnection $RetiredUpdateconnection.ConnectionString = $RetiredUpdateconnectionString #$connection.ConnectionString = "Server=$dataSource;Database=$database;Integrated Security=True;" $RetiredUpdateconnection.Open() $RetiredUpdatecommand = $RetiredUpdateconnection.CreateCommand() $RetiredUpdatecommand.CommandText = $RetiredUpdatequery $RetiredUpdateresult = $RetiredUpdatecommand.ExecuteReader() $Global:RetiredUpdatetable = new-object "System.Data.DataTable" $RetiredUpdatetable.Load($RetiredUpdateresult) $RetiredUpdateconnection.Close()
I wanted to write that “key” number to a text file on the PC and maybe even store it in AD with the computer object but don’t know how to retrieve it at the time the record is inserted.
Thanks,
Scott