我在SQL Server Management Studio中填充第二个表时遇到问题。
我的第一个表格ServerList
包含以下屏幕截图中的所有信息。然后,第二个表ServerDrives
应包含有关硬盘驱动器规格的所有信息(免费,已使用,字母以及连接到硬盘驱动器的服务器)。
目前,我从ServerList
列ServerName
中的所有值中获取了powershell脚本,并将信息推送到powershell脚本中,然后填充操作系统,内存等。
问题:我不确定如何使用相同的PowerShell脚本使用表ServerID
中的相同主键ServerList
来填充我的ServerDrives
表。因为我希望该表中的每一行都是ServerList
中每个服务器的一行。我目前在脚本中有一种收集硬盘信息的方法,但我不知道如何使用该信息来填充我的空表
目标:要让ServerID(ServerList
中的主键)和ServerDriveID(ServerDrives
中的主键)用于同一服务器(QAGGAPP01,QAGGAPP03,QAGGAPP05和等等)。使用适当的硬盘驱动器字母和规格将变量$disks
推入数据库表ServerDrives
。每台服务器都有1个或2个硬盘。
代码:
Write-Output " `n Start of Hal0 `n";
#Start of Server Connection
$connectionString = "Server=QAUTILITYDB01;Database=Hal0Test;Integrated Security=True;"
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$connection.Open()
$command = $connection.CreateCommand()
$ServerArray = [System.Collections.ArrayList]@()
$query = "SELECT ServerName FROM ServerList"
$command.CommandText = $query
$ServerNames = $command.ExecuteReader()
$table = new-object “System.Data.DataTable”
$table.Load($ServerNames)
$ServerArray = $table | select -Expand ServerName
$ServerArray | ForEach-Object {
#$ServerArray returns each server name
#Operating System
$SystemInfo = Get-WmiObject -Class Win32_OperatingSystem -computername $_
$os = Get-WmiObject -Class Win32_OperatingSystem -Computer $_
#Server's Memory (RAM) Usage Average
$TotalRAM = [math]::round($SystemInfo.TotalVisibleMemorySize/1MB,4)
$FreeRAM = [math]::round($SystemInfo.FreePhysicalMemory/1MB,3)
$UsedRAM = $TotalRAM - $FreeRAM
$RAMPercentFree = [math]::round(($FreeRAM / $TotalRAM) * 100,3)
$RAMPercentUsed = [math]::round(($UsedRAM / $TotalRAM) * 100,3)
#Server's CPU (Proccess) Usage Average
$cpuAVG = Get-WmiObject -computername $_ win32_processor | Measure-Object -property LoadPercentage -Average | Select Average
#Server's Hard Drives (MB) Free/Used
$disks = Get-WmiObject -Class Win32_LogicalDisk -Computer $_ |
Where-Object {$_.DriveType -eq 3} |
ForEach-Object {
'{0} {1:D} MB Free/{2:D} MB Used' -f $_.DeviceID,
[int]($_.FreeSpace/1MB), [int]($_.Size/1MB)
}
#Value Types being pushed to Server
$command.CommandText = "UPDATE ServerList SET FQDN = '$_',
TotalRAM= '$TotalRAM' ,
FreeRAM= '$FreeRAM' ,
UsedRAM= '$UsedRAM' ,
RAMPercentFree = '$RAMPercentFree' ,
RAMPercentUsed= '$RAMPercentUsed' ,
OS = '$($os.Caption)' WHERE ServerName LIKE '$($os.PSComputerName)%';"
$result = $command.ExecuteNonQuery()
}
Write-Output "`n End of Hal0";
<小时/> 的 ServerDrives:
HTML预览,我只在我的powershell代码中只是为了更容易看到什么被推送到服务器而不必每次都重新打开表:
答案 0 :(得分:1)
当您要求提供服务器名称时,您是否只能获取主键?
$query = "SELECT ServerID, ServerName FROM ServerList"
现在您可以使用WHERE ServerID = $CurrentServerID
或其他一些。
您没有运行任何INSERT语句。它是一个SELECT,然后是UPDATE。如果您需要主键,只需向服务器询问即可。
答案 1 :(得分:1)
根据其他评论中的建议获取 ServerID 。您可以按如下方式更改代码:
#Start of Server Connection
$ConnectionString = "<CONNECTION>"
$Connection = New-Object System.Data.SqlClient.SqlConnection
$Connection.ConnectionString = $ConnectionString
$Connection.Open()
$Command = $Connection.CreateCommand()
# Selects server id and name and puts it into the table
$ServerInfoQuery = "SELECT ServerID, ServerName FROM ServerList"
$Command.CommandText = $ServerInfoQuery
$ServerInfoReader = $Command.ExecuteReader()
$ServerInfo = New-Object System.Data.DataTable
$ServerInfo.Load($ServerInfoReader)
$ServerInfo | ForEach-Object {
# Read Name and ID from table
$ServerName = $_.ServerName
$ServerID = $_.ServerID
# Operating System
$SystemInfo = Get-WmiObject -Class Win32_OperatingSystem -computername $ServerName
$os = Get-WmiObject -Class Win32_OperatingSystem -Computer $ServerName
$TotalRAM = [math]::round($SystemInfo.TotalVisibleMemorySize/1MB,4)
$FreeRAM = [math]::round($SystemInfo.FreePhysicalMemory/1MB,3)
$UsedRAM = $TotalRAM - $FreeRAM
$RAMPercentFree = [math]::round(($FreeRAM / $TotalRAM) * 100,3)
$RAMPercentUsed = [math]::round(($UsedRAM / $TotalRAM) * 100,3)
# Server's CPU (Proccess) Usage Average
$cpuAVG = Get-WmiObject -computername $ServerName win32_processor | Measure-Object -property LoadPercentage -Average | Select Average
# Value Types being pushed to Server
$Command.CommandText = "
UPDATE
ServerList
SET
FQDN = '$ServerName',
TotalRAM= '$TotalRAM',
FreeRAM= '$FreeRAM',
UsedRAM= '$UsedRAM',
RAMPercentFree = '$RAMPercentFree',
RAMPercentUsed= '$RAMPercentUsed',
OS = '$($os.Caption)'
WHERE
ServerID = $ServerID"
$result = $Command.ExecuteNonQuery()
# Server's Hard Drives (MB) Free/Used
$disks = Get-WmiObject -Class Win32_LogicalDisk -Computer $ServerName | Where-Object {$_.DriveType -eq 3} | ForEach-Object {
$DriveLetter = $_.DeviceID
$DiskFree = [Math]::Round($_.FreeSpace/1MB, 0)
$DiskUsed = [Math]::Round($_.Size/1MB, 0)
$Command.CommandText = "
IF EXISTS (SELECT * FROM ServerDrives WHERE ServerID = '$ServerID' AND DriveLetter = '$DriveLetter')
BEGIN
UPDATE
ServerDrives
SET
DriveLetter = '$DriveLetter',
DiskFree = '$DiskFree',
DiskUsed = '$DiskUsed',
ServerID = '$ServerID'
WHERE
ServerID = '$ServerID' AND DriveLetter = '$DriveLetter'
END
ELSE
BEGIN
INSERT INTO
ServerDrives (DriveLetter, DiskFree, DiskUsed, ServerID)
VALUES
('$DriveLetter','$DiskFree','$DiskUsed', '$ServerID')
END"
$command.ExecuteNonQuery()
}
}