濮阳杆衣贸易有限公司

主頁 > 知識(shí)庫(kù) > powershell遠(yuǎn)程管理服務(wù)器磁盤空間的實(shí)現(xiàn)代碼

powershell遠(yuǎn)程管理服務(wù)器磁盤空間的實(shí)現(xiàn)代碼

熱門標(biāo)簽:電話智能外呼系統(tǒng)誠(chéng)信合作 雷霆電話機(jī)器人電話 什么渠道可以找外呼系統(tǒng)客戶 金融電銷公司怎么辦理外呼系統(tǒng) 真人和電話機(jī)器人對(duì)話 湖州電銷防封卡 安徽400電話辦理 使用電話機(jī)器人電銷是否違法 信陽話務(wù)外呼系統(tǒng)怎么收費(fèi)

一、啟用遠(yuǎn)程管理

1、將管理服務(wù)器的trusthost列表改為*

運(yùn)行Set-item wsman:localhost\client\trustedhosts –value *

2、在遠(yuǎn)程服務(wù)器上運(yùn)行Enable-PSremoting

注:

在本地服務(wù)器上以Administrator運(yùn)行“Enable-Psremoting 、 Winrm Quickconfig 、  Set-WSManQuickConfig”,均提示“訪問被拒絕”,可能的原因如下:

1.在工作組計(jì)算機(jī)上,確認(rèn)組策略: secpol.msc > Local Policies > Security Options > Network Access: Sharing and security model for local accounts - change to classic
2.修改注冊(cè)表:Set-ItemProperty –Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System –Name  LocalAccountTokenFilterPolicy –Value 1 –Type DWord
3.確認(rèn)WinRM服務(wù)是否正在運(yùn)行,Windows Firewall服務(wù)是否正在運(yùn)行,網(wǎng)絡(luò)位置是否不是“公用”,如果要啟用PS遠(yuǎn)程管理,此時(shí)網(wǎng)絡(luò)位置不能被設(shè)置為public,因?yàn)閃indows 防火墻例外不能在網(wǎng)絡(luò)位置是public時(shí)被啟用。
4.Telnet localhost 47001是否可以連通
5.運(yùn)行 winrm get winrm/config 是否會(huì)提示“訪問被拒絕”
6.Administrator密碼不能為空

遠(yuǎn)程啟用開啟之后可以在cmd命令窗口輸入wbemtest測(cè)試是否可以連接遠(yuǎn)程服務(wù)器,如圖:


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


下面就可以來取每個(gè)服務(wù)器的磁盤空間了

二、腳本

$server = "."
$uid = "sa"
$db="master"
$pwd="數(shù)據(jù)庫(kù)sa密碼"
$mailprfname = "test" ---需要跟select name FROM msdb.dbo .sysmail_profile一致
$recipients = "接收郵箱,多個(gè)用;隔開" 
$subject = "郵件標(biāo)題"
$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"--因?yàn)槿〉姆?wù)器用戶名都是administrator,如果每臺(tái)機(jī)器不一樣,可以放在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 = "機(jī)器名:"+$cp+"; 盤符:"+$pfc.get_InnerText()
  $diskFree=";總磁盤空間大小為:"+[math]::truncate($total).ToString()+"G;當(dāng)前剩余空間大小為:"+[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>

完畢,歡迎拍磚!大笑

您可能感興趣的文章:
  • shell腳本快速創(chuàng)建、格式化、掛載新添加的磁盤實(shí)現(xiàn)方法詳解
  • 一天一個(gè)shell命令 linux好管家--磁盤--df命令詳解
  • Powershell中獲取所有磁盤盤符的方法
  • 使用shell腳本采集系統(tǒng)cpu、內(nèi)存、磁盤、網(wǎng)絡(luò)等信息
  • 獲取磁盤IO與系統(tǒng)負(fù)載Load的shell腳本
  • 監(jiān)視磁盤使用情況的Shell腳本(本地+遠(yuǎn)程)
  • shell腳本快速創(chuàng)建格式化磁盤與詳細(xì)操作步驟

標(biāo)簽:鶴崗 德州 六盤水 運(yùn)城 湛江 山南 濟(jì)南 岳陽

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《powershell遠(yuǎn)程管理服務(wù)器磁盤空間的實(shí)現(xiàn)代碼》,本文關(guān)鍵詞  powershell,遠(yuǎn)程管理,服務(wù)器,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《powershell遠(yuǎn)程管理服務(wù)器磁盤空間的實(shí)現(xiàn)代碼》相關(guān)的同類信息!
  • 本頁收集關(guān)于powershell遠(yuǎn)程管理服務(wù)器磁盤空間的實(shí)現(xiàn)代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    清丰县| 江阴市| 彰武县| 东辽县| 鄂温| 罗山县| 三穗县| 嘉义县| 全南县| 宝鸡市| 伊宁市| 隆德县| 玉溪市| 灵宝市| 渝北区| 镇远县| 孝义市| 青川县| 威海市| 康乐县| 多伦县| 河北区| 仁怀市| 昌吉市| 马龙县| 西贡区| 文安县| 华池县| 江西省| 乌苏市| 朝阳市| 宾川县| 景谷| 潍坊市| 福海县| 廉江市| 八宿县| 祁门县| 新宾| 浙江省| 辉县市|