Powershell:检索数据库属性,以百分比和格式转换大小以发送邮件报告
Powershell: retrive DB properties, convert sizes in percentage and formatting to send a mail report
我有一个工作脚本(由一位前同事编写)连接到 mssql 数据库,获取每个数据库的大小并使用我不理解的日志记录和格式化技术将它们报告到 csv 文件中.
这是相关的行:
$reportcsv = $((get-location).path + "\Results\result-size.csv")
Out-File -FilePath $reportcsv -InputObject $("srvname;Db.Name;TotalDBSize;dbSize;LogSize;DbUsedSpace;LogUsedSpace")
foreach ($Server in $icmDBServers)
{
$logstring = "Connessione DB " + $Server.Name
WriteLog -OutFile $LogFile -Type info -text $logString -prompt $true
$DBConnection = new-object Microsoft.SqlServer.Management.Common.ServerConnection
$DBConnection.ServerInstance=$Server.Name
$ServerObj = new-object Microsoft.SqlServer.Management.SMO.Server($DBConnection)
if (----Param cmd condition----)
{
"############################################################################################"
"{0,-28}{1,64}" -f "##","#"
"####### {0,-28} {1,11} {2,38} " -f "S E R V E R N A M E", $Server.Name, "########"
"{0,-28}{1,64}" -f "##","#"
"############################################################################################"
foreach ($db in $ServerObj.Databases)
{
switch ($db.Name )
{
"master" {}
"model" {}
"msdb" {}
default
{
$logstring = ""
$LogUsedSpace = [System.Math]::Round($($db.LogFiles | measure-object -property UsedSpace -sum).sum/1024,2)
$LogSize = [System.Math]::Round($($db.LogFiles | measure-object -property Size -sum).sum/1024,2)
$TotalDBSize = [System.Math]::Round($db.Size,2)
$dbSize = [System.Math]::Round($db.Size - $logsize,2)
$DbUsedSpace = [System.Math]::Round($db.Size - $db.SpaceAvailable/1024,2)
$DBUsageP = $($DbUsedSpace / $TotalDBSize)
$LogUsageP = $($LogUsedSpace / $LogSize)
#"{0,-28} {1,12} ########" -f "####### DB SIZE REPORT for DATABASE ", $db.Name
"{0,-28} {1,50} ###" -f "####### DB SIZE REPORT for DATABASE ", $db.Name
"{0,-28}{1,64}" -f "##","#"
"{0,-28} {1,12} MB {2,44} " -f "DB TotalSize", $TotalDBSize ,"#"
"{0,-28} {1,12} MB {2,44}" -f "DB TotalSize", $TotalDBSize ,"#"
"{0,-28} {1,12} MB {2,44}" -f "DB Size", $dbSize ,"#"
"{0,-28} {1,15} MB {2,44}" -f "DB UsedSpace", $DbUsedSpace ,"#"
"{0,-28} {1,17:p} {2,45}" -f "DB Usage", $DBUsageP ,"#"
"{0,-28} {1,12} MB {2,44}" -f "Log Size", $LogSize ,"#"
"{0,-28} {1,15} MB {2,44}" -f "Log UsedSpace", $logUsedSpace ,"#"
"{0,-28} {1,17:p} {2,45}" -f "Log Usage", $LogUsageP,"#"
"{0,-28}{1,64}" -f "##","#"
"############################################################################################"
$logstring = $Server.Name + ";" + $Db.Name + ";" + $TotalDBSize + ";" + $dbSize + ";" + $LogSize + ";" + $DbUsedSpace + ";" + $LogUsedSpace
Out-File -FilePath $reportcsv -InputObject $logstring -Append
}
}
}
}
"{0,-28}{1,64}" -f "##","#"
"{0,-28}{1,64}" -f "##","#"
实际上文本输出是这样的:
> ## #
> ####### S E R V E R N A M E server0 ########
> ## #
> ############################################################################################
> ####### DB SIZE REPORT for DATABASE db1 ###
> ## # DB TotalSize 1152 MB # DB TotalSize 1152 MB # DB Size 1024 MB # DB UsedSpace 202 MB # DB Usage 17,53% # Log Size 128 MB # Log UsedSpace 2,27 MB # Log Usage 1,77% #
> ## #
> ############################################################################################
我需要对其进行修改,以便我可以仅报告(通过邮件消息)$DBUsageP
值高于 90% 的数据库。
对于我想要检索的数据库:
$Server.Name
$db.Name
$DBUsageP
我认为我需要在 foreach 语句中为捕获和存储 $DBUsageP
值 >90% 设置条件,但我不知道如何实现。
您可以像在列表中那样在变量中捕获您想要的内容:
$over90List = New-Object System.Collections.Generic.List[object]
并在 default
块中添加这些最后几行:
if ($DBUsageP -gt 0.9) {
$over90List.Add([PsCustomObject]@{
Server = $Server.Name
DataBase = $Db.Name
Usage = '{0:p}' -f $DBUsageP
})
}
然后你将在 $over90List
中有一个对象数组,用 ConvertTo-Html 转换成 HTML table 或者用 plain-text 和 |Format-Table -AutoSize | Out-String
输入您的电子邮件。
我有一个工作脚本(由一位前同事编写)连接到 mssql 数据库,获取每个数据库的大小并使用我不理解的日志记录和格式化技术将它们报告到 csv 文件中.
这是相关的行:
$reportcsv = $((get-location).path + "\Results\result-size.csv")
Out-File -FilePath $reportcsv -InputObject $("srvname;Db.Name;TotalDBSize;dbSize;LogSize;DbUsedSpace;LogUsedSpace")
foreach ($Server in $icmDBServers)
{
$logstring = "Connessione DB " + $Server.Name
WriteLog -OutFile $LogFile -Type info -text $logString -prompt $true
$DBConnection = new-object Microsoft.SqlServer.Management.Common.ServerConnection
$DBConnection.ServerInstance=$Server.Name
$ServerObj = new-object Microsoft.SqlServer.Management.SMO.Server($DBConnection)
if (----Param cmd condition----)
{
"############################################################################################"
"{0,-28}{1,64}" -f "##","#"
"####### {0,-28} {1,11} {2,38} " -f "S E R V E R N A M E", $Server.Name, "########"
"{0,-28}{1,64}" -f "##","#"
"############################################################################################"
foreach ($db in $ServerObj.Databases)
{
switch ($db.Name )
{
"master" {}
"model" {}
"msdb" {}
default
{
$logstring = ""
$LogUsedSpace = [System.Math]::Round($($db.LogFiles | measure-object -property UsedSpace -sum).sum/1024,2)
$LogSize = [System.Math]::Round($($db.LogFiles | measure-object -property Size -sum).sum/1024,2)
$TotalDBSize = [System.Math]::Round($db.Size,2)
$dbSize = [System.Math]::Round($db.Size - $logsize,2)
$DbUsedSpace = [System.Math]::Round($db.Size - $db.SpaceAvailable/1024,2)
$DBUsageP = $($DbUsedSpace / $TotalDBSize)
$LogUsageP = $($LogUsedSpace / $LogSize)
#"{0,-28} {1,12} ########" -f "####### DB SIZE REPORT for DATABASE ", $db.Name
"{0,-28} {1,50} ###" -f "####### DB SIZE REPORT for DATABASE ", $db.Name
"{0,-28}{1,64}" -f "##","#"
"{0,-28} {1,12} MB {2,44} " -f "DB TotalSize", $TotalDBSize ,"#"
"{0,-28} {1,12} MB {2,44}" -f "DB TotalSize", $TotalDBSize ,"#"
"{0,-28} {1,12} MB {2,44}" -f "DB Size", $dbSize ,"#"
"{0,-28} {1,15} MB {2,44}" -f "DB UsedSpace", $DbUsedSpace ,"#"
"{0,-28} {1,17:p} {2,45}" -f "DB Usage", $DBUsageP ,"#"
"{0,-28} {1,12} MB {2,44}" -f "Log Size", $LogSize ,"#"
"{0,-28} {1,15} MB {2,44}" -f "Log UsedSpace", $logUsedSpace ,"#"
"{0,-28} {1,17:p} {2,45}" -f "Log Usage", $LogUsageP,"#"
"{0,-28}{1,64}" -f "##","#"
"############################################################################################"
$logstring = $Server.Name + ";" + $Db.Name + ";" + $TotalDBSize + ";" + $dbSize + ";" + $LogSize + ";" + $DbUsedSpace + ";" + $LogUsedSpace
Out-File -FilePath $reportcsv -InputObject $logstring -Append
}
}
}
}
"{0,-28}{1,64}" -f "##","#"
"{0,-28}{1,64}" -f "##","#"
实际上文本输出是这样的:
> ## #
> ####### S E R V E R N A M E server0 ########
> ## #
> ############################################################################################
> ####### DB SIZE REPORT for DATABASE db1 ###
> ## # DB TotalSize 1152 MB # DB TotalSize 1152 MB # DB Size 1024 MB # DB UsedSpace 202 MB # DB Usage 17,53% # Log Size 128 MB # Log UsedSpace 2,27 MB # Log Usage 1,77% #
> ## #
> ############################################################################################
我需要对其进行修改,以便我可以仅报告(通过邮件消息)$DBUsageP
值高于 90% 的数据库。
对于我想要检索的数据库:
$Server.Name
$db.Name
$DBUsageP
我认为我需要在 foreach 语句中为捕获和存储 $DBUsageP
值 >90% 设置条件,但我不知道如何实现。
您可以像在列表中那样在变量中捕获您想要的内容:
$over90List = New-Object System.Collections.Generic.List[object]
并在 default
块中添加这些最后几行:
if ($DBUsageP -gt 0.9) {
$over90List.Add([PsCustomObject]@{
Server = $Server.Name
DataBase = $Db.Name
Usage = '{0:p}' -f $DBUsageP
})
}
然后你将在 $over90List
中有一个对象数组,用 ConvertTo-Html 转换成 HTML table 或者用 plain-text 和 |Format-Table -AutoSize | Out-String
输入您的电子邮件。