根据我的理解,我的查询结果未正确保存/转发。应该发生的是每个服务器(svr01,svr03,svr05,svr06,svr08)我应该获得他们的操作系统,完整域名等。然后这个信息用于填充我的SQL数据库(Hal0Test)。但是,该信息永远不会被发送,我不知道如何解决这个问题。
我在我的powershell脚本中添加了一个小的html输出,以便在本地查看我的结果。 html对我来说更容易打开然后运行SQL Server Management Studio并刷新该程序。但我仍然希望将所有这些变量发送到数据库。
目标:纠正重复的问题和开始将该信息发送到我的sql数据库。如果你能帮助我那将是令人惊叹的!
代码:
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
#Variables for each Server in Array
$ServerArray | ForEach-Object {
# $ServerArray returns each server name
#Operating System
$os = Get-WmiObject -Class Win32_OperatingSystem -Computer $_
#Server's Memory (RAM) Usage Average
$memAvg = gwmi -Class win32_operatingsystem -computername $_ |
Select-Object @{Name = "MemoryUsage"; Expression = {“{0:N2}” -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize) }}
#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)
}
New-Object -Type PSCustomObject -Property @{
'FQDN' = $_
'ServerName' = $os.PSComputerName
'OperatingSystem' = $os.Caption
'CPUAvg' = "$($cpuAVG.Average)%"
'MemAvg' = "$($memAvg.MemoryUsage)%"
'Disks' = $disks -join ' | '
}
$command.CommandText = "UPDATE ServerList SET FQDN = '$_', OS = '$($os.Caption)' WHERE ServerName = '$($os.PSComputerName)';"
$result = $command.ExecuteNonQuery()
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
#Start of HTML Code
$Outputreport =
"<HTML><TITLE> IDI Server Report </TITLE>
<BODY background-color:peachpuff>
<font color =""#99000"" face=""Microsoft Tai le"">
<H2> IDI Server Report </H2></font>
<Table border=1 cellpadding=0 cellspacing=0>
<TR bgcolor=gray align=center>
<TD><B>Server Name</B></TD>
<TD><B>FQDN</B></TD>
<TD><B>CPU Usage</B></TD>
<TD><B>RAM Usage</B></TD>
<TD><B>Disk Space</B></TD>
<TD><B>Operating System</B></TD>
<TD><B>Query result</B></TD></TR>"
$ServerArray | ForEach-Object {
# $ServerArray returns each server name
# collection code skipped
$command.CommandText = "UPDATE ServerList SET FQDN = '$_', OS = '$($os.Caption)' WHERE ServerName LIKE '$($os.PSComputerName)%';"
$result = $command.ExecuteNonQuery()
if($cpuAVG.Average -ge "2")
{
$Outputreport += "<TR bgcolor=#FF0000>"
}
else
{
$Outputreport += "<TR bgcolor=#E6E6FA>"
}
$Outputreport +=
"<TD>$($os.PSComputerName)</TD>
<TD align=center>$($_)</TD>
<TD align=center>$("$($cpuAVG.Average)%")</TD>
<TD align=center>$("$($memAvg.MemoryUsage)%")</TD>
<TD align=center>$($disks)</TD>
<TD align=center>$($os.Caption)</TD></TR>
<TD align=center>$($result)</TD></TR>"
}
$Outputreport += "</Table></BODY></HTML>"
$Outputreport | out-file C:\Users\king\Desktop\HalO\ServerReport.html
Invoke-Expression C:\Users\king\Desktop\HalO\ServerReport.html
}
Write-Output "`n End of Hal0";
#End
HTML结果:
SQL数据库:仅供参考我手动添加&#34; char&#34;
答案 0 :(得分:2)
您正在最外层的foreach循环中创建基于HTML的报告,但您的$_
仅限于当前正在处理的服务器。相反,在主循环开始之前将包装器HTML放入$Outputreport
,然后在循环内添加包含当前数据的<tr>...</tr>
部分。并将第二个循环放在$ServerArray
上。还在报告中添加了$result
,显然所有更新查询都会生成0 row(s) updated
而您不明白为什么。 (一个疯狂的猜测:编码不匹配(UTF16与数据库的UTF8))可能你可能决定INSERT INTO ServerList
数据并删除旧行,这样你的关键字段肯定会是一致的。
#Start of HTML Code
$Outputreport = "<HTML><TITLE> IDI Server Report </TITLE>
<BODY background-color:peachpuff>
<font color =""#99000"" face=""Microsoft Tai le"">
<H2> IDI Server Report </H2></font>
<Table border=1 cellpadding=0 cellspacing=0>
<TR bgcolor=gray align=center>
<TD><B>Server Name</B></TD>
<TD><B>FQDN</B></TD>
<TD><B>CPU Usage</B></TD>
<TD><B>RAM Usage</B></TD>
<TD><B>Disk Space</B></TD>
<TD><B>Operating System</B></TD>
<TD><B>Query result</B></TD></TR>"
$ServerArray | ForEach-Object {
# $ServerArray returns each server name
# collection code skipped
$command.CommandText = "UPDATE ServerList SET FQDN = '$_', OS = '$($os.Caption)' WHERE ServerName LIKE '$($os.PSComputerName)%';"
$result = $command.ExecuteNonQuery()
if($cpuAVG.Average -ge "2")
{
$Outputreport += "<TR bgcolor=#FF0000>"
}
else
{
$Outputreport += "<TR bgcolor=#E6E6FA>"
}
$Outputreport += @"
<TD>$($os.PSComputerName)</TD>
<TD align=center>$($_)</TD>
<TD align=center>$($cpuAVG.Average)%</TD>
<TD align=center>$($memAvg.MemoryUsage)%</TD>
<TD align=center>$($disks)</TD>
<TD align=center>$($os.Caption)</TD></TR>
<TD align=center>$($result)</TD></TR>
"@
# used a here-string for clarification
}
$Outputreport += "</Table></BODY></HTML>"
我还将where子句更改为like语句,可能您的数据库在ServerName
列中包含FQDN。