此PowerShell脚本非常有用,如果您在DMZ中有SQL服务器,则可以使用它来获取总容量,已用容量,可用空间和可用空间%。我使用相同的脚本来检查所有内部SQL服务器的硬盘空间。但是,当我尝试在DMZ中对我的SQL服务器运行时,会发生一件奇怪的事情。
此PowerShell脚本从名为dmzsl.txt的文件中获取SQL Server列表。当我仅使用一个SQL服务器更新dmzsl.txt文件并运行PowerShell脚本时,我会为同一服务器返回3个结果集。
我已经尝试了几种方法来解决这个问题,但我不能让它用一个数据集报告而不重复。
set-location C:\SQLServerDiskSpaceReport
$isodate=Get-Date -format s
$isodate=$isodate -replace(":","")
$basepath=(Get-Location -PSProvider FileSystem).ProviderPath
$instancepath=$basepath + "\config\dmzsl.txt"
$outputfile="\DMZServerLogs\sql_server_db_sizes_" + $isodate + ".html"
$outputfilefull = $basepath + $outputfile
$filePath = ""
$freeSpaceFileName = $outputfilefull
$warning = 30
$critical = 10
New-Item -ItemType file $freeSpaceFileName -Force
Function writeHtmlHeader {
param($fileName)
$date = ( get-date ).ToString('MM/dd/yyyy')
Add-Content $fileName "<html>"
Add-Content $fileName "<head>"
Add-Content $fileName "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>"
Add-Content $fileName '<title>DiskSpace Report</title>'
add-content $fileName '<STYLE TYPE="text/css">'
add-content $fileName "<!--"
add-content $fileName "td {"
add-content $fileName "font-family: Tahoma;"
add-content $fileName "font-size: 11px;"
add-content $fileName "border-top: 1px solid #999999;"
add-content $fileName "border-right: 1px solid #999999;"
add-content $fileName "border-bottom: 1px solid #999999;"
add-content $fileName "border-left: 1px solid #999999;"
add-content $fileName "padding-top: 0px;"
add-content $fileName "padding-right: 0px;"
add-content $fileName "padding-bottom: 0px;"
add-content $fileName "padding-left: 0px;"
add-content $fileName "}"
add-content $fileName "body {"
add-content $fileName "margin-left: 5px;"
add-content $fileName "margin-top: 5px;"
add-content $fileName "margin-right: 0px;"
add-content $fileName "margin-bottom: 10px;"
add-content $fileName ""
add-content $fileName "table {"
add-content $fileName "border: thin solid #000000;"
add-content $fileName "}"
add-content $fileName "-->"
add-content $fileName "</style>"
Add-Content $fileName "</head>"
Add-Content $fileName "<body>"
add-content $fileName "<table width='100%'>"
add-content $fileName "<tr bgcolor='#CCCCCC'>"
add-content $fileName "<td colspan='7' height='25' align='center'>"
add-content $fileName "<font face='tahoma' color='#003399' size='4'><strong>DMZ SQL Server Disk Space Report - $date</strong></font>"
add-content $fileName "</td>"
add-content $fileName "</tr>"
add-content $fileName "</table>"
}
# Function to write the HTML Header to the file
Function writeTableHeader {
param($fileName)
Add-Content $fileName "<tr bgcolor=#CCCCCC>"
Add-Content $fileName "<td width='10%' align='center'>Drive</td>"
Add-Content $fileName "<td width='50%' align='center'>Drive Label</td>"
Add-Content $fileName "<td width='10%' align='center'>Total Capacity(GB)</td>"
Add-Content $fileName "<td width='10%' align='center'>Used Capacity(GB)</td>"
Add-Content $fileName "<td width='10%' align='center'>Free Space(GB)</td>"
Add-Content $fileName "<td width='10%' align='center'>Freespace %</td>"
Add-Content $fileName "</tr>"
}
Function writeHtmlFooter {
param($fileName)
Add-Content $fileName "</body>"
Add-Content $fileName "</html>"
}
Function writeDiskInfo {
param($fileName,$devId,$volName,$frSpace,$totSpace)
$totSpace=[math]::Round(($totSpace/1073741824),2)
$frSpace=[Math]::Round(($frSpace/1073741824),2)
$usedSpace = $totSpace - $frspace
$usedSpace=[Math]::Round($usedSpace,2)
$freePercent = ($frspace/$totSpace)*100
$freePercent = [Math]::Round($freePercent,0)
if ($freePercent -gt $warning) {
Add-Content $fileName "<tr>"
Add-Content $fileName "<td>$devid</td>"
Add-Content $fileName "<td>$volName</td>"
Add-Content $fileName "<td>$totSpace</td>"
Add-Content $fileName "<td>$usedSpace</td>"
Add-Content $fileName "<td>$frSpace</td>"
Add-Content $fileName "<td>$freePercent</td>"
Add-Content $fileName "</tr>"
} elseif ($freePercent -le $critical) {
Add-Content $fileName "<tr>"
Add-Content $fileName "<td>$devid</td>"
Add-Content $fileName "<td>$volName</td>"
Add-Content $fileName "<td>$totSpace</td>"
Add-Content $fileName "<td>$usedSpace</td>"
Add-Content $fileName "<td>$frSpace</td>"
Add-Content $fileName "<td bgcolor='#e60000' align=center>$freePercent</td>"
#<td bgcolor='#e60000' align=center>
Add-Content $fileName "</tr>"
} else {
Add-Content $fileName "<tr>"
Add-Content $fileName "<td>$devid</td>"
Add-Content $fileName "<td>$volName</td>"
Add-Content $fileName "<td>$totSpace</td>"
Add-Content $fileName "<td>$usedSpace</td>"
Add-Content $fileName "<td>$frSpace</td>"
Add-Content $fileName "<td bgcolor='#ffff00' align=center>$freePercent</td>"
# #ffff00
Add-Content $fileName "</tr>"
}
}
writeHtmlHeader $freeSpaceFileName
foreach ($server in Get-Content $instancepath) {
Add-Content $freeSpaceFileName "<table width='100%'><tbody>"
Add-Content $freeSpaceFileName "<tr bgcolor='#CCCCCC'>"
Add-Content $freeSpaceFileName "<td width='100%' align='center' colSpan=6><font face='tahoma' color='#003399' size='2'><strong> $server </strong></font></td>"
Add-Content $freeSpaceFileName "</tr>"
writeTableHeader $freeSpaceFileName
$dp = Get-WmiObject win32_logicaldisk -ComputerName $server | Where-Object {$_.drivetype -eq 3}
foreach ($item in $dp) {
Write-Host $item.DeviceID $item.VolumeName $item.FreeSpace $item.Size
writeDiskInfo $freeSpaceFileName $item.DeviceID $item.VolumeName $item.FreeSpace $item.Size
}
}
$Command1 = "C:\SQLServerDiskSpaceReport\DMZServerLogs\sql_server_db_sizes_"
$Command2 = $isodate + ".html"
$Command3 = $Command1 +$Command2
Invoke-Expression $Command3
我收到一条错误消息,但我不熟悉PowerShell以了解这个错误。错误也有点含糊不清,因为你可以看到它无法验证参数&#39; computername&#39;的参数。但是它能够为同一台服务器生成3次报告。
Get-WmiObject : Cannot validate argument on parameter 'ComputerName'. The
argument is null or empty. Provide an argument that is not null or empty,
and then try the command again.
At C:\SQLServerDiskSpaceReport\DMZ Disk Space Report V3.ps1:155 char:54
+ $dp = Get-WmiObject win32_logicaldisk -ComputerName $server | Where-Object {$_ ...
+ ~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-WmiObject], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetWmiObjectCommand
答案 0 :(得分:1)
错误消息实际上是不言自明的。您的循环变量$server
为空。检查文件$instancepath
。