一、启用远程管理
1、将管理服务器的trusthost列表改为*
运行set-item wsman:localhost\client\trustedhosts –value *
2、在远程服务器上运行enable-psremoting
注:
在本地服务器上以administrator运行“enable-psremoting 、 winrm quickconfig 、 set-wsmanquickconfig”,均提示“访问被拒绝”,可能的原因如下:
1.在工作组计算机上,确认组策略: secpol.msc > local policies > security options > network access: sharing and security model for local accounts - change to classic
2.修改注册表:set-itemproperty –path hklm:\software\microsoft\windows\currentversion\policies\system –name localaccounttokenfilterpolicy –value 1 –type dword
3.确认winrm服务是否正在运行,windows firewall服务是否正在运行,网络位置是否不是“公用”,如果要启用ps远程管理,此时网络位置不能被设置为public,因为windows 防火墙例外不能在网络位置是public时被启用。
4.telnet localhost 47001是否可以连通
5.运行 winrm get winrm/config 是否会提示“访问被拒绝”
6.administrator密码不能为空
远程启用开启之后可以在cmd命令窗口输入wbemtest测试是否可以连接远程服务器,如图:

连接成功的状态如下所示:

下面就可以来取每个服务器的磁盘空间了
二、脚本
$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
}
效果:

附:
xml文件格式:
1、computername.xml
<computername>
<computername>
test
</computername>
</computernames>
2、cpdisk.xml
<counters>
<counter>c:</counter>
<counter>d:</counter>
</counters>
3、pwd.xml
<pwd>
<password>
helloworld
</password>
<pwd>
完毕,欢迎拍砖!大笑
发表评论