Implementation code for PowerShell remote Management Server disk space

Source: Internet
Author: User
Tags administrator password

First, enable remote management

1. Change the Trusthost list of the Management Server to *

Run Set-item wsman:localhostclienttrustedhosts? Value *

2. Run enable-psremoting on the remote server

Note:

Run "enable-psremoting, Winrm Quickconfig, Set-wsmanquickconfig" on the local server with administrator, prompting "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:softwaremicrosoftwindowscurrentversionpoliciessystem? 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 common because Windows Firewall exceptions cannot be enabled when the network location is public.
Whether 4.Telnet localhost 47001 can be connected
5. Running WinRM get Winrm/config will prompt "Access denied"
6.Administrator Password cannot be empty

After remote enable on, you can enter WBEMTest in the CMD command window to test if you can connect to a remote server.

The status of the connection success 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 followSelect name from msdb.dbo. Sysmail_profile consistent$recipients ="Receive mailbox, multiple use;$subject ="Message title"$computernamexml ="E:powershellcomputername.xml"$alter _xml ="E:powershellcpdisk.xml"$pwd _xml ="E:powershellpwd.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-eq1) {$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-eq1) {$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"-because the server user name is administrator, if each machine is different, can be placed in XML and other files to 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-objectSystem.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 +"; Drive letter: "+$pfc. Get_innertext ()$diskFree ="; Total disk space size:" +[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"}}$reportif (
                                                                                                                                                                                   
                                                                                                                                                                                     $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 shoot Bricks! Laugh

Implementation code for PowerShell remote Management Server disk space

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.