powershell远程管理服务器磁盘空间的实现代码(powershell远程连接服务器)学会了吗

随心笔谈2年前发布 admin
195 0 0

文章摘要

这篇文章介绍了一段使用PowerShell编写的代码,用于从SQL 身份管理器(SSM)中获取服务器信息,并通过自动化报告的形式发送电子邮件。代码的主要功能包括: 1. 从指定路径的XML文件中读取服务器名称、设备计数器和密码等信息。 2. 使用这些信息连接到目标SQL 服务器,并执行发送电子邮件的任务。 3. 通过循环处理每个计算机名,生成包含磁盘空间信息的报告,并发送给指定的接收方。 代码中定义了几个函数: - `GetServerName`:从指定XML文件中提取服务器名称。 - `GetAlterCounter`:从指定XML文件中提取设备计数器。 - `Getpwd`:从指定XML文件中提取密码。 - `CreateAlter`:发送电子邮件并生成报告。 整体功能是通过自动化流程从SSM获取服务器信息,生成报告并发送给指定邮箱。


$server=”.”
$uid=”sa”
$db=”master”
$pwd=”数据库sa密码”
$mailprfname=”test” —需要跟select name FROM msdb.dbo .sysmail_profile一致
$recipients=”接收邮箱,多个用;隔开”
$subject=”邮件标题”
$computernamexml=”E:\powershell\computername.xml”
$alter_xml=”E:\powershell\cpdisk.xml”
$pwd_xml=”E:\powershell\pwd.xml”
function GetServerName($xmlpath)
{
$xml=[xml] (Get-Content $xmlpath)
$return=New-Object Collections.Generic.List[string]
for($i=0;$i -lt $xml.computernames.ChildNodes.Count;$i++)
{
if ( $xml.computernames.ChildNodes.Count -eq 1)
{
$cp=[string]$xml.computernames.computername
}
else
{
$cp=[string]$xml.computernames.computername[$i]
}
$return.Add($cp.Trim())
}
$return
}
function GetAlterCounter($xmlpath)
{
$xml=[xml] (Get-Content $xmlpath)
$return=New-Object Collections.Generic.List[string]
$list=$xml.counters.Counter
$list
}
function Getpwd($xmlpath)
{
$xml=[xml] (Get-Content $xmlpath)
$returnpwd=New-Object Collections.Generic.List[string]
for($i=0;$i -lt $xml.pwd.ChildNodes.Count;$i++)
{
if ( $xml.pwds.ChildNodes.Count -eq 1)
{
$pw=[string]$xml.pwd.password
}
else
{
$pw=[string]$xml.pwd.password[$i]
}
$returnpwd.Add($pw.Trim())
}
$returnpwd
}
function CreateAlter($message)
{
$SqlConnection=New-Object System.Data.SqlClient.SqlConnection
$CnnString=”Server=$server; Database=$db;User Id=$uid; Password=$pwd”
$SqlConnection.ConnectionString=$CnnString
$CC=$SqlConnection.CreateCommand();
if (-not ($SqlConnection.State -like “Open”)) { $SqlConnection.Open() }

$cc.CommandText=” EXEC msdb..sp_send_dbmail
@profile_name=’$mailprfname’
,@recipients=’$recipients’
,@body=’$message’
,@subject=’$subject’

$cc.ExecuteNonQuery()|out-null
$SqlConnection.Close();
}
$names=GetServerName($computernamexml)
$pfcounters=GetAlterCounter($alter_xml)
$upwd=Getpwd($pwd_xml)
$report=””
for($m=0;$m -lt $names.count;$m++)
{
$cp=$names[$m]
$p=New-Object -TypeName System.Collections.ArrayList
$uname=”administrator”–因为取的服务器用户名都是administrator,如果每台机器不一样,可以放在XML等文件中读取
$pw=$upwd[$m]
$upassword=convertto-securestring $pw -AsplainText -force;
foreach ($pfc in $pfcounters)
{
$filter=”deviceID='”+$pfc.get_InnerText().Trim()+”‘”
#$Disk=get-wmiobject win32_logicaldisk -computername $cp -Filter $filter
#$counter=$Disk.Freespace/1024MB
$cred=new-object system.management.automation.PSCredential($uname,$upassword);
$counter=(get-wmiobject -credential $cred -class win32_logicaldisk -computername $cp -filter $filter).Freespace/1024MB
$total=(get-wmiobject -credential $cred -class win32_logicaldisk -computername $cp -filter $filter).Size/1024MB
#$pfc=$pfcounters[$i]
$path=”机器名:”+$cp+”; 盘符:”+$pfc.get_InnerText()
$diskFree=”;总磁盘空间大小为:”+[math]::truncate($total).ToString()+”G;当前剩余空间大小为:”+[math]::truncate($counter).ToString()+”G!”
$item=”{0} {1} ” -f $path,$diskFree
$report +=$item + “`n”
}

}
$report
if($report -ne “”)
{
CreateAlter $report
}

© 版权声明

相关文章