powershell遠程管理服務(wù)器磁盤空間的實現(xiàn)代碼
一、啟用遠程管理
1、將管理服務(wù)器的trusthost列表改為*
運行Set-item wsman:localhost\client\trustedhosts –value *
2、在遠程服務(wù)器上運行Enable-PSremoting
注:
在本地服務(wù)器上以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服務(wù)是否正在運行,Windows Firewall服務(wù)是否正在運行,網(wǎng)絡(luò)位置是否不是“公用”,如果要啟用PS遠程管理,此時網(wǎng)絡(luò)位置不能被設(shè)置為public,因為Windows 防火墻例外不能在網(wǎng)絡(luò)位置是public時被啟用。
4.Telnet localhost 47001是否可以連通
5.運行 winrm get winrm/config 是否會提示“訪問被拒絕”
6.Administrator密碼不能為空
遠程啟用開啟之后可以在cmd命令窗口輸入wbemtest測試是否可以連接遠程服務(wù)器,如圖:

連接成功的狀態(tài)如下所示:

下面就可以來取每個服務(wù)器的磁盤空間了
二、腳本
$server = "."
$uid = "sa"
$db="master"
$pwd="數(shù)據(jù)庫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"--因為取的服務(wù)器用戶名都是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>
完畢,歡迎拍磚!大笑
相關(guān)文章
windows Powershell 快速編輯模式和標準模式
powershell控制臺有兩種模式,一個是快速編輯模式,一個是標準模式。2014-08-08
PowerShell中使用Like運算符配合通配符查找字符串例子
這篇文章主要介紹了PowerShell中使用Like運算符配合通配符查找字符串例子,Like的返值為TRUE和FALSE,需要的朋友可以參考下2014-08-08
PowerShell中iso8601格式日期和DateTime對象互轉(zhuǎn)實例
這篇文章主要介紹了PowerShell中iso8601格式日期和DateTime對象互轉(zhuǎn)實例,本文講解了iso8601格式轉(zhuǎn)換成DateTime對象、日期時間轉(zhuǎn)換成iso8601格式兩個方法,需要的朋友可以參考下2015-01-01
PowerShell把IP地址轉(zhuǎn)換成二進制的方法
這篇文章主要介紹了PowerShell把IP地址轉(zhuǎn)換成二進制的方法,在一些IP判斷的場合經(jīng)常使用的小技巧,需要的朋友可以參考下2014-08-08
Powershell實現(xiàn)監(jiān)測服務(wù)器連通狀態(tài)
這篇文章主要介紹了Powershell實現(xiàn)監(jiān)測服務(wù)器連通狀態(tài),代碼很簡單,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下2015-05-05
Windows Powershell ForEach-Object 循環(huán)
Powershell管道就像流水線,對于數(shù)據(jù)的處理是一個環(huán)節(jié)接著一個環(huán)節(jié),如果你想在某一環(huán)節(jié)對流進來的數(shù)據(jù)逐個細致化的處理,可是使用ForEach-Object,$_ 代表當前的數(shù)據(jù)。2014-10-10

