PowerShell Collection server daily check report, concurrent mail to Administrator script modification applied to production environment instance

Source: Internet
Author: User
Tags cpu usage mailmessage

The former saw an article in the big direction and felt that the script he shared was very useful. Original address: http://blog.51cto.com/hubuxcg/1604582

But in the process of deployment still encountered a variety of problems, and now the article to record.

    1. The first problem encountered was that the script could not read the CPU usage. The original code is as follows:

$CPULoad = (get-wmiobject win32_processor-computername $Computer). Loadpercentage

After the check found that the server's CPU has 2, so can not be displayed, the code is modified to:

$CPULoad = (gwmi–computername $Computer win32_processor) |select Loadpercentage|foreach{echo "CPU:" $_. Loadpercentage}

This allows multiple CPUs to be read.

2. The script run needs to run the command account to the machine has Administrator rights, Division I in the domain of the other machine is taken off the domain of the administrator account. So you need to open an account specifically to operate the script.

After the new account is added to Builtin's Administrators group, the account has administrative access to the DCs within the domain.

In addition, I use this account to run the script, so you want to read the content of all the server Administrators group to add the account.

3. Scheduled tasks cannot be performed

Original code:

$ServerName = (get-content ". \serverlist.txt")

The problem here is that if you want to add a script to a scheduled task, you can't use a relative address, but an absolute address.

$ServerName = (get-content "D:\script\serverlist.txt")

There is no problem after the change.

4. The original script requires an anonymous relay of the open execution host to send the message. If you do not want to open the host's anonymous relay permissions for security reasons. You can modify the following location:

Send-mailmessage @email

Modified to:

$ss =convertto-securestring-string "actual account password"-asplaintext-force$ss| write-host$cre= new-object System.Management.Automation.PSCredential ("Actual account", $SS)

Add parameters later

Send-mailmessage @email-credential $cre

5. The script uses WMI classes primarily to read server content. The WMI class can also use the-credential parameter to increase credentials, which makes it possible to read non-domain machines.

Add the credential code in front of the script

$ss =convertto-securestring-string "actual account password"-asplaintext-force$ss| write-host$cre= new-object System.Management.Automation.PSCredential ("Actual account", $SS)

After each get-wmiobject win32_processor add a parameter-credential adding credentials

Why do you do this? Instead of executing on the non-domain machine itself? Because the company also has some 2003 of the machine does not add domain, also no powershell ...

Here is a problem, read the log function Get-eventlog, found the use of WMI class instead of Get-wmiobject-class win32_ntlogevent but need to modify the corresponding content according to different language

Original:

$ApplicationEvents = get-eventlog-computername $Computer-logname application-entrytype error,warning-newest $ Eventnum

After modification:

$ApplicationEvents =get-wmiobject-computername $Computer-credential $cre-class win32_ntlogevent-filter ' (type= "error" or type= "Warning") and logfile= "Application" ' |select-first $EventNum-property Type, timegenerated,sourcename, message

If the English system is changed (type= "Error" or type= "Warning").

6.WMI need to open firewall corresponding port,

Use the cmd command as follows:

netsh firewall set service remoteadmin enable

Or

netsh advfirewall set allprofiles settings remotemanagement enable



The modified script:

http://down.51cto.com/data/2440489





PowerShell Collection server daily check report, concurrent mail to Administrator script modification applied to production environment instance

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.