What can you do with C: \>? (1)-Build a Time Management Secretary using PowerShell

Source: Internet
Author: User
Introduction

    Do you think they are still alive? Why can someone else do so much?

    Do you often get down in the class eight hours, but you don't know what you did?

    Is it busy to switch between Email, BBS, and Google Reader?

    You needTime management!

    Do you know how much code you have written in a day?

    Do you know how much time you spend wandering online in a week?

    Can you tell me that you have been with your family for several days in a month?

    You needPowerShell!

     

Management? Record first!

    Time management is to do the most with the least time. Like the performance of the optimization program, to improve the utilization of time, you must first know where your time is spent. If 50% of the work time is wasted on trivial matters, the remaining half of the time will be concentrated, and the effect of the day will not be good. Correspondingly, if 90% of the time is working, it is better to slightly improve efficiency than trying to reduce the remaining time. Archery emphasizes targeted treatment and remedies. KnowledgeBottleneckThe premise is optimization.

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

     

     

    Imagine that at the end of the week, you will see a statistical table: working overtime for five hours this week, 27% writing code, 22% meeting, 16% writing documents, and 35% stealing food during all working hours. I wrote 3600 lines of code and encountered 75 bugs. I found 69 bugs. Does it make time management much easier? Write more code, steal less food, and have fewer children and raise more pigs. Of course, this "food stealing" can be a lot of things, office politics, information addiction, take a bus to and from work... without this table, I am afraid I may not realize that I am wasting so much time on trivial matters. How can I improve my efficiency.

    Records can not only serve management, but also give motivation to people. "I am in the three provinces of Japan, then I know what I can do ." Record is not just a matter of action. When we review what we did in the past, it is also a process of "saving. Why is there only 3000 lines of code each week for the first two weeks -? Why did my wife get angry three times yesterday and only two times in the past three weeks? Why ...? We are so easy to ignore the changes that happen around us, and the records we keep us awake.

    But... do I need to remember every time I access the Internet for a few minutes? Every time my wife gets angry, I need to turn over a small book and write down a rotten sesame account?

     

The record is too annoying. The secretary will help you.

    Of course not. All you need is a secretary. This fascinating world is nothing more than computers and mobile phones. I am not able to find a series of amazing software to tell you how many dishes you stole last month, but it may inspire you to build your own computer secretary.

    It's a good thing to ask the computer guy who is not afraid of trouble to tell you what's wrong with your electric brain stem. Let's start from here! But what tools are used? PowerShell! As we introduced last time, this new command line tool can easily obtain system information, join. NET objects, manage background tasks, and call advanced data analysis tools such as Excel. Which of the following services can be used to record, process, present, and mine data?

     

Observation

    The idea of recording the entire time is relatively simple. The computer is not in trouble. You can check what you are doing every half a minute. Just show the report to you at the end of the day.

    But the question is, how does the computer know whether you are surfing the Internet, writing documents or writing code? Ha, this is simple for PowerShell. Remember what objects are passed in PowerShell? Each command returns an object that contains rich information, which can be exploited. You can use the get-member function to view the attributes of each object. For example, Get-process can get all the current processes. We can use get-process | Get-member to view the attributes of the process. Of course, since ps is the alias of get-process, we can also use ps | get-member to view it.

    Name MemberType Definition

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

    ......

    MainModule Property System. Diagnostics. ProcessModule M...

    Main1_whandle Property System. IntPtr main1_whandle {get ;}

    MainWindowTitle Property System. String MainWindowTitle {get ;}

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

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

    ......

    A lot... there are 90 in total. Have you noticed MainWindowTitle? This is the title of the Main Window of the process. Let's use ps |? {$ _. MainWindowTitle} | select MainWindowTitle to see the title of the current system main window:

     

    MainWindowTitle

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

    Start Page-Microsoft Visual Studio

    Computing Life-blog garden-Windows Internet Explorer

    What can you do with C: \>? (1) -- use PowerShell to create a Time Management Secretary-Microsoft Office One...

    Untitled-Message (HTML)

    Windows PowerShell

    Windows Task Manager

    Document1-Microsoft Word

    Grace, you can see a lot of things. For example, you can use IE to browse the blog Park, use Word to write documents, and use Visual Studio to write code. I really appreciate the software designers who put all the software names on the title of the window. Otherwise, our statistics are quite difficult. The following is simple. We can use regular expressions to match each title. For example, Internet Explorer is included, and Visual Studio instructions are included in the Code. You only need to make statistics on how long the Internet accesses will take and how long it takes to write code.

     

