- <#
- .SYNOPSIS
- This script displays service details
- .DESCRIPTION
- This script first enumerates all the service controllers (ie services)
- running on the local system. For each service, we look into WMI and
- get info about that running service.
- .NOTES
- File Name : Get-ServiceDetails.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/05/get-servicedetailsps1.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/hde9d63a.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-ServiceDetails.ps1'
- Services running on the local computer:
- Service : AeLookupSvc
- Display name: Application Experience
- Start name: localSystem
- Description: Processes application compatibility cache requests for applications as they are launched
- Service : AppHostSvc
- Display name: Application Host Helper Service
- Start name: LocalSystem
- Description: Provides administrative services for IIS, for example configuration history and Applicati
- on Pool account mapping. If this service is stopped, configuration history and locking down files or directori
- es with Application Pool specific Access Control Entries will not work.
- <rest snipped to save space!>
- #>
- ##
- # Start of script
- ##
- # Load assembly with ServiceProcess class
- $result = [reflection.Assembly]::LoadWithPartialName("System.ServiceProcess")
- $Services = [System.ServiceProcess.ServiceController]::GetServices()
- # Get WMI Services
- $WMIServices = gwmi win32_service
- # Display the list of services currently running on this computer.
- "Services running on the local computer:"
- foreach ($Service in $Services) {
- if ($Service.Status -eq [system.ServiceProcess.ServiceControllerStatus]::Running) {
- # Write the service name and the display name
- # for each running service.
- ""
- " Service : {0}" -f $Service.ServiceName
- " Display name: {0}" -f $Service.DisplayName
- # query WMI for more info on service
- $svc = $wmiServices | where {$_.name -eq $service.servicename}
- " Start name: {0}" -f $Svc.StartName
- " Description: {0}" -f $Svc.Description
- }
- }
- # End
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Saturday, 22 May 2010
Get-ServiceDetails.ps1
Friday, 21 May 2010
Write-XmlElementString.ps1
- <#
- .SYNOPSIS
- This script uses XMLWriter to write an XML Element
- .DESCRIPTION
- This script creates and uses and XML writer to write an
- XML Element
- .NOTES
- File Name : Write-XxmElementString.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/05/write-xxmelementstringps1.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/aex0e7zs.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Write-XmlElementString.ps1'
- <orderID>1-456-ab</orderID><orderID>2-36-00a</orderID>
- #>
- # Create new XMLWriter settings
- $settings = new-object System.Xml.XmlWriterSettings
- $settings.OmitXmlDeclaration = $true
- $settings.ConformanceLevel = 'Fragment'
- $settings.CloseOutput = $false;
- # Create the XmlWriter object and write some content.
- #$strm = new-object system.io.MemoryStream
- $writer = [system.xml.XmlWriter]::Create("c:\foo\data.xml", $settings)
- $writer.WriteElementString("orderID", "1-456-ab")
- $writer.WriteElementString("orderID", "2-36-00a")
- $writer.Flush()
- $writer.Close()
- # Display data
- cat c:\foo\data.xml
- # End Script
Write-XML.ps1
- <#
- .SYNOPSIS
- This script uses XMLWriter to write XML
- .DESCRIPTION
- This script creates and XML writer and writes some basic
- XML.
- .NOTES
- File Name : Write-XML.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/05/write-xmlps1.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Write-XML.ps1'
- <?xml version="1.0" encoding="IBM437"?>
- <order
- orderID="367A54"
- date="2001-05-03">
- <price>19.95</price>
- </order>PSH [C:\foo]:
- #>
- # Create a new writer settings object
- # And set settings
- $settings = New-Object system.Xml.XmlWriterSettings
- $settings.Indent = $true
- $settings.OmitXmlDeclaration = $false
- $settings.NewLineOnAttributes = $true
- # Create a new Writer
- $writer = [system.xml.XmlWriter]::Create([system.console]::Out, $settings)
- #Write some XML
- $writer.WriteStartElement("order")
- $writer.WriteAttributeString("orderID", "367A54")
- $writer.WriteAttributeString("date", "2001-05-03")
- $writer.WriteElementString("price", "19.95")
- $writer.WriteEndElement()
- # Flush the writer (and close the file)
- $writer.Flush()
- $writer.Close()
Wednesday, 19 May 2010
Start-Process2.ps1
- <#
- .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
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]
- }
Sunday, 2 May 2010
New-MailAddress.ps1
- <#
- .SYNOPSIS
- This script demonstrates the constructors for system.net.mail.mailaddress
- .DESCRIPTION
- This script uses the three constructors for new objects of the type
- system.net.mail.mailaddress using PowerShell. These constructors are:
- . single string
- . two strings
- . two strings and an encoding.
- .NOTES
- File Name : New-MailAddress.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN Sample posted at:
- http://pshscripts.blogspot.com/2010/05/get-utf8encoding.html
- .EXAMPLE
- PSH [C:\foo]: .\New-MailAddress.ps1'
- Creating mail address for: tfl@psp.co.uk
- User Host Display Name To String
- ---- ---- ------------ ---------
- tfl psp.co.uk tfl@psp.co.uk
- Creating mail address for: Thomas Lee <tfl@psp.co.uk>
- User Host Display Name To String
- ---- ---- ------------ ---------
- tfl psp.co.uk Thomas Lee "Thomas Lee" <tfl@psp.co.uk>
- Creating mail address for: "Thomas Lee" and "tfl@psp.co.uk"
- User Host Display Name To String
- ---- ---- ------------ ---------
- tfl psp.co.uk Thomas Lee "Thomas Lee" <tfl@psp.co.uk>
- Creating mail address for: "Thomas Lee","tfl@psp.co.uk", and Ascii encoding
- User Host Display Name To String
- ---- ---- ------------ ---------
- tfl psp.co.uk ThOmas Lee - πΣ "ThOmas Lee - πΣ" <tfl@psp.co.uk>
- Creating mail address for: "Thomas Lee","tfl@psp.co.uk", and UTF8 encoding
- User Host Display Name To String
- ---- ---- ------------ ---------
- tfl psp.co.uk ThOmas Lee - πΣ "ThOmas Lee - πΣ" <tfl@psp.co.uk>
- #>
- ##
- # Start of script
- ##
- # Define helper functions
- # nma - creates a new mail address
- # uses single string constuctor
- function nma {
- param ([string] $addr)
- new-object system.net.Mail.MailAddress $addr
- }
- # nma2 - creates a new mail address
- # uses twin string constuctor
- function nma2 {
- param ([string] $addr,
- [string] $dspnam
- )
- new-object system.net.Mail.MailAddress $addr, $dspnam
- }
- # nma3 - creates a new mail address
- # uses twin string+encoding constuctor
- function nma3 {
- param ([string] $addr, [string] $dspnam, $enc)
- new-object system.net.Mail.MailAddress $addr, $dspnam, $enc
- }
- # Create hash tables for display of email addresses
- $ts=@{label="To String"; expression={$_.tostring()}}
- $dn=@{label="Display Name"; expression={$_.displayname}}
- # Look at single string constructor usage
- # Create and display for address: tfl@psp.co.uk
- $ad="tfl@psp.co.uk"
- "Creating mail address for: $ad"
- nma $ad | ft user,host,$dn,$ts -a
- # Create and display for address: Thomas Lee <tfl@psp.co.uk>
- $ad = "Thomas Lee <tfl@psp.co.uk>"
- "Creating mail address for: $ad"
- nma $ad | ft user,host,$dn,$ts -a
- # Twin string constructor
- # Create and display for address: tfl@psp.co.uk, "Thomas Lee"
- $ad = "tfl@psp.co.uk"
- $dnam = "Thomas Lee"
- "Creating mail address for: `"$dname`" and `"$ad`""
- nma2 $ad $dnam | ft user,host,$dn,$ts -a
- # Using 3rd constructor - two strings and and encoding
- # Create and display for address: tfl@psp.co.uk, "Thomas Lee" in Ascii
- $c=[char]0xd8
- $ad = "tfl@psp.co.uk"
- $dnam = "Th"+$c+"mas Lee - " + [char] [char]0x03a0 + [char] 0x03a3
- $enc = New-Object System.Text.AsciiEncoding
- "Creating mail address for: `"$dname`",`"$ad`", and Ascii encoding"
- nma3 $ad $dnam $enc | ft user,host,$dn,$ts -a
- # Create and display for address: tfl@psp.co.uk, "Thomas Lee" in UTF8
- $c=[char]0xd8
- $ad = "tfl@psp.co.uk"
- $dnam = "Th"+$c+"mas Lee - " + [char] [char]0x03a0 + [char] 0x03a3
- $enc = New-Object System.Text.utf8encoding
- "Creating mail address for: `"$dname`",`"$ad`", and UTF8 encoding"
- nma3 $ad $dnam $enc | ft user,host,$dn,$ts -a
Get-UTF8Encoding
- <#
- .SYNOPSIS
- This script implements an MSDN Encoding example in PowerShell
- .DESCRIPTION
- This script creates an encoding plus a Unicode string then
- manipulates it as per the C# sample.
- .NOTES
- File Name : Get-UTF8Encoding
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/05/get-utf8encoding.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.text.utf8encoding.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-UTF8Encoding.ps1
- Original string:This unicode string contains two characters with
- codes outside an 8-bit code range, Pi (π) and Sigma (Σ).
- Encoded bytes:
- <long table omitted>
- Decoded bytes:
- This unicode string contains two characters with codes outside an
- 8-bit code range, Pi (π) and Sigma (Σ).
- #>
- # Start of Script
- # Create a UTF-8 encoding.
- $utf8 = New-Object System.Text.utf8encoding
- $unicodeString =
- "This unicode string contains two characters " +
- "with codes outside an 8-bit code range, " +
- "Pi (" + [char] 0x03a0 + ") and Sigma (" + [char] 0x03a3 + ")."
- "Original string:{0}" -f $unicodeString
- # Encode the string.
- $encodedBytes = $utf8.GetBytes($unicodeString)
- ""
- "Encoded bytes:"
- foreach($b in $encodedBytes) {"[{0}]" -f $b}
- # Decode bytes back to string
- # Notice Pi and Sigma characters are still present
- $decodedString = $utf8.GetString($encodedBytes)
- "";"Decoded bytes:"
- $decodedString
- # End of Script
Saturday, 17 April 2010
Get-ProfileFile.ps1
- <#
- .SYNOPSIS
- This script gets the details of the PowerShell profile files
- related to a given host.
- .DESCRIPTION
- This script gets the 4 script file names from the $profile
- variable and creates a custom object containing the name and
- path or each profile, and whether that profile actually exists.
- .NOTES
- File Name : Get-ProfileFile.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/04/get-profilefileps1.html
- .EXAMPLE
- [PS] C:\foo:> .\Get-ProfileFile.ps1'
- Name : AllUsersAllHosts
- Path : C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
- Exists : True
- Name : AllUsersCurrentHosts
- Path : C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
- Exists : False
- Name : CurrentUserAllHosts
- Path : C:\Users\tfl\Documents\WindowsPowerShell\profile.ps1
- Exists : True
- Name : CurrentUserCurrentHost
- Path : C:\Users\tfl\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
- Exists : True
- #>
- ##
- # Start of Script
- ##
- $profile | gm *Host* | % { $_.Name } | % {
- $rv = @{}; $rv.Name = $_ ;$rv.Path = $profile.$_;
- $rv.Exists= (Test-Path $profile.$_);
- New-Object PSObject -Property $rv
- } | Format-List
- # End of script
Labels:
powershell,
PowerShell scripts,
profile
Tuesday, 23 March 2010
Get-FirewallStatus.ps1
- <#
- .SYNOPSIS
- This script gets the status of the host firewall
- and ensures the firewall IS running!
- .DESCRIPTION
- This script gets the status and displays it to the
- console. The script also turns on the firewall if it's
- currently off. It's a simpler script than in MSDN for VBScript!
- .NOTES
- File Name : Get-FirewallStatus.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/03/get-firewallstatusps1.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/aa366442%28VS.85%29.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-FirewallStatus.ps1
- Firewall Enabled : True
- Firewall Exceptions Not Allowed: False
- #>
- ##
- # Start Script
- ##
- # Create the firewall manager object.
- $fwMgr = New-Object -com HNetCfg.FwMgr
- # Get the current profile for the local firewall policy.
- $profile = $fwMgr.LocalPolicy.CurrentProfile
- # Verify that the Firewall is enabled. If it isn't, then enable it.
- if (!$profile.FirewallEnabled)
- {$profile.FirewallEnabled = $TRUE}
- # Display details
- "Firewall Enabled : {0}" -f $profile.FirewallEnabled
- "Firewall Exceptions Not Allowed: {0}" -f $profile.ExceptionsNotAllowed
- # End Script
Labels:
code,
HNetCfg.FwMgr,
powershell,
PowerShell scripts
Monday, 22 March 2010
Get-FWAuthorisedApplications.ps1
- <#
- .SYNOPSIS
- This script displays each Firewall Authorised Application
- .DESCRIPTION
- This script gets the list of authorised applications, then
- displays them. This is a re-write of a MSDN Script written in
- VBScript.
- .NOTES
- File Name : Get-FWAuthorisedApplications.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/aa366181%28VS.85%29.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-FWAuthorisedApplications.ps1
- 2 Authorised Applications:
- Name: : Delivery Manager Service
- Image Filename : C:\Program Files (x86)\Kontiki\KService.exe
- IP Version : ANY
- Scope : All subnets
- RemoteAddresses: *
- Enabled : True
- Name: : BitTorrent
- Image Filename : C:\Program Files (x86)\BitTorrent\bittorrent.ex
- IP Version : ANY
- Scope : All subnets
- RemoteAddresses: *
- Enabled : True
- #>
- ##
- # Start of script
- ##
- # IP Version Constants
- $NET_FW_IP_VERSION_V4 = 0
- $NET_FW_IP_VERSION_V4_NAME = "IPv4"
- $NET_FW_IP_VERSION_V6 = 1
- $NET_FW_IP_VERSION_V6_NAME = "IPv6"
- $NET_FW_IP_VERSION_ANY = 2
- $NET_FW_IP_VERSION_ANY_NAME = "ANY"
- # Scope constants
- $NET_FW_SCOPE_ALL = 0
- $NET_FW_SCOPE_ALL_NAME = "All subnets"
- $NET_FW_SCOPE_LOCAL_SUBNET = 1
- $NET_FW_SCOPE_LOCAL_SUBNET_NAME = "Local subnet only"
- $NET_FW_SCOPE_CUSTOM = 2
- $NET_FW_SCOPE_CUSTOM_NAME = "Custom Scope (see RemoteAddresses)"
- # Create the firewall manager object
- $fwMgr = new-object -com HNetCfg.FwMgr
- # Get the current profile for the local firewall policy
- $profile = $fwMgr.LocalPolicy.CurrentProfile
- #Display authorised applications
- "{0} Authorised Applications:" -f $profile.AuthorizedApplications.Count
- foreach ($app in $profile.AuthorizedApplications) {
- " Name: : {0}" -f $app.Name
- " Image Filename : {0}" -f $app.ProcessImageFileName
- switch ($app.IpVersion) {
- $NET_FW_IP_VERSION_V4 {" IP Version : {0}" -f $NET_FW_IP_VERSION_V4_NAME}
- $NET_FW_IP_VERSION_V6 {" IP Version : {0}" -f $NET_FW_IP_VERSION_V6_NAME}
- $NET_FW_IP_VERSION_ANY {" IP Version : {0}" -f $NET_FW_IP_VERSION_ANY_NAME}
- }
- switch ($app.Scope) {
- $NET_FW_SCOPE_ALL {" Scope : {0}" -f $NET_FW_SCOPE_ALL_NAME}
- $NET_FW_SCOPE_LOCAL_SUBNET {" Scope : {0}" -f $NET_FW_SCOPE_LOCAL_SUBNET_NAME}
- $NET_FW_SCOPE_CUSTOM {" Scope : {0}" -f $NET_FW_SCOPE_CUSTOM_NAME}
- }
- " RemoteAddresses: {0}" -f $app.RemoteAddresses
- " Enabled : {0}" -f $app.Enabled
- ""
- }
Labels:
HNetCfg.FwMgr,
powershell,
PowerShell scripts,
Script,
scripts
Subscribe to:
Posts (Atom)