I am trying to insert Values I have collected from Endpoints into an access database using Powershell. I am not well versed in many SQL Syntax and am currently having difficulty getting the values I've collected inserted into an existing table. Below is my code, in full, where everything is working except the entry into the database. Could anyone please advice on what I am doing wrong and how I might go about getting this working?
#import necessary modules
Import-Module ActiveDirectory
#Get the Hostname of the Compuer
$Hostname = $env:COMPUTERNAME
#Get The Model of The Computer
$SystemModel = (systeminfo | Select-String 'System Model:').ToString().Split(':')[1].Trim()
#Determine The Form Factor of the Computer
If ($SystemModel -like 'Optiplex*'){
$FormFactor = 'Desktop'
}
ElseIf ($SystemModel -like 'Latitude*'){
$FormFactor = 'Laptop'
}
Write-Host $FormFactor
#Get The manufacturer of The Computer
$SystemManufacturer = (Systeminfo |Select-String 'System Manufacturer:').ToString().Split(':')[1].Trim()
#Get Serial number of Computer
$ServiceTag = (wmic bios get serialnumber).Split("\n")[2].Trim()
#Get Total memory Installed on the Machine.
$TotalPhysicalMemory = (systeminfo | Select-String 'Total Physical Memory:').ToString().Split(':')[1].Trim()
#Get The Processor Installed in the Machine.
$ProcessorInstalled = Get-wmiobject win32_processor |Select -expand name
function VerifyMachineAssignment($Hostname){
#STEP 1: Get The User's Given name From Active Directory to Compare Against Computer Description
$RawUserName = (Get-ADUser $env:USERNAME | Select-Object -ExpandProperty name).ToString()
#STEP 2: Clean Up the Username you Found in AD So that The Filter understands it.
$UserDescribed = $RawUserName + '*'
#STEP 3: Search the Users OU for an Object Whose Description Matches the Variable from Step 3
$MachineName = (Get-AdComputer -filter {(Description -Like $UserDescribed)} -properties CN | Select-String 'CN').ToString().Split(',')[0].Trim().Split('=')[1]
#STEP 4: Compare Localhost to AD Machine Name To Determine Proper Assignment
If($MachineName -eq $Hostname){
$Global:MachineVerification = 'yes'
}
ELSE{
$Global:MachineVerification = 'no'
}
}
function WriteToDatabase($Hostname, $FormFactor, $SystemManufacturer,$SystemModel, $ServiceTag, $TotalPhysicalMemory, $ProcessorInstalled, $MachineVerification){
# Open a connection To The Database Containing our Inventory Data
if ($FormFactor -like 'Laptop'){
$query = "Select * from Laptops"
$cursor = 2
$lock = 3
$recordset = New-Object -ComObject ADODB.Recordset
$Ado = New-Object -ComObject ADODB.Connection
$Ado.open("Provider = Microsoft.ACE.OLEDB.12.0;Data Source=Documents\CorpInventory.accdb")
$recordset.open($query,$ado,$cursor,$lock)
$recordset.Addnew()
$recordset.Fields.Item("Hostname") = $Hostname
$recordset.Fields.Item("FormFactor") = $FormFactor
$recordset.Fields.Item("Make") = $SystemManufacturer
$recordset.Fields.Item("Model") = $SystemModel
$recordset.Fields.Item("ServiceTag") = $ServiceTag
$recordset.Fields.Item("MemoryInstalled") = $TotalPhysicalMemory
$recordset.Fields.Item("ProcessorInstalled") = $ProcessorInstalled
$recordset.Fields.Item("AssignmentVerified") = $MachineVerification
$recordset.Update()
$recordset.close()
$ado.close()
echo "Hit the DB Add for Laptops"
}
Elseif ($FormFactor -like 'Laptop'){
$query = "Select * from Desktops"
$cursor = 2
$lock = 3
$recordset = New-Object -ComObject ADODB.Recordset
$Ado = New-Object -ComObject ADODB.Connection
$Ado.open("Provider = Microsoft.ACE.OLEDB.12.0;Data Source=Documents\CorpInventory.accdb")
$recordset.open($query,$ado,$cursor,$lock)
$recordset.Addnew()
$recordset.Fields.Item("Hostname") = $Hostname
$recordset.Fields.Item("FormFactor") = $FormFactor
$recordset.Fields.Item("Make") = $SystemManufacturer
$recordset.Fields.Item("Model") = $SystemModel
$recordset.Fields.Item("ServiceTag") = $ServiceTag
$recordset.Fields.Item("MemoryInstalled") = $TotalPhysicalMemory
$recordset.Fields.Item("ProcessorInstalled") = $ProcessorInstalled
$recordset.Fields.Item("AssignmentVerified") = $MachineVerification
$recordset.Update()
$recordset.close()
$ado.close()
echo "Hit the DB Add for Desktops"
}
}
VerifyMachineAssignment
WriteToDatabase($Hostname, $FormFactor, $SystemManufacturer,$SystemModel, $ServiceTag, $TotalPhysicalMemory, $ProcessorInstalled, $MachineVerification)
Write-Host "The Name of the computer is: $Hostname "
Write-Host "The Type of Computer is: $FormFactor"
Write-Host "The Manufacturer of the computer is $SystemManufacturer"
Write-Host "The System model Is: $SystemModel "
Write-Host "The Service Tag for This Machine Is: $ServiceTag "
Write-Host "The amount of System Memory is: $TotalPhysicalMemory"
Write-Host "The Processor Installed Is: $ProcessorInstalled"
Write-Host "Does The Machine Assignment Match AD? $MachineVerification"