Record

    So far, "observing" is a solution. But how to "record? This is also quite simple. You can use an array to get it done. If you find Internet Explorer, you will get Internet access + 1. If you find Microsoft Word, you will get the document + 1. Fortunately, PowerShell has long thought of this. Even if it does not use the powerful data structure in. NET, it also has the built-in Hash-Table data type, which is very suitable for us to complete the statistical task.

    For example, if you want to monitor the Internet access, write code, and write documents, you only need to write such a script:

      $ TimeInterval = 30 # monitoring every 30 s
    $ Record = @ {"Surfing" = 0; "programming" = 0; "document" = 0}
    $ Count = 0
    While ($ true)
    {
    $ Titles = ps |? {$ _. MainWindowTitle} | select MainWindowTitle
    $ Titles | % {# This part is used to match the window title and make statistics. It can be freely defined.
    If ($ _-match "Internet Explorer") {$ record [""] ++}
    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) # transfers the thread to sleep and wakes up every 30 seconds
    $ Count = ($ count + 1) % 10 # To prevent data loss, the file is written every 10 times
    If ($ count-eq 0) {$ record> d: \ temp \ timeRecord.txt}
    }

     

     

     

    The code is very simple. The general idea is to match and count the titles of each window. The execution is also very fast, in milliseconds. Execution is performed every 30 seconds, which has almost no impact on the system.

    If the requirements are not high, you only need to run the script in the background when you are on the computer. When I get off work every day, I will go and check the recording documents. It is easy to see how much time is spent. However, this will occupy the PowerShell session window, and the current statistical results cannot be obtained in real time. So the following will try to demonstrate how to make this script run in the background, while allowing us to see the current time utilization in a timely manner. If you are not interested in this content, you can directly skip to the next section, which will not affect subsequent reading. :-)

    PowerShell provides powerful background task management, which is just as easy to use. We can save this script as a script file, such as Motinor. ps1, and then type start-job {c: \ users \ grapeot \ Monitor in PowerShell. ps1} can run it in the background. We can see that PowerShell returns the following text and returns to the command line, so we can continue to process other transactions:

    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. You can also use job management commands such as stop-job to stop tasks, get-job to observe tasks, and receive-job to receive output.

    Wait. No, can't we directly display $ record? Let's give it a try. Type $ record, press enter, success? Why is there no result?

    This is because the variables in the scripting language PowerShell also have scope. $ Record in the script is only valid in the script. We certainly cannot see it within the global scope. But can't the script write global variables? Of course. We can make a small change to the call command. c: \ users \ grapeot \ Monitor. ps1. Pay attention to the preceding vertex and space, and execute directly without start-job. After a while, press Ctrl + C to end the script, and type $ record. The result is displayed ~ This "." is called dot sourcing, which enables scripts or functions to directly read and write global variables. Of course, another method is to use the set-variable command and the-scope parameter to read and write data. You can use the help set-variable-parameter scope command to query specific usage.

    However, these two features seem a little ineffective in the background job. If you use the dot sourcing STARTUP script, the job will be suspended directly. The job is Running but not Running. Microsoft says this is a zombie of deadlocks caused by the cross-fork process. If you use set-variable to assign values to global variables, you still cannot use $ record to observe the results. It is estimated that the sessions running in the background and the current sessions are independent of each other, so there is no way to share variables. It seems that only Receive-Job or temporary files can be used to transmit data between the background task and the current session. If you have any tips, please kindly advise ~

     

Presentation and mining

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

    Name Value

    ---------

    Programming 5869

    Document 3217

    Internet access 3078

    We can use the script introduced in the previous article to draw a pie chart. Of course, we can also save the daily records and draw a weekly trend chart.

     

    In addition to providing the decision-making basis for time management, PowerShell can also do more meaningful work, such as simple data mining and warning. When the Internet access time exceeds 50% in the last two hours, a warning is automatically prompted. When the user stays on Google Reader for too long, he is forcibly disabled, when a new window appears this week and the window takes more than 20% times, the system will automatically remind you... there is still a lot of record and mining space for us to explore, and PowerShell can be the most considerate Secretary in our creation.

     

You can do more...

    Everyone has their own habits of living their own computers. creating their own tools based on their own needs is actually quite a sense of accomplishment. PowerShell is suitable for building the foundation. In addition to using computers to monitor time utilization, you can certainly do more. For example, you can take a closer look at browser records to see which websites you often visit. You can view Google Reader's reading rate statistics and unsubscribe unnecessary feeds. You can even use your mobile phone to do some simple work, for example, you can use the pedometer to measure how much exercise you have in a day, use the GPS software to measure how long you have been delayed on your way to work, and check whether the call time is too long. Sometimes very simple or even very bare algorithms can bring magical discoveries.

    In fact, the purpose of this article is not to show off how PowerShell works, but to emphasize the importance of the habit of "Recording. It is relatively simple to use PowerShell. For those who pursue perfection or do not like command line, you can also write a system service on your own, with the same effect.

    Therefore, data is value. Let's use the DIY spirit to discover the value that has passed us through our lives!

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.