亚洲国产欧美一区二区三区f,亚洲A∨精品永久无码青草网,亚洲 暴爽 av人人爽日日碰,亚洲AV永久无码精心天堂久久_无码

系統城裝機大師 - 唯一官網:www.outletmksalestore.com!

當前位置:首頁 > 腳本中心 > PowerShell > 詳細頁面

powershell遠程管理服務器磁盤空間的實現代碼

時間:2020-01-28來源:系統城作者:電腦系統城

一、啟用遠程管理

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測試是否可以連接遠程服務器,如圖:

連接成功的狀態如下所示:

下面就可以來取每個服務器的磁盤空間了

二、腳本


 
  1. $server = "."
  2. $uid = "sa"
  3. $db="master"
  4. $pwd="數據庫sa密碼"
  5. $mailprfname = "test" ---需要跟select name FROM msdb.dbo .sysmail_profile一致
  6. $recipients = "接收郵箱,多個用;隔開"
  7. $subject = "郵件標題"
  8. $computernamexml = "E:\powershell\computername.xml"
  9. $alter_xml = "E:\powershell\cpdisk.xml"
  10. $pwd_xml = "E:\powershell\pwd.xml"
  11. function GetServerName($xmlpath)
  12. {
  13. $xml = [xml] (Get-Content $xmlpath)
  14. $return = New-Object Collections.Generic.List[string]
  15. for($i = 0;$i -lt $xml.computernames.ChildNodes.Count;$i++)
  16. {
  17. if ( $xml.computernames.ChildNodes.Count -eq 1)
  18. {
  19. $cp = [string]$xml.computernames.computername
  20. }
  21. else
  22. {
  23. $cp = [string]$xml.computernames.computername[$i]
  24. }
  25. $return.Add($cp.Trim())
  26. }
  27. $return
  28. }
  29. function GetAlterCounter($xmlpath)
  30. {
  31. $xml = [xml] (Get-Content $xmlpath)
  32. $return = New-Object Collections.Generic.List[string]
  33. $list = $xml.counters.Counter
  34. $list
  35. }
  36. function Getpwd($xmlpath)
  37. {
  38. $xml = [xml] (Get-Content $xmlpath)
  39. $returnpwd = New-Object Collections.Generic.List[string]
  40. for($i = 0;$i -lt $xml.pwd.ChildNodes.Count;$i++)
  41. {
  42. if ( $xml.pwds.ChildNodes.Count -eq 1)
  43. {
  44. $pw = [string]$xml.pwd.password
  45. }
  46. else
  47. {
  48. $pw = [string]$xml.pwd.password[$i]
  49. }
  50. $returnpwd.Add($pw.Trim())
  51. }
  52. $returnpwd
  53. }
  54. function CreateAlter($message)
  55. {
  56. $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
  57. $CnnString ="Server = $server; Database = $db;User Id = $uid; Password = $pwd"
  58. $SqlConnection.ConnectionString = $CnnString
  59. $CC = $SqlConnection.CreateCommand();
  60. if (-not ($SqlConnection.State -like "Open")) { $SqlConnection.Open() }
  61.  
  62. $cc.CommandText=
  63. " EXEC msdb..sp_send_dbmail
  64. @profile_name = '$mailprfname'
  65. ,@recipients = '$recipients'
  66. ,@body = '$message'
  67. ,@subject = '$subject'
  68. "
  69. $cc.ExecuteNonQuery()|out-null
  70. $SqlConnection.Close();
  71. }
  72. $names = GetServerName($computernamexml)
  73. $pfcounters = GetAlterCounter($alter_xml)
  74. $upwd = Getpwd($pwd_xml)
  75. $report = ""
  76. for($m=0;$m -lt $names.count;$m++)
  77. {
  78. $cp=$names[$m]
  79. $p=New-Object -TypeName System.Collections.ArrayList
  80. $uname="administrator"--因為取的服務器用戶名都是administrator,如果每臺機器不一樣,可以放在XML等文件中讀取
  81. $pw=$upwd[$m]
  82. $upassword=convertto-securestring $pw -AsplainText -force;
  83. foreach ($pfc in $pfcounters)
  84. {
  85. $filter="deviceID='"+$pfc.get_InnerText().Trim()+"'"
  86. #$Disk =get-wmiobject win32_logicaldisk -computername $cp -Filter $filter
  87. #$counter=$Disk.Freespace/1024MB
  88. $cred=new-object system.management.automation.PSCredential($uname,$upassword);
  89. $counter=(get-wmiobject -credential $cred -class win32_logicaldisk -computername $cp -filter $filter).Freespace/1024MB
  90. $total=(get-wmiobject -credential $cred -class win32_logicaldisk -computername $cp -filter $filter).Size/1024MB
  91. #$pfc = $pfcounters[$i]
  92. $path = "機器名:"+$cp+"; 盤符:"+$pfc.get_InnerText()
  93. $diskFree=";總磁盤空間大小為:"+[math]::truncate($total).ToString()+"G;當前剩余空間大小為:"+[math]::truncate($counter).ToString()+"G!"
  94. $item = "{0} {1} " -f $path,$diskFree
  95. $report += $item + "`n"
  96. }
  97.  
  98. }
  99. $report
  100. if($report -ne "")
  101. {
  102. CreateAlter $report
  103. }

效果:

附:

xml文件格式:

1、computername.xml


 
  1. <computername>
  2. <computername>
  3. test
  4. </computername>
  5. </computernames>

2、cpdisk.xml


 
  1. <Counters>
  2. <Counter>C:</Counter>
  3. <Counter>D:</Counter>
  4. </Counters>

3、pwd.xml


 
  1. <pwd>
  2. <password>
  3. helloworld
  4. </password>
  5. <pwd>

完畢,歡迎拍磚!大笑

分享到:

相關信息

系統教程欄目

欄目熱門教程

人氣教程排行

站長推薦

熱門系統下載

亚洲国产欧美一区二区三区f,亚洲A∨精品永久无码青草网,亚洲 暴爽 av人人爽日日碰,亚洲AV永久无码精心天堂久久_无码 日本少妇又色又爽又高潮