我正在尝试使用Powershell从MS Access数据库中提取PC名称列表,提取其驱动器大小统计信息(WMI对象),然后将其保存回Access。我不断收到以下错误:
Exception calling "ExecuteNonQuery" with "0" argument(s): "No value given
for one or more required parameters."
At C:\Users\*******\Desktop\Scripts\AccessUpdate\HDDUpdate.ps1:75 char:5
+ $WriteFreeSpaceCmd.ExecuteNonQuery()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : OleDbException
它在Windows 10,Access 2016 x64,使用Visual Studio Code,PowerShell 5.1,NET Framework 4.8,ADE 2016 x64进行调试的系统上运行。
我已经检查了所有字段名称,但是不知道要检查什么。我的Google-fu也无效。
$connstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\jharold\Desktop\Scripts\AccessUpdate\WinHDDTest.accdb;Persist Security Info=False;" #Change Source to DB location
[reflection.assembly]::LoadWithPartialName("System.Data") | Out-Null
$Conn = New-Object System.Data.OleDb.OleDbConnection
$Conn.ConnectionString = $connstr
$Conn.Open() | Out-Null
#
# Pull PC names from db and gather HDD info code works fine
#
foreach ($PC in $HDDInfo) {
$WriteFreeSpaceCmd = New-Object System.Data.OleDb.OleDbCommand
$WriteFreeSpaceCmd.Connection = $Conn
$WriteTotalSpaceCmd = New-Object System.Data.OleDb.OleDbCommand
$WriteTotalSpaceCmd.Connection = $Conn
$temp1 = "UPDATE Status SET ImportFreeSpace = " + $PC.FreeSpace + " WHERE NetBios_Name = " + $PC.NetBiosInfo
$WriteFreeSpaceCmd.CommandText = $temp1
$temp2 = "UPDATE Status SET ImportTotalSpace = " + $PC.TotalSpace + " WHERE NetBios_Name = " + $PC.NetBiosInfo
$WriteTotalSpaceCmd.CommandText = $temp2
$WriteFreeSpaceCmd.ExecuteNonQuery()
$WriteTotalSpaceCmd.ExecuteNonQuery()
}
我希望$ PC.FreeSpace和$ PC.TotalSpace作为文本保存到相应PC的ImportFreeSpace和ImportTotalSpace。相反,它在运行ExecuteNonQuery()时会出错。
答案 0 :(得分:0)
您UPDATE命令中的值应在其两边加上引号。喜欢:
$temp1 = "UPDATE Status SET ImportFreeSpace = '{0}' WHERE NetBios_Name = '{1}' " -f $PC.FreeSpace, $PC.NetBiosInfo