- <#
- .SYNOPSIS
- This script starts up a process using System.Diagnostics.Process
- .DESCRIPTION
- This script creates 7 occurences of IExplore.exe, using
- diffrent start up options, based on a sample script in MSDN.
- .NOTES
- File Name : Start-process2.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/05/start-process2ps1.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/0w4h05yb.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Start-Process2.ps1'
- Starting IE at home page (p1)
- Opening Favourites (p2)
- Opening PSHScripts (p3)
- Opening c:\foo\myfile.htm (p4)
- Opening c:\foo\myfile.asp (p5)
- Opening with Start info (p6)
- Opening with startinfo + www.reskit.net (p7)
- #>
- # Helper Functions
- # Opens the Internet Explorer application.
- function OpenApplication {
- param([string] $FavoritesPath)
- # Start Internet Explorer. Defaults to the home page.
- "Starting IE at home page (p1)"
- $proc1 = [System.Diagnostics.Process]::start("IExplore.exe");
- # Display the contents of the favorites folder in the browser.
- "Opening Favourites (p2)"
- $proc2=[System.Diagnostics.Process]::Start($FavoritesPath);
- }
- # Opens urls and .html documents using Internet Explorer.
- function OpenWithArguments {
- # url's are not considered documents. They can only be opened
- # by passing them as arguments.
- "Opening PSHScripts (p3)"
- $proc3 = [System.Diagnostics.Process]::Start("IExplore.exe", "www.pshscripts.blogspot.com")
- # Start a Web page using a browser associated with .html and .asp files.
- "Opening c:\foo\myfile.htm (p4)"
- $proc4 = [System.Diagnostics.Process]::Start("IExplore.exe", "C:\foo\myFile.htm")
- "Opening c:\foo\myfile.asp (p5)"
- $proc5 = [System.Diagnostics.Process]::Start("IExplore.exe", "C:\foo\myFile.asp")
- }
- # Uses the ProcessStartInfo class to start new processes,
- # both in a minimized mode.
- function OpenWithStartInfo {
- $StartInfo = New-Object System.Diagnostics.ProcessStartInfo "IExplore.exe"
- $StartInfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Minimized;
- "Opening with Start info (p6)"
- $proc6 = [System.Diagnostics.Process]::Start($StartInfo)
- "Opening with startinfo + www.reskit.net (p7)"
- $StartInfo.Arguments = "www.reskit.net";
- $proc7 = [System.Diagnostics.Process]::Start($startInfo)
- }
- ##
- # Start of script
- ##
- # First, get Favourites path
- $FavoritesPath = [system.Environment]::GetFolderPath('Favorites')
- # Open two windows - one pointing to IE's home page, the other
- # to my favourites folder (p1, p2)
- OpenApplication $FavoritesPath
- # Here call function to open pshscripts, c:\foo\myfile.htm c:\foo\myfile.asp
- #p3, p3, p5
- OpenWithArguments
- #Here open two processes using Startinfo object
- # p6, p7
- OpenWithStartInfo
- #End of script
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
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
Tuesday, 18 May 2010
Start-Process.ps1
- <#
- .SYNOPSIS
- This script creates and starts a Process using .NET
- .DESCRIPTION
- This script Creates a process object and sets
- the executable to notepad. The script then starts
- the process.
- .NOTES
- File Name : Start-Process.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/05/start-processps1.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx
- .EXAMPLE
- When this script runs - you see a copy of notepad popup.
- #>
- ##
- # Start of Script
- ##
- # Create a new process object
- $Process = new-object System.Diagnostics.Process
- try {
- $Process.StartInfo.UseShellExecute = $false
- # Pick process to start now - for excitement, use notepad.exe
- $Process.StartInfo.FileName = "C:\windows\system32\notepad.exe"
- # Start process
- $Process.Start()
- }
- catch {
- " Error:";$Error[0]
- }
Saturday, 6 June 2009
Get-ProcessPerfCounter.ps1
- <#
- .SYNOPSIS
- This script creates a process, then displays some performance counters.
- .DESCRIPTION
- This script calls System.Diagnostics.Process's Start static
- method to create a process. Then it displays perf stats till the
- process is stopped. Then it prints final stats out.
- .NOTES
- File Name : Get-ProcessPerfCounter
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- <#
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.diagnostics.process.peakworkingset64.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-ProcessPerfCounter.ps1
- System.Diagnostics.Process (notepad) -
- -------------------------------------
- physical memory usage : 2,512
- base priority : 8
- priority class : Normal
- user processor time : 00:00:00.0156001
- privileged processor time : 00:00:00.0156001
- total processor time : 00:00:00.0312002
- ** Loads more - snipped for brevity **
- System.Diagnostics.Process (notepad) -
- -------------------------------------
- physical memory usage : 12,424
- base priority : 8
- priority class : Normal
- user processor time : 00:00:00.0156001
- privileged processor time : 00:00:00.0624004
- total processor time : 00:00:00.0780005
- Process has ended
- Process exit code: 0
- Peak physical memory usage of the process : 12,424 kb
- Peak paged memory usage of the process : 2,740 kb
- Peak virtual memory usage of the process : 88,832 kb
- #>
- ##
- # Start of script
- ##
- # Start up Notepad, catching issues
- try {
- $myproc = [System.Diagnostics.Process]::Start("c:\windows\notepad.exe")
- }
- catch {
- "Error starting process"
- return
- }
- # Now print perf stats until Notepad.exe is closed
- do {
- if ( ! $myproc.HasExited ) {
- $myproc.Refresh()
- ""
- "{0} -" -f $myProc.ToString()
- "-------------------------------------"
- " physical memory usage : {0}" -f $($MyProc.WorkingSet64/1kb).tostring("###,###")
- " base priority : {0}" -f $MyProc.BasePriority
- " priority class : {0}" -f $MyProc.PriorityClass
- " user processor time : {0}" -f $MyProc.UserProcessorTime
- " privileged processor time : {0}" -f $MyProc.PrivilegedProcessorTime
- " total processor time : {0}" -f $MyProc.TotalProcessorTime
- # calculate overall peak
- $peakPagedMem = $MyProc.PeakPagedMemorySize64
- $peakVirtualMem = $MyProc.PeakVirtualMemorySize64
- $peakWorkingSet = $MyProc.PeakWorkingSet64
- } # end of if
- } # end of do
- while (!$myproc.WaitForExit(1000)) # Wait a second and do it again
- # Here process has exited
- # Print out final results
- ""
- "Process has ended"
- "Process exit code: {0}" -f $MyProc.ExitCode
- # Display peak memory statistics for the process.
- "Peak physical memory usage of the process : {0,7} kb" -f $($peakWorkingSet/1kb).ToString("###,###")
- "Peak paged memory usage of the process : {0,7} kb" -f $($peakPagedMem/1kb).ToString("###,###")
- "Peak virtual memory usage of the process : {0,7} kb" -f $($peakVirtualMem/1kb).ToString("###,###")
- # End of script
Subscribe to:
Posts (Atom)