I am having trouble populating my second table in my SQL Server Management Studio.
My first table ServerList contains all information in the screenshot below. The second table ServerDrives should then contain all the information regarding hard drive specs (free, used, letter, and which server are the hard drives connected to).
Currently, I have my powershell script pull from the ServerList all the values in the column ServerName and push info into the powershell script which will then populate OS, RAM, and etc.
Issue: I am not sure how to have the same powershell script use the same primary key ServerID from table ServerList to populate my ServerDrives table. For I want each row in that table to be one for each server in ServerList. I have in the script currently a way to collect the hard drive information but I am not sure how to use that info to populate my empty table
Goal: To have ServerID (primary key in ServerList) and ServerDriveID (primary key in ServerDrives) be for the same server (QAGGAPP01, QAGGAPP03, QAGGAPP05, and etc). To have the variable $disks be pushed in the database table ServerDrives with the proper hard drives letters and specs. Each server has 1 or 2 hard drives.
Code:
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 Preview, I only have in my powershell code just to make it easier to see what is being pushed to the server without having to reopen the tables everytime:

ServerDrivestable is empty. There's nothing to update then – instead, you want to INSERT data into it. On the other hand, if your PS script is supposed to be run more than once, you would need to either clear that table each time before repopulating it or merge the new info with the contents ofServerDrives, i.e. some of its rows would be updated, others inserted, while still others even deleted (if that is a possibility). If you clean up the table every time,ServerDriverIDvalues will be generated every time anew.