First, enable remote administration
1. Change the Trusthost list of the Management Server to *
Run Set-item Wsman:localhost\client\trustedhosts–value *
2, run the enable-psremoting on the remote server
Note:
Running "enable-psremoting, Winrm quickconfig, Set-wsmanquickconfig" with the administrator on the local server prompts "Access denied" for the following possible reasons:
1. On the workgroup computer, confirm Group Policy: secpol.msc > Local Policies > Security Options > Network access:sharing and security model for Local Accounts-change to Classic
2. Modify the registry:Set-itemproperty–path Hklm:\software\microsoft\windows\currentversion\policies\system–name Localaccounttokenfilterpolicy–value 1–type DWord
3. Verify that the WinRM service is running, that the Windows Firewall service is running, that the network location is not public, and that if you want to enable PS remote management, the network location cannot be set to publicly at this time because Windows Firewall exceptions cannot be enabled when the network location is public.
4.Telnet localhost 47001 can connect
5. Run WinRM get Winrm/config will prompt "Access denied"
6.Administrator Password cannot be empty
After remote enabled, you can enter WBEMTest in the CMD command window to test whether the remote server can be connected, as shown in figure:
The status of the successful connection is as follows:
You can take the disk space for each server below.
Second, the script
$server = "." $uid = "sa" $db = "Master" $pwd = "database sa password" $mailprfname = "test"---need to be with select name from msdb.dbo. sysmail_prof Ile Consistent $recipients = "Receive mailboxes, multiple use; separate" $subject = "message header" $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]} $return Pwd. ADD ($PW. Trim ())} $returnpwd} function Createalter ($message) {$SqlConnection = New-object System.Data.SqlClient.SqlConnect Ion $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 $u Name= "Administrator"-because the server user name is administrator, if each machine is different, can be placed in XML and other files read $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 = "Machine Name:" + $CP + "; Letter: "+ $PFC. Get_innertext () $diskFree ="; Total disk space size is: +[math]::truncate ($total). ToString () + "G; The current remaining space size is:" +[math]::truncate ($counter).
ToString () + "g!" $item = "{0} {1""-F $path, $diskFree $report + = $item +" ' N "}} $report if ($report-ne" ") {Createalter $report}
Effect:
Report:
XML file Format:
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>
Finished, welcome to the Brick! Laugh