{"id":17491,"date":"2010-08-08T00:01:00","date_gmt":"2010-08-08T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/08\/08\/weekend-scripter-automatically-collecting-process-snapshots\/"},"modified":"2010-08-08T00:01:00","modified_gmt":"2010-08-08T00:01:00","slug":"weekend-scripter-automatically-collecting-process-snapshots","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-automatically-collecting-process-snapshots\/","title":{"rendered":"Weekend Scripter: Automatically Collecting Process Snapshots"},"content":{"rendered":"<p>&nbsp;\nMicrosoft Scripting Guy Ed Wilson here. It may seem like a &ldquo;well duh&rdquo; thing for a Scripting Guy to say, but I love writing scripts. In particular, I love writing <a href=\"http:\/\/technet.microsoft.com\/en-us\/scriptcenter\/powershell.aspx\">Windows PowerShell<\/a> scripts. One problem with sharing everything I write is that people always have a better idea about how to do things. But better can be relative. In my mind, shorter is not always better, especially for scripts. If I am working interactively at the Windows PowerShell console&mdash;oh wait, that is next week&rsquo;s article.\nAnyway, after I wrote <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/08\/07\/weekend-scripter-boot-tracing-with-windows-powershell.aspx\">yesterday&rsquo;s Weekend Scripter article<\/a>, I decided to write a script that would automate collecting four process snapshots on a computer after it had rebooted and logged on. I was a little carried away and spent all day on the script. It is actually a fun script to play around with, and the information that it returns is simply fascinating.\nThe first function I create is the <b>Get-ProcessStartUp<\/b> function. After defining the parameters for the function, I create the path that will be used for saving the process information. To create the path, I use the format specifier to substitute values in a string. The <b>$path<\/b> variable goes in the first position and is followed by a backslash and the word <i>Process<\/i>. The <b>$pass<\/b> variable value goes into the second position followed by an underscore. Lastly, the computer name (contained in the <b>$computer<\/b> variable goes into the last position, followed by a period and the letters <i>xml<\/i>. In this manner, the path to the file that will contain the process information (in xml format obviously) is created. (I could have used the <b>Join-Path<\/b> cmdlet to create the path to the file, but because I wanted to use a combination of three different values from three different variables, I decided this approach was best). This line of code is shown here:<\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #2b91af\">$ppath<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #808080\">&#8220;{0}Process{1}_{2}.xml&#8221; <\/span><span style=\"color: #000000\">-f<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$path<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #2b91af\">$pass<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #2b91af\">$computer<\/span><span style=\"color: #000000\">)<\/span>\nAfter the path has been created, I check to see if there is an old file in the location. If an older file exists, I delete it and write an entry into a custom event log by calling the <b>Add-EventLogEntry<\/b> function. This section of the <b>Get-ProcessStartUp<\/b> function is shown here:<\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000ff\">Test-Path<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Path<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$ppath<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">Remove-Item<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Path<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$ppath<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Add-EventLogEntry<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-source<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">gpst<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-eventType<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">information<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-eventID<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">4<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">`<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">-message<\/span><span style=\"color: #808080\"> &#8220;$ppath exists and is being removed&#8221; <\/span><span style=\"color: #000000\">-logName<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">ForScripting<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">}<\/span>\nI now use the <b>Get-Process<\/b> cmdlet to retrieve process information from the computer. The <b>System.Diagnostics.Process<\/b> .NET Framework object is passed to the <b>Export-CliXML<\/b> cmdlet with the path we created earlier. The XML file will be used later to &ldquo;reconstitute&rdquo; the <b>Process<\/b> object. This behavior is just like saving the result of running the <b>Get-Process<\/b> cmdlet into a variable so that it can be worked with later. The difference is we are saving the process object into an XML file for later use. After the XML file is reconstituted, I can work with it in exactly the same way as if I had stored it in a variable. In this way, they behave like &ldquo;<a href=\"http:\/\/en.wikipedia.org\/wiki\/Freeze_drying\">freeze-dried<\/a> objects.&rdquo; I pause the script for 60 seconds between passes. If the script has completed the fourth pass, there is no need to pause the script. This portion of the <b>Get-ProcessStartUp<\/b> function is seen here:<\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #0000ff\">Get-Process<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-ComputerName<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$computer<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #0000ff\">Export-Clixml<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Path<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$ppath<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #2b91af\">$pass<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">-ne<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">4<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #0000ff\">Start-Sleep<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Seconds<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">60<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-ProcessStartUp<\/span>\nThe <b>Add-EventLogEntry<\/b> function is used to create a new EventLog and define a new EventLog source. In reality, I guess I should check to see if the EventLog exists. I should also check to see if the EventLog source exists. If these things exist, I should go ahead and use the EventLog and the EventLog source instead of trying to create new ones. If you are interested in this technique, I have an entire chapter in the Microsoft Press <a href=\"http:\/\/www.amazon.com\/gp\/product\/0735622795?ie=UTF8&amp;tag=technet-sc-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0735622795\">Windows PowerShell Scripting Guide<\/a>.\nBecause it is the weekend, I decided to use a little <a href=\"http:\/\/blogs.technet.comhttps:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-march-11-2010\/\">structured error handling<\/a>, specify the <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/03\/09\/hey-scripting-guy-march-9-2010.aspx\">error action preference<\/a> on the <b>New-EventLog<\/b> cmdlet to <b>silentlycontinue<\/b>, and catch the errors that arise. After the new EventLog and EventLog source has been created, the <b>finally<\/b> block writes the entry to the new EventLog. The <b>Add-EventLogEntry<\/b> function is seen here:<\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Add-EventLogEntry<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">param(<\/span><span style=\"color: #2b91af\">$source<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$eventType<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$eventID<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$message<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$logName<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">try<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">New-EventLog<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-source<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$source<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-logname<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$logName<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-EA<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">silentlyContinue<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Catch{<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">[System.Exception]<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Finally<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">Write-EventLog<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-LogName<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$logName<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Source<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$source<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">`<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">-EntryType<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$eventType<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">`<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">-EventId<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$eventID<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">`<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">-Message<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$message<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">catch<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">add-eventlogEntry<\/span>\nThe <b>ForScripting<\/b> event log that is created is shown in the following image.\n<img decoding=\"async\" height=\"417\" width=\"600\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/august\/hey0808\/wes-08-08-10-01.jpg\" alt=\"Image of ForScripting event log\">\nTo create a new EventLog or to retrieve process information from certain protected system processes requires that the script runs with administrator rights. This is one of the things I kept forgetting to do when I was writing the Get-ProcessStartUpTimes.ps1 script. I decided to copy the <strong>Test-IsAdministrator<\/strong> function from my <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2009\/11\/04\/hey-scripting-guy-november-4-2009.aspx\">MonitorDiskFormatDrive.ps1<\/a> script that I wrote about in <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2009\/11\/04\/hey-scripting-guy-november-4-2009.aspx\">Hey, Scripting Guy! Can I Format a Portable Drive When It Is Inserted Into a Computer?<\/a> (a way cool article by the way). Anyway, the whole idea of the <strong>Test-IsAdministrator<\/strong> function is to tell you if you are running with admin rights. If you are, it returns True; otherwise, it returns false. The complete function is shown here. If you want to know more about it, refer to the previously mentioned Hey, Scripting Guy! Blog post. <\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #000000\">function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Test-IsAdministrator<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">&lt;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">.Synopsis<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Tests<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">the<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">user<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">is<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">an<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">administrator<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">.Description<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Returns<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">true<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">a<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">user<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">is<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">an<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">administrator,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">false<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">the<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">user<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">is<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">not<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">an<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">administrator<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">.Example<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Test-IsAdministrator<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">.Notes<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">NAME:<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Test-IsAdministrator<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">AUTHOR:<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Ed<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Wilson<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">LASTEDIT:<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">5<\/span><span style=\"color: #000000\">\/<\/span><span style=\"color: #800000\">20<\/span><span style=\"color: #000000\">\/<\/span><span style=\"color: #800000\">2009<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">KEYWORDS:<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">.Link<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Http:\/\/www.ScriptingGuys.com<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">Requires<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Version<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">2<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #800000\">0<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">&gt;<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">param()<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #2b91af\">$currentUser<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">[Security.Principal.WindowsIdentity]::GetCurrent()<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000ff\">New-Object<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Security.Principal.WindowsPrincipal<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$currentUser<\/span><span style=\"color: #000000\">).IsInRole(`<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">[Security.Principal.WindowsBuiltinRole]::Administrator)<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Test-IsAdministrator<\/span>\nThe entry point to the script first calls the <b>Test-IsAdministrator<\/b> function. If the function returns true, the script calls the <b>Add-EventLogEntry<\/b> function and writes an event that the script is starting. This is shown here:<\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #0000ff\">If<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000ff\">-not<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">(Test-IsAdministrator))<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\"> &#8220;Admin rights are required for this script&#8221; <\/span><span style=\"color: #0000ff\">;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">exit<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Add-EventLogEntry<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-source<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">gpst<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-eventType<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">information<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-eventID<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">`<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">-message<\/span><span style=\"color: #808080\"> &#8220;beginning $($MyInvocation.InvocationName) at $(get-date)&#8221; <\/span><span style=\"color: #000000\">`<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">-logName<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">ForScripting<\/span>\nThe script then makes four passes. On each pass, it writes a new EventLog entry with the pass number and current time. Next, it calls the <b>Get-ProcessStartup<\/b> function and passes the path, pass number, and computer name. The pass number comes from the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/winpsh\/manual\/pipe.mspx\">pipeline<\/a>. The path is a hard-coded UNC path on my network. You should modify it to fit your needs. Because the script is running locally, I pick up the computer name from the environment variables. When the pass is completed, another EventLog entry is added that states the script completed and adds the date to the entry. By the way, eventID 1 is starting the script. EventID 2 is starting the pass, and eventID 3 is completing the script. You can make up your own event ID numbers if you do not like mine. Here is this portion of the script:<\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #800000\">1<\/span><span style=\"color: #000000\">..<\/span><span style=\"color: #800000\">4<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">ForEach-Object<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Add-EventLogEntry<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-source<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">gpst<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-eventType<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">information<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-eventID<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">2<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">`<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">-message<\/span><span style=\"color: #808080\"> &#8220;Starting pass $_ at $(get-date)&#8221; <\/span><span style=\"color: #000000\">-logName<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">ForScripting<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Get-ProcessStartup<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-path<\/span><span style=\"color: #808080\"> &#8220;\\hyperv-boxshared&#8221; <\/span><span style=\"color: #000000\">-pass<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$_<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-computer<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #2b91af\">$env<\/span><span style=\"color: #000000\">:COMPUTERNAME)<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">foreach-object<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Add-EventLogEntry<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-source<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">gpst<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-eventType<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">information<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-eventID<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">3<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">`<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">-message<\/span><span style=\"color: #808080\"> &#8220;Completed $($MyInvocation.InvocationName) at $(get-date)&#8221; <\/span><span style=\"color: #000000\">`<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">-logName<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">ForScripting<\/span>\nThe complete Get-ProcessStartUpTimes.ps1 script is shown here. <\/p>\n<p style=\"padding-left: 30px\"><strong>Get-ProcessStartUpTimes.ps1<\/strong><\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-ProcessStartUp<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Param(<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #2b91af\">$path<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #2b91af\">$pass<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #2b91af\">$computer<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #2b91af\">$ppath<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #808080\">&#8220;{0}Process{1}_{2}.xml&#8221; <\/span><span style=\"color: #000000\">-f<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$path<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #2b91af\">$pass<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #2b91af\">$computer<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000ff\">Test-Path<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Path<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$ppath<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">Remove-Item<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Path<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$ppath<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Add-EventLogEntry<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-source<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">gpst<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-eventType<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">information<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-eventID<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">4<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">`<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">-message<\/span><span style=\"color: #808080\"> &#8220;$ppath exists and is being removed&#8221; <\/span><span style=\"color: #000000\">-logName<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">ForScripting<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">Get-Process<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-ComputerName<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$computer<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #0000ff\">Export-Clixml<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Path<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$ppath<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #2b91af\">$pass<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">-ne<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">4<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #0000ff\">Start-Sleep<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Seconds<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">60<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-ProcessStartUp<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Add-EventLogEntry<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">param(<\/span><span style=\"color: #2b91af\">$source<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$eventType<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$eventID<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$message<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$logName<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Microsoft Scripting Guy Ed Wilson here. It may seem like a &ldquo;well duh&rdquo; thing for a Scripting Guy to say, but I love writing scripts. In particular, I love writing Windows PowerShell scripts. One problem with sharing everything I write is that people always have a better idea about how to do things. But [&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":[31,87,3,61,45],"class_list":["post-17491","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-operating-system","tag-processes","tag-scripting-guy","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; Microsoft Scripting Guy Ed Wilson here. It may seem like a &ldquo;well duh&rdquo; thing for a Scripting Guy to say, but I love writing scripts. In particular, I love writing Windows PowerShell scripts. One problem with sharing everything I write is that people always have a better idea about how to do things. But [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17491","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=17491"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17491\/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=17491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=17491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=17491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}