powershell 원 격 관리 서버 디스크 공간의 실현 코드

원 격 관리 사용
1.서버 를 관리 하 는 trusthost 목록 을*로 변경 합 니 다.
Set-item wsman:localhost\client\\trustedhosts Cvalue 실행*
2.원 격 서버 에서 Enable-PSremoting 실행
주:
로 컬 서버 에서 Administrator 로"Enable-Psremoting,Winrm Quickconfig,  Set-WSman QuickConfig"는"접근 이 거부 되 었 습 니 다"라 고 알려 줍 니 다.가능 한 이 유 는 다음 과 같 습 니 다.
1.작업 그룹 컴퓨터 에서 그룹 정책 확인:secpol.msc>Local Policies>Security Options>Network Access:Sharing and security model for local account-change to classic
2.레 지 스 트 수정:Set-iteMPerty CPath HKLM:\\SOFTWARE\Microsoft\\Windows\\CurrentVersion\Policies\\System CName  LocalAccountTokenFilterPolicy CValue 1 CType 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>
이상,벽돌 을 찍 는 것 을 환영 합 니 다!크게 웃다

좋은 웹페이지 즐겨찾기