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 

No comments: