PowerShell implementation time management small Secretary _powershell

Source: Internet
Author: User
Tags current time

Introduction

If you feel that you are still alive, why can others do so much?

is not often 8 hours in the class down, but do not know what they have done?

is not accustomed to in the email, BBS, Google reader switch between, feel very busy?

You need time management!

Do you know how much code you wrote in one day?

Do you know how much time you spend on the internet in a week?

Can you name a few days with your family for one months?

You need powershell!.

Management? Record first!

Time management is doing the most things with the least amount of time. As with the performance of the optimizer, in order to improve the utilization of time, you have to know where your time is spent. If 50% of working hours are wasted on trivia, half the rest of the time is spent concentrating on the results of the day. Accordingly, if 90% of the time is working, compared to trying to narrow the rest of the wandering time, a little more efficient. Archery emphasis on targeted, medical treatment stresses the right remedy. Knowing the bottleneck is the premise of optimization.

So how do you know where your weaknesses are? Record and develop the habit of recording.

Just imagine when the end of the week, you see a statistical table: this week overtime 5 hours, all working hours, 27% write code, 22% meetings, 16% writing documents, 35% stealing vegetables. Wrote 3600 lines of code, there are 75 bugs, since 69. Do you think time management is a lot simpler? Write more code less stealing vegetables, less children and more pigs. Of course, this "stealing food" can be a lot of things, office politics, information addiction, bus to work ... If not this watch, I am afraid that many times I do not realize that there is so much time wasted on trifles, but also how to improve efficiency.

The record can not only serve the management, the action itself can give people power. "My day is Sungo, then I know it and do nothing." "The record is not just a pen, it is a" provincial "process when looking back at the past. Why the first two weeks of the week are 3000+ lines of code this week only 1500-? Why did the wife send 3 fires yesterday and 3 weeks before the total two times? Why...? Sensual pleasures, light and shadow, we are too easy to ignore some of the changes that happen around us, and the random records, may let us immediately alert.

But... Do I have to write down on the internet every time for a few minutes, every time the wife is angry must immediately turn a small book to write a Chennan account?

The record is too annoying, secretary to help

Of course not, all you need is a secretary. The heart of the world Xiaomi can not afford to, but the computer, mobile phone these cold plastic guys can really become your partner. I have no ability to collect a series of powerful software to tell you how many nets were stolen last month, but maybe it will inspire you to build your own computer secretary.

It's his forte to let the computer, the troublesome guy, tell you what you did with the computer. Let's start from here! But... What tools do you use? PowerShell! As we introduced last time, this new command-line tool makes it easy to get information about the system and engage it. NET objects, manage background tasks, and call advanced data analysis tools such as Excel. Data recording, processing, rendering, mining one-stop service, AM?

Observation

The whole time record of the idea is relatively simple. The computer is not afraid of trouble, can every half minutes to see what you are doing. It's good to show you the report at the end of the day.

But the question is, how does the computer know that you are on the Internet, writing a document or writing code? Well, for PowerShell, this could be easy. Remember PowerShell in the passing of a object? Each of these commands returns an object that contains rich information that we can use. As for how to know each object has what attributes, you can use get-member this function to view. For example, get-process can get all the current processes, we can use Get-process | Get-member to see what property properties The process has. Of course, because PS is the alias of Get-process, we can also use PS | Get-member to see.

Copy Code code as follows:

Name MemberType Definition
----                       ----------     ----------

... ...

Mainmodule Property System.Diagnostics.ProcessModule M ...

Mainwindowhandle Property System.IntPtr mainwindowhandle {get;}

MainWindowTitle Property System.String MainWindowTitle {get;}

Maxworkingset Property System.IntPtr Maxworkingset {get;s ...

Minworkingset Property System.IntPtr Minworkingset {get;s ...

... ...

Better... There are 90 of them. Did you notice the mainwindowtitle? This is the title of the main window of the process. Let's use PS |? {$_. MainWindowTitle} | Select MainWindowTitle to see what the title of the current system main window is:

Copy Code code as follows:

MainWindowTitle

---------------

Start Page-microsoft Visual Studio

Computing Life-Blog Park-Windows Internet Explorer

What's your c:\> capable of? (1)--use PowerShell to build time management small Secretary-Microsoft Office one ...

Untitled-message (HTML)

Windows PowerShell

Windows Task Manager

Document1-microsoft Word


Mmm, you can see a lot of things. For example, use IE to browse blog parks, write documents in Word, write code in Visual Studio, and so on. Also really thanks to the designers of the software, the name of the software is placed on the title of the window, otherwise our statistics are quite difficult. The following things are simple, we can use regular expressions to match each title, such as Internet Explorer is on the Internet, including Visual Studio instructions in writing code, and so on. As long as the time to statistics, how long a day to surf the internet, how long to write code is very easy to calculate out.

Recording

The "observation" of this step is resolved. But how to "record" it? This is also very simple, with a number of sets, every time you find Internet Explorer to the Internet +1, found Microsoft Word on the document +1. Fortunately PowerShell already thought of all this, even if not use. NET in the powerful data structure, it also has built-in hash-table this data type, is very suitable for us to complete the statistical task.

For example, we want to monitor the Internet, write code and write the document time, just write such a script:

Copy Code code as follows:

$timeInterval = #每30s监测一次
$record = @{"internet" = 0; "Programming" = 0; "Document" = 0}
$count = 0
while ($true)
{
$titles = PS |? {$_. MainWindowTitle} | Select MainWindowTitle
$titles | % {#这部分用来匹配窗口标题并进行统计, can be freely defined
if ($_-match "Internet Explorer") {$record [internet]]++}
if ($_-match "Visual Studio") {$record [Programming]++}
if ($_-match "Microsoft Word") {$record [document]++}
if ($_-match "Microsoft Office OneNote") {$record [document]++}
if ($_-match "Microsoft PowerPoint") {$record [document]++}
}
Sleep ($timeInterval) #将线程转入睡眠, waking once every 30 seconds
$count = ($count + 1)% #为了防止数据丢失, write file every 10 times
if ($count-eq 0) {$record > D:\temp\timeRecord.txt}
}

The code is very simple, the effect is to match the title of each window and statistics. The execution is also quick, in millisecond magnitude. Because it is performed once every 30 seconds, it has little impact on the system.

If the request is not high, as long as the computer on the background to run the script is good. Take a look at the document at the end of the day, and how the time is spent naturally at a glance. However, after all, the PowerShell session window is occupied, and the current statistical results are not immediately known. So here's how to get this script to execute in the background, while allowing us to see the current time utilization in time. If you are not interested in this aspect of the content can jump directly to the next section, this section does not affect the reading behind. :-)

PowerShell provides powerful background task management and is as easy to use as it is. We can save this script as a script file, such as MOTINOR.PS1, and then type Start-job {C:\USERS\GRAPEOT\MONITOR.PS1} in PowerShell to run it in the background. We can see that PowerShell returns to the command line after returning the following text, so you can continue to work on other transactions:

Copy Code code as follows:

Id Name State Hasmoredata Location

--              ----            -----      -----------     --------

5 Job5 Running True localhost

In this process, we can use the Get-content d:\temp\timeRecord.txt command to understand the time usage. can also use job management commands such as stop-job to stop the task, get-job to observe the task, receive-job to receive output and so on.

Wait a minute, no, can't we just show $record? Let's try it out. Type $record, enter, eh? How come there's no result?

This is because the variables in the scripting language of PowerShell are also scoped (scope). The $record in the script is only valid in the script, and we certainly don't see it in global scope. But can't the script write global variables? Of course. We can make a little change to the call command. C:\USERS\GRAPEOT\MONITOR.PS1, pay attention to this point and the space in front of, and here temporarily do not use start-job but direct execution. After running for a while, press CTRL + C to end the script, type $record See, the results come out ~ this. "Called Dot sourcing, we can use it to let a script or function read and write to a global variable directly." Of course, another method is to use the set-variable command plus-scope parameters to read and write. You can use the Help set-variable-parameter scope command query with the specific method.

However, these two features appear to be somewhat ineffective in the background job. If you start the script with dot sourcing, the job hangs directly, the state is running, but it's not actually running. Microsoft says this is because the fork process caused the dead lock to pound the ghost. If you use Set-variable to assign a value to a global variable, you still cannot observe the result with $record. It is speculated that a background-run session and current session are independent of each other, so there is no way to share variables. It seems that if you want to pass the data in the background task and the current session, you can only pass Receive-job or temporary files. If you have any good idea, please enlighten me ~

Presentation and excavation

At the end of the day, we also got a statistical table, such as

Copy Code code as follows:

Name Value

----                           -----

Programming 5869

Document 3217

Internet access 3078


We can use the script described in the previous article to draw it into a pie chart, and, of course, we can save the daily Records and draw a weekly state trend chart.

You can do more ...

Everyone has their own life of their own computer habits, according to their own needs to create their own tools in fact is quite a sense of achievement. And PowerShell is very suitable as a foundation for building. You can certainly do more than using computers to monitor time utilization. For example, more detailed statistics browser records, to see what websites they often go to, watch Google Reader reading rate statistics, unsubscribe from the unnecessary feed, and even use mobile phone to do some simple work, such as the pedometer software statistics how many days of movement, Use GPS software to count how long you've been on the road to work, and see who you've been calling for too long, and so on. Sometimes very simple and even very bare algorithms can bring magical discoveries.

In fact, the purpose of this article is not to show off how PowerShell is capable, but to emphasize the importance of the habit of "recording". Just use PowerShell to achieve relatively simple. For the pursuit of perfection or do not like the command line, of course, you can also write a system service, the effect is the same.

So, data is value. Let us use DIY spirit to explore the life and we pass the value of it!

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.