{"id":678,"date":"2014-09-15T00:01:00","date_gmt":"2014-09-15T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/09\/15\/use-powershell-to-start-a-process-at-random-times\/"},"modified":"2014-09-15T00:01:00","modified_gmt":"2014-09-15T00:01:00","slug":"use-powershell-to-start-a-process-at-random-times","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-start-a-process-at-random-times\/","title":{"rendered":"Use PowerShell to Start a Process at Random Times"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to start a process at random intervals.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;Hey, Scripting Guy! I have a rather unusual request. I am testing a new monitoring application, and I need a script that will start processes at random intervals so I can check the effectiveness of the solution. Is this something you can help me with?<\/p>\n<p>&mdash;BB<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello BB,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. This morning I am sitting on the porch, sipping a nice cup of green tea with orange flavorings in it. The tea is called Orange Cream, and it tastes exactly like the old Dreamsicles I used to eat when I was a kid. It is a different way to start the morning. I am cooling off after spending an hour in the gym. I have my Surface Pro&nbsp;3 that the Scripting Wife got for me for my birthday (it was an early birthday present&hellip;I have had it for awhile now), and I am catching up with email sent to <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a> and Facebook. I don&rsquo;t know about you, but lately, it seems like I am always multitasking. I find it hard to sit and do just one thing at a time anymore. I&rsquo;m not sure if this is a good thing or a bad thing.<\/p>\n<h2>Picking out tools<\/h2>\n<p>BB, there are many ways to write a Windows PowerShell script that will generate new processes at random intervals. But when I hear this requirement, I think that I will need the following:<\/p>\n<ul>\n<li><b>Get-Random&nbsp;&nbsp;<\/b>To generate random numbers. I also know that I will need to specify minimum and maximum values. I will accept the default seed.<\/li>\n<li><b>Start-Process<\/b> &nbsp;&nbsp;To start the processes and give me a bit of control over the way they start. For testing purposes, I probably want to use a minimized window style so that it does not block other things (such as the Windows PowerShell ISE for example), and so I will still be able to see that the processes have in fact started. Obviously, I will need a way to specify the program name (and path if needed). I can do this with a variable.<\/li>\n<li><b>Start-Sleep<\/b>&nbsp;&nbsp;To control the amount of time between the startup of the process. I will use the <b>&ndash;seconds<\/b> parameter and use the number I get back from <b>Get-Random<\/b> to control the sleeping. This also means that I will need to use numbers large enough to be minutes instead of seconds (for example, 300 for five minutes).<\/li>\n<li><b>While<\/b> <b>statement<\/b>&nbsp;&nbsp;To put the script in an infinite loop. I am only testing the script at this point, so this will work well because I am sitting here monitoring it. Later I may want to increase my minimum and maximum random values, and run the script over a twelve hour period of time. For this, I will use the <b>For<\/b> statement and control exactly how many times I want the script to execute.<\/li>\n<\/ul>\n<h2>Writing the script<\/h2>\n<p>Because I did a good job analysis, and I know pretty much all of the commands I am going to use in the script, writing the script does not take me very long at all. In fact, here is the script I wrote:<\/p>\n<p><b>&nbsp; &nbsp;Note&nbsp;<\/b> Using <b>While ($true)<\/b> puts the script into an infinite loop. To stop the loop, press the red square (stop button) in the Windows PowerShell ISE.<\/p>\n<p style=\"margin-left:30px\"># StartProcessesAtRandomTimes.ps1<\/p>\n<p style=\"margin-left:30px\">$prog = &quot;notepad.exe&quot;<\/p>\n<p style=\"margin-left:30px\">while ($true)<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;$rnd = Get-Random -Minimum 1 -Maximum 5<\/p>\n<p style=\"margin-left:30px\">&nbsp;Start-Sleep -Seconds $rnd<\/p>\n<p style=\"margin-left:30px\">&nbsp;start-process -FilePath $prog -WindowStyle Minimized}<\/p>\n<h2>Testing the script<\/h2>\n<p>To test the script, I load it in the Windows PowerShell ISE, press the green triangle, and then wait for a while. Nothing appears, and nothing appears to happen. But, at least on my laptop, I see the screen flash every once in a while, and on my task bar, I see several Notepads stacked up. So I press the red square, and stop the script. Still nothing appears to have happened&mdash;at least not from looking at the Windows PowerShell ISE. Here is the screenshot:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-15-14-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-15-14-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>In the interactive pane, I use <b>Get-Process<\/b> to find all the Notepad processes. As shown here, there were quite a few generated:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-15-14-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-15-14-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>That is cool. Now I want to see if my randomizer is working&mdash;that is, at what time were the Notepad processes generated. That is easy enough. I use the <b>StartTime<\/b> property. It is probably best to also sort them. So I use the following script:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Get-Process notepad | Select StartTime | sort&nbsp; -Descending<\/p>\n<p>The problem is that it does not sort properly. It looks like it is sorted, but I try the ascending and the descending sorts, and I see the order does not change. Dude.<\/p>\n<p>I then realize that I need to expand the <b>DateTime<\/b> object (that is contained in the <b>StartTime<\/b> property) so I can specify WHAT I want to sort on. Obviously, I am interested in the <b>TimeOfDay<\/b>. So I revise my command to the following:<\/p>\n<p style=\"margin-left:30px\">Get-Process notepad | select -expand StartTime | sort timeofday&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p>Now I try it, and I see that it is working. In fact, I am getting random offsets between the process starts. This is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-15-14-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-15-14-03.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>BB, that is all there is to using Windows PowerShell to create processes at random intervals. Process Week will continue tomorrow when I will talk about launching random processes at random times.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to start a process at random intervals. &nbsp;Hey, Scripting Guy! I have a rather unusual request. I am testing a new monitoring application, and I need a script that will start processes at random intervals so I can check the effectiveness of the solution. [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[41,31,60,87,3,4,45],"class_list":["post-678","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-monitoring","tag-operating-system","tag-performance","tag-processes","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to start a process at random intervals. &nbsp;Hey, Scripting Guy! I have a rather unusual request. I am testing a new monitoring application, and I need a script that will start processes at random intervals so I can check the effectiveness of the solution. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/678","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=678"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/678\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}