Ich versuche, Werte, die ich von Endpunkten gesammelt habe, in eine Access-Datenbank mit Powershell einzufügen. Ich bin mit vielen SQL-Syntax nicht gut vertraut und habe derzeit Schwierigkeiten, die Werte, die ich gesammelt habe, in eine vorhandene Tabelle einzufügen. Unten ist mein Code, in dem alles funktioniert außer dem Eintrag in die Datenbank. Könnte jemand bitte einen Ratschlag darüber geben, was ich falsch mache und wie ich daran arbeiten könnte?Einfügen von Werten in Access DB von Powershell
#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"