{"id":672,"date":"2014-09-16T00:01:00","date_gmt":"2014-09-16T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/09\/16\/use-powershell-to-start-random-processes-at-random-times\/"},"modified":"2014-09-16T00:01:00","modified_gmt":"2014-09-16T00:01:00","slug":"use-powershell-to-start-random-processes-at-random-times","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-start-random-processes-at-random-times\/","title":{"rendered":"Use PowerShell to Start Random Processes at Random Times"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to create random processes at random times.\n<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 need to start a process that I pick randomly from a list. I would like these processes to start at random times so I can test my monitoring application. Is this something Windows PowerShell can do?\n&mdash;JD\n<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 JD,\nMicrosoft Scripting Guy, Ed Wilson, is here. This morning, I am sipping a cup of Darjeeling tea that I made with spearmint, licorice root, and a cinnamon stick. The taste is understated, and quite nice. It goes well with the Irish steel-cut oats and blueberries that I am having for breakfast. I am sitting in the kitchen this morning (it is raining outside) and eating at the counter. I have my Surface Pro&nbsp;3 with me, and I am checking email sent to <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>.<\/p>\n<h2>Create random processes the easy way<\/h2>\n<p>If you really want the path to an executable that is random, the easy way to do it is to use the <b>Get-Command<\/b> cmdlet, and search for <b>*.exe<\/b>. This will return a <b>CommandInfo<\/b> object that has a <b>Path<\/b> property. I would then feed that to the <b>Get-Random<\/b> cmdlet as an <b>InputObject<\/b>. Here is a command that would work for this:<\/p>\n<p style=\"margin-left:30px\">Get-Random -InputObject (Get-Command *.exe) | Select path\nThe output displays the complete to the executable. Here is an example:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; Get-Random -InputObject (Get-Command *.exe) | Select path<\/p>\n<p style=\"margin-left:30px\">Path<\/p>\n<p style=\"margin-left:30px\">&#8212;-<\/p>\n<p style=\"margin-left:30px\">C:WINDOWSSYSTEM32gpresult.exe<\/p>\n<h2>A safer approach<\/h2>\n<p>The issue with the previous approach of gathering really random executables is that if I later decide I want to launch the executables, I could really get some dangerous and unpredictable results. A safer approach is to create an array of executable names, use <b>Get-Command<\/b> to find the path, and send that to <b>Start-Process<\/b>. In this way, I can ensure that I am only starting processes that I want to start. Not really random processes, but launching known processes in a random order.\nBecause I need to create an array of strings, it would normally mean using a lot of quotation marks and commas, and extra typing. I then assign it to a variable to hold the array. But I was recently watching <a href=\"http:\/\/www.toddklindt.com\/netcast\/default.aspx\" target=\"_blank\">Todd Klindt&rsquo;s Netcast,<\/a> and he used a trick that I had forgotten about. I have added it back to my repertoire. The secret is to create a string that contains commas, and then use the <b>Split<\/b><i> <\/i>method to create the array. It is simple and fast. Here is an example of what I am talking about:\nFirst the old way:<\/p>\n<p style=\"margin-left:30px\">$a = &#8220;mspaint.exe&#8221;,&#8221;write.exe&#8221;,&#8221;calc.exe&#8221;,&#8221;notepad.exe&#8221;\nNow for the new way:<\/p>\n<p style=\"margin-left:30px\">$a = &#8220;mspaint.exe,write.exe,calc.exe,notepad.exe&#8221;.Split(&#8216;,&#8217;)\nI like doing this because although it is a little bit more typing (only a couple extra key strokes), it is easier to type. I don&rsquo;t like having to hold down the Shift key and then pressing the quotation mark. (Actually, if I include Shift in my keystroke count, it is a real savings in typing.)<\/p>\n<h2>Getting the random process names from the array<\/h2>\n<p>It is possible to index into the array to get the process names. To do this, I might use a command such as the following:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; $a<\/p>\n<p style=\"margin-left:30px\">mspaint.exe<\/p>\n<p style=\"margin-left:30px\">write.exe<\/p>\n<p style=\"margin-left:30px\">calc.exe<\/p>\n<p style=\"margin-left:30px\">notepad.exe<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; $r = Get-Random -Maximum 3 -Minimum 0<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; $a.Item($r)<\/p>\n<p style=\"margin-left:30px\">mspaint.exe\nBut that is cumbersome, and it uses extra lines of code and extra variables. In addition, if I later add additional executable names to the array, I have to update the <b>Get-Random<\/b> portion (an easy thing to forget). A better way to do this is to pass the array of executable names directly to <b>Get-Random<\/b> and let it handle the details. Here is the command:<\/p>\n<p style=\"margin-left:30px\">Get-Random -InputObject $a\nTo make sure I have the path to the executable, I call the <b>Get-Command<\/b> cmdlet and select the path.\n<b>&nbsp; &nbsp;Note<\/b>&nbsp; I could simply type the complete path to the executable in my array, but that is a lot of extra typing, and it is <br \/>&nbsp; &nbsp;error prone. Using <b>Get-Command<\/b>&nbsp;is much simpler.\nHere is the command I use to get the path to my randomly selected executable:<\/p>\n<p style=\"margin-left:30px\">Get-Random -InputObject $a | Get-Command | select path\nUnfortunately, the <b>Start-Process<\/b> cmdlet does not accept pipelined input. So I need to use a Foreach<b>-Object<\/b> cmdlet prior to calling the <b>Start-Process<\/b> cmdlet. So I use <b>Get-Random<\/b> to select random executable names, get the path by using <b>Get-Command<\/b>, and pass it to <b>Start-Process<\/b>. Here is the command:<\/p>\n<p style=\"margin-left:30px\">Get-Random -InputObject $a | Get-Command | Foreach-Object {Start-Process $_.path}<\/p>\n<h2>Groovy&hellip;now put it with yesterday&rsquo;s script<\/h2>\n<p>I am now ready to put my new command into the script I wrote yesterday in <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2014\/09\/15\/use-powershell-to-start-a-process-at-random-times.aspx\" target=\"_blank\">Use PowerShell to Start a Process at Random Times<\/a>. &nbsp;\nAfter I do that, I will launch a randomly selected process from an array of executable names, and I will do so at random intervals.\nHere is the complete script:<\/p>\n<p style=\"margin-left:30px\">$prog = &#8220;mspaint.exe,write.exe,calc.exe,notepad.exe&#8221;.Split(&#8216;,&#8217;)<\/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;Get-Random -InputObject $prog |<\/p>\n<p style=\"margin-left:30px\">&nbsp;Get-Command |<\/p>\n<p style=\"margin-left:30px\">&nbsp;ForEach-Object {<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; start-process -FilePath $_.path -WindowStyle Minimized } }\n<b>&nbsp; &nbsp;Note<\/b>&nbsp; Calculator does not start if it is minimized, but the other three processes will. So be aware of this prior to <br \/>&nbsp; &nbsp;running the script.\nJD, that is all there is to using Windows PowerShell to create random processes at random times. Process Week will continue tomorrow when I will talk about more cool stuff.\nI 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=\"http:\/\/blogs.technet.commailto: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.\n<b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to create random processes at random times. &nbsp;Hey, Scripting Guy! I need to start a process that I pick randomly from a list. I would like these processes to start at random times so I can test my monitoring application. Is this something Windows [&hellip;]<\/p>\n","protected":false},"author":595,"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-672","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 create random processes at random times. &nbsp;Hey, Scripting Guy! I need to start a process that I pick randomly from a list. I would like these processes to start at random times so I can test my monitoring application. Is this something Windows [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/672","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=672"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/672\/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=672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}