SQL Server 2016 Expressでテストデータベースを作成しました。テーブルにはdrivers
と表示されています。powershellからSQL Serverテーブルを更新できません
私はPowerShellを使用して、インストールされているドライバの機密クエリを実行し、それらの値をテストデータベースのドライバテーブルに挿入します。 (挿入が正常に動作します) ドライバテーブルを更新しようとすると、最後のオブジェクトのみがデータベースに40回挿入されます(つまり、ciminstanceクエリから返されるドライバの数です)。私は2つのPowerShellスクリプトに
- 挿入が
- Updateは困惑
値の値を作成しました!
$database = 'test'
$server = 'groga\sqlExpress'
$table = 'dbo.Driver'
$SQLServer = "groga\sqlExpress"
$SQLDBName = "test"
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database =
$SQLDBName; Integrated Security = True"
$SqlConnection.Open()
$today = Get-Date
$drivers = gcim win32_pnpsigneddriver -Property *
$model = gcim win32_computersystem -Property *
foreach($driver in $drivers)
{
if(!($driver.Description -match "Generic") -and $driver.Manufacturer -
notmatch 'Microsoft|Standard|Generic' -and $driver.DriverDate -ne $null)
{
$count = New-Object psobject -Property @{
'Date' = $driver.DriverDate
'Manufacturer' = $driver.Manufacturer
'Version' = $driver.DriverVersion
'PackageID' = "0"
'SKU' = $model.SystemSKUNumber
'Model' = $model.Model
'Today' = $today}
$col1 = $count.Date
$col2 = $count.Manufacturer
$col3 = $count.Version
$col4 = $count.PackageID
$col5 = $count.SKU
$col6 = $count.Model
$col7 = $count.Today
$update = @"
UPDATE $table
SET [Date]='$col1',
[Manufacturer]='$col2',
[Version]='$col3',
[PackageID]='$col4',
[SKU]='$col5',
[Model]='$col6',
[Today]='$col7'
"@
$dbwrite = $SqlConnection.CreateCommand()
$dbwrite.CommandText = $update
$dbwrite.ExecuteNonQuery()
}
}
$Sqlconnection.Close()
は注意してください。 WHERE句を省略すると、**すべての**レコードが更新されます! – JosefZ
ありがとうJosefZ、私は実際にすべてのレコードを更新しようとしています。 – grunzer