不太了解Powershell,而且确实是sql我花了整整一周的时间来拼凑一个有效的脚本,但我需要帮助扩展它。它适用于从同一个类中检索多个值并插入值。我需要做的是能够从其他类中检索值并将结果插入到适当的列中。任何帮助/建议都会非常感激。
$servernames = Get-WmiObject -computername Anycompname -class win32_ComputerSystem | Select Name, Manufacturer
# Open Connection
$conn = New-Object System.Data.SqlClient.SqlConnection
$connectionString = "Server=;Database=;user=;pwd=;"
$conn.ConnectionString = $connectionString
$conn.Open()
# Create the Command object to execute the queries
$cmd = New-Object System.Data.SqlClient.SqlCommand
$cmd.Connection = $conn
$cmd.CommandType = [System.Data.CommandType]::Text
# Write Data
foreach ($servername in $servernames)
{
# Compose Query for this $servername - watch the single quotes for string values!!
$query = "INSERT INTO dbo.U_Server (ServerName, OSVersion) VALUES ('" + $servername.Name + "', '" + $servername.Manufacturer + "')"
# Uncomment next line to display query for checking
$query
# Setup Command
$cmd.CommandText = $query
# Execute Command to insert data for this $drive
$result = $cmd.ExecuteNonQuery()
}
# Tidy Up
$conn.Close()
答案 0 :(得分:0)
只需获取foreach循环中的其他WMI对象,并根据需要继续添加到插入字符串中。
foreach ($servername in $servernames)
{
## Get other WMI info as needed here.
$os = Get-WmiObject -class win32_operatingsystem –computername $servername.Name
$processors = Get-WmiObject Win32_Processor -computername $servername.Name
# Compose Query for this $servername
$query = "Insert Into dbo.U_Server (ServerName, OSVersion, OSName, OSServicePack) Values
('$($servername.Name)', '$($servername.Manufacurer)', ‘$($os.Caption)’, ‘$($os.ServicePackMajorVersion)’ )"
## The rest of your foreach loop follows
} ## End of foreach