Showing posts with label System.Diagnostics.Process. Show all posts
Showing posts with label System.Diagnostics.Process. Show all posts

Wednesday, 19 May 2010

Start-Process2.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script starts up a process using System.Diagnostics.Process  
  4. .DESCRIPTION 
  5.     This script creates 7 occurences of IExplore.exe, using 
  6.     diffrent start up options, based on a sample script in MSDN. 
  7. .NOTES 
  8.     File Name  : Start-process2.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell Version 2.0 
  11. .LINK 
  12.     This script posted to: 
  13.         http://pshscripts.blogspot.com/2010/05/start-process2ps1.html
  14.     MSDN Sample posted at: 
  15.         http://msdn.microsoft.com/en-us/library/0w4h05yb.aspx  
  16. .EXAMPLE 
  17.     PSH [C:\foo]: .\Start-Process2.ps1' 
  18.     Starting IE at home page (p1) 
  19.     Opening Favourites (p2) 
  20.     Opening PSHScripts (p3) 
  21.     Opening c:\foo\myfile.htm (p4) 
  22.     Opening c:\foo\myfile.asp (p5) 
  23.     Opening with Start info (p6) 
  24.     Opening with startinfo + www.reskit.net (p7) 
  25. #> 
  26.  
  27. # Helper Functions 
  28.  
  29. # Opens the Internet Explorer application. 
  30. function OpenApplication { 
  31. param([string] $FavoritesPath) 
  32.  
  33. # Start Internet Explorer. Defaults to the home page. 
  34. "Starting IE at home page (p1)" 
  35. $proc1 = [System.Diagnostics.Process]::start("IExplore.exe"); 
  36. # Display the contents of the favorites folder in the browser. 
  37. "Opening Favourites (p2)" 
  38. $proc2=[System.Diagnostics.Process]::Start($FavoritesPath); 
  39. } 
  40.  
  41. # Opens urls and .html documents using Internet Explorer. 
  42. function OpenWithArguments {  
  43. # url's are not considered documents. They can only be opened 
  44. # by passing them as arguments. 
  45. "Opening PSHScripts (p3)" 
  46. $proc3 = [System.Diagnostics.Process]::Start("IExplore.exe", "www.pshscripts.blogspot.com"
  47.  
  48. # Start a Web page using a browser associated with .html and .asp files. 
  49. "Opening c:\foo\myfile.htm (p4)" 
  50. $proc4 = [System.Diagnostics.Process]::Start("IExplore.exe", "C:\foo\myFile.htm"
  51. "Opening c:\foo\myfile.asp (p5)" 
  52. $proc5 = [System.Diagnostics.Process]::Start("IExplore.exe", "C:\foo\myFile.asp"
  53.  
  54. # Uses the ProcessStartInfo class to start new processes, 
  55. # both in a minimized mode. 
  56. function OpenWithStartInfo { 
  57. $StartInfo = New-Object System.Diagnostics.ProcessStartInfo "IExplore.exe" 
  58. $StartInfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Minimized; 
  59. "Opening with Start info (p6)" 
  60. $proc6 = [System.Diagnostics.Process]::Start($StartInfo
  61. "Opening with startinfo + www.reskit.net (p7)" 
  62. $StartInfo.Arguments = "www.reskit.net"
  63. $proc7 = [System.Diagnostics.Process]::Start($startInfo
  64.  
  65. ## 
  66. # Start of script 
  67. ## 
  68.  
  69. # First, get Favourites path 
  70. $FavoritesPath = [system.Environment]::GetFolderPath('Favorites'
  71.  
  72. # Open two windows - one pointing to IE's home page, the other 
  73. # to my favourites folder (p1, p2) 
  74. OpenApplication $FavoritesPath 
  75.  
  76. # Here call function to open pshscripts, c:\foo\myfile.htm c:\foo\myfile.asp 
  77. #p3, p3, p5 
  78. OpenWithArguments 
  79.  
  80. #Here open two processes using Startinfo object 
  81. # p6, p7 
  82. OpenWithStartInfo 
  83. #End of script 

Tuesday, 18 May 2010

Start-Process.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script creates and starts a Process using .NET 
  4. .DESCRIPTION 
  5.     This script Creates a process object and sets 
  6.     the executable to notepad. The script then starts 
  7.     the process. 
  8. .NOTES 
  9.     File Name  : Start-Process.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 2.0 
  12. .LINK 
  13.     This script posted to: 
  14.         http://pshscripts.blogspot.com/2010/05/start-processps1.html
  15.     MSDN Sample posted at: 
  16.         http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx  
  17. .EXAMPLE 
  18.    When this script runs - you see a copy of notepad popup. 
  19. #> 
  20.  
  21. ## 
  22. # Start of Script 
  23. ## 
  24.  
  25. # Create a new process object 
  26. $Process = new-object System.Diagnostics.Process  
  27. try  { 
  28. $Process.StartInfo.UseShellExecute = $false 
  29.  
  30. # Pick process to start  now - for excitement, use notepad.exe 
  31. $Process.StartInfo.FileName = "C:\windows\system32\notepad.exe" 
  32.  
  33. # Start process  
  34. $Process.Start() 
  35. catch { 
  36.   " Error:";$Error[0]  

Saturday, 6 June 2009

Get-ProcessPerfCounter.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script creates a process, then displays some performance counters. 
  4. .DESCRIPTION 
  5.     This script calls System.Diagnostics.Process's Start static 
  6.     method to create a process.  Then it displays perf stats till the
  7.     process is stopped. Then it prints final stats out.
  8. .NOTES 
  9.     File Name  : Get-ProcessPerfCounter 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell V2 CTP3 
  12. <# 
  13. .LINK 
  14.     This script posted to: 
  15.         http://www.pshscripts.blogspot.com 
  16.     MSDN Sample posted at: 
  17.         http://msdn.microsoft.com/en-us/library/system.diagnostics.process.peakworkingset64.aspx 
  18. .EXAMPLE 
  19.     PSH [C:\foo]: .\Get-ProcessPerfCounter.ps1 
  20.  
  21.     System.Diagnostics.Process (notepad) - 
  22.     ------------------------------------- 
  23.       physical memory usage     : 2,512 
  24.       base priority             : 8 
  25.       priority class            : Normal 
  26.       user processor time       : 00:00:00.0156001 
  27.       privileged processor time : 00:00:00.0156001 
  28.       total processor time      : 00:00:00.0312002 
  29.   
  30.     ** Loads more - snipped for brevity ** 
  31.      
  32.     System.Diagnostics.Process (notepad) - 
  33.     ------------------------------------- 
  34.       physical memory usage     : 12,424 
  35.       base priority             : 8 
  36.       priority class            : Normal 
  37.       user processor time       : 00:00:00.0156001 
  38.       privileged processor time : 00:00:00.0624004 
  39.       total processor time      : 00:00:00.0780005 
  40.   
  41.       Process has ended 
  42.       Process exit code: 0 
  43.       Peak physical memory usage of the process :  12,424 kb 
  44.       Peak paged memory usage of the process    :   2,740 kb 
  45.       Peak virtual memory usage of the process  :  88,832 kb 
  46. #> 
  47.   
  48. ## 
  49. # Start of script 
  50. ## 
  51.    
  52. # Start up Notepad, catching issues 
  53. try  { 
  54. $myproc =  [System.Diagnostics.Process]::Start("c:\windows\notepad.exe"
  55. catch { 
  56.   "Error starting process" 
  57.   return 
  58.   
  59. # Now print perf stats until Notepad.exe is closed 
  60. do { 
  61.   if ( ! $myproc.HasExited ) { 
  62.   $myproc.Refresh() 
  63.   "" 
  64.   "{0} -"        -f $myProc.ToString() 
  65.   "-------------------------------------" 
  66.   "  physical memory usage     : {0}" -f $($MyProc.WorkingSet64/1kb).tostring("###,###"
  67.   "  base priority             : {0}" -f $MyProc.BasePriority 
  68.   "  priority class            : {0}" -f $MyProc.PriorityClass 
  69.   "  user processor time       : {0}" -f $MyProc.UserProcessorTime 
  70.   "  privileged processor time : {0}" -f $MyProc.PrivilegedProcessorTime 
  71.   "  total processor time      : {0}" -f $MyProc.TotalProcessorTime 
  72.     
  73. # calculate overall peak 
  74. $peakPagedMem   = $MyProc.PeakPagedMemorySize64 
  75. $peakVirtualMem = $MyProc.PeakVirtualMemorySize64 
  76. $peakWorkingSet = $MyProc.PeakWorkingSet64 
  77.   
  78. } # end of if 
  79.      
  80. } # end of do 
  81. while (!$myproc.WaitForExit(1000)) # Wait a second and do it again 
  82.  
  83. # Here process has exited 
  84. # Print out final results 
  85. "" 
  86. "Process has ended" 
  87. "Process exit code: {0}" -f $MyProc.ExitCode 
  88.  
  89. # Display peak memory statistics for the process. 
  90. "Peak physical memory usage of the process : {0,7} kb" -f $($peakWorkingSet/1kb).ToString("###,###"
  91. "Peak paged memory usage of the process    : {0,7} kb" -f $($peakPagedMem/1kb).ToString("###,###"
  92. "Peak virtual memory usage of the process  : {0,7} kb" -f $($peakVirtualMem/1kb).ToString("###,###"
  93. # End of script