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.  
  55. # Uses the ProcessStartInfo class to start new processes, 
  56. # both in a minimized mode. 
  57. function OpenWithStartInfo { 
  58. $StartInfo = New-Object System.Diagnostics.ProcessStartInfo "IExplore.exe" 
  59. $StartInfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Minimized; 
  60. "Opening with Start info (p6)" 
  61. $proc6 = [System.Diagnostics.Process]::Start($StartInfo) 
  62. "Opening with startinfo + www.reskit.net (p7)" 
  63. $StartInfo.Arguments = "www.reskit.net"; 
  64. $proc7 = [System.Diagnostics.Process]::Start($startInfo) 
  65. } 
  66.  
  67. ## 
  68. # Start of script 
  69. ## 
  70.  
  71. # First, get Favourites path 
  72. $FavoritesPath = [system.Environment]::GetFolderPath('Favorites') 
  73.  
  74. # Open two windows - one pointing to IE's home page, the other 
  75. # to my favourites folder (p1, p2) 
  76. OpenApplication $FavoritesPath 
  77.  
  78. # Here call function to open pshscripts, c:\foo\myfile.htm c:\foo\myfile.asp 
  79. #p3, p3, p5 
  80. OpenWithArguments 
  81.  
  82. #Here open two processes using Startinfo object 
  83. # p6, p7 
  84. OpenWithStartInfo 
  85. #End of script 

No comments: