Monday, 14 February 2011

New-Shortcut.ps1

  1. <# 
  2. .SYNOPSIS 
  3.    This script creates a shortcut to Notepad on the Desktop 
  4. .DESCRIPTION 
  5.    This script creates a Wscript.shell item, then creates  
  6.    a shortcut on the desktop to Notepad.exe. 
  7. .NOTES 
  8.     File Name  : New-Shortcut.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell Version 2.0 
  11. .LINKS 
  12.     This post is a re-implementation of an MSDN Script 
  13.         http://msdn.microsoft.com/en-us/library/0ea7b5xe%28VS.85%29.aspx 
  14.     Posted to Powershell Scripts Blog 
  15.         HTTP://Pshscripts.blogspot.com 
  16. .EXAMPLE 
  17.    Left as an exercise for the reader 
  18. #> 
  19.  
  20. # create wscript object 
  21. $WshShell = New-Object -com Wscript.Shell 
  22.  
  23. #Get Desktop location 
  24. $Desktop  = $WshShell.SpecialFolders.item("Desktop"
  25.  
  26. # create a new shortcut 
  27. $ShellLink                  = $WshShell.CreateShortcut($Desktop + "\Shortcut Script.lnk"
  28. $ShellLink.TargetPath       = $WScript.ScriptFullName 
  29. $ShellLink.WindowStyle      = 1 
  30. $ShellLink.Hotkey           = "CTRL+SHIFT+F" 
  31. $ShellLink.IconLocation     = "notepad.exe, 0" 
  32. $ShellLink.Description      = "Shortcut Script" 
  33. $ShellLink.WorkingDirectory = $Desktop 
  34.  
  35. #Save the link to the desktop 
  36. $ShellLink.Save() 

Tuesday, 4 January 2011

Get-ComputerDomain.ps1

<#
.SYNOPSIS
This script uses WMI to get the name of a computer's domain then displays the name.
.DESCRIPTION
This script is a re-write of an MSDN sample, using PowerShell.
.NOTES
File Name : Get-ComputerDomain.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 tot:
http://msdn.microsoft.com/en-us/library/aa394586%28VS.85%29.aspx/
.EXAMPLE
PSH [C:\foo]: .\Get-ComputerDomain.ps1
System Name: WIN7
Domain: cookham.net
.EXAMPLE
Psh[Cookham8]> .\Get-ComputerDomain.ps1 cookham2
System Name: COOKHAM2
Domain: cookham.net
.PARAMETER comp
The name of the computer whose domain name to be displayed. Default is localhost.
#>


# Parameter block
param (
$comp = "."
)

# Get WMI Object
$system = Get-WmiObject -class Win32_ComputerSystem -ComputerName $comp

# Display results
"System Name: {0}" -f $System.Name
"Domain: {0}" -f $System.Domain


Friday, 31 December 2010

Count-GDDuplicate.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Finds duplicates in the GD archive – A script
  4.     for New Year’s eve! 
  5. .DESCRIPTION 
  6.  
  7. .NOTES 
  8.     File Name  : Count-GDDuplicate.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell V2 
  11. .LINK 
  12.     http://pshscripts.blogspot.com/2010/12/count-gdduplicateps1.html 
  13. .EXAMPLE 
  14.     Psh[Cookham8]>C:\foo\Count-GdDuplicate.ps1 
  15.     Count-GDDuplicate.ps1 - v 1.0.1 
  16.  
  17.     99 shows have duplicates 
  18.     109 shows are duplicates of others 
  19. #> 
  20.  
  21. ### 
  22. # Start of Script 
  23. ## 
  24.  
  25. # Constants: 
  26. # $GDDiskRoot    - where to find shows 
  27. # $DeadShowBase  - folder at top of gd shows 
  28.  
  29. $GDDiskRoot = "M:" 
  30. $DeadShowBase = $GDDiskRoot + "\gd" 
  31.  
  32. # Announce Ourselves 
  33. "DuplicateCount.ps1 - v 0.0.1" 
  34. "+---------------------------------+" 
  35. "! Find Duplicate Shows :  $DeadShowBase   !" 
  36. "+---------------------------------+" 
  37. "" 
  38. # Get start time 
  39. $starttime = Get-Date 
  40. set-strictmode -off 
  41.  
  42. # Get the Dead shows 
  43.  
  44. $Dir = ls $DeadShowBase | where {$_.psiscontainer} 
  45. $DeadShows = $Dir.count 
  46. if ($DeadSHows -le 0) {"no shows found - check constants"; return
  47.  
  48. # So here look for duplicates. 
  49.  
  50. $shows =@{} 
  51.  
  52. $j = 0 
  53. foreach ($show in $dir){ 
  54. $j++ 
  55. $showdate = $show.name.substring(0,10) 
  56. if ($shows.$showdate) { 
  57.   $shows.$showdate++ 
  58. }   
  59. else
  60.   $shows += @{$showdate=1} 
  61. }    
  62.  
  63.  
  64. $shows = $shows.getenumerator() | Sort name 
  65. $totaldups = 0 
  66. $totaldupshows = 0 
  67. foreach ($show in $shows){ 
  68.    if ($show.value -gt 1) { 
  69.      # "{1} copies of: {0}" -f  $show.name, $show.value 
  70.      $totaldups++    
  71.      $totaldupshows += $show.value - 1 # anything after the 1st is a dup 
  72.    } 
  73.  
  74. # Display Summary 
  75. "{0} shows have duplicates" -f $totaldups 
  76. "{0} shows are duplicates of others" -f $totaldupshows 
Technorati Tags:

Thursday, 23 December 2010

Set-IseThemeVim.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script sets an ISE Theme to similar to the old VIM editor. 
  4. .DESCRIPTION 
  5.     This script sets the key values in $PsIse.Options to values consistent 
  6.     with the VIM editor, beloved by many, particularly on the Powershell 
  7.     product team. This script is based on Davis Mohundro's blog post
  8.     (http://bit.ly/iib5IM), updated for RTM of PowerShell V2.0. See also  
  9. .NOTES 
  10.     File Name  : Set-ISEThemeVIM.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell Version 2.0 (ISE only) 
  13. .LINK 
  14.     This script posted to: 
  15.         http://pshscriptsbog.blogspot.com  
  16.         http://bit.ly/iaA2iX
  17. .EXAMPLE 
  18.     This script when run resets colours on key panes, including 
  19.     colourising tokens in the script pane. Try it and see it... 
  20. #> 
  21.  
  22.  
  23. # PowerShell ISE version of the VIM blackboard theme at  
  24. # http://www.vim.org/scripts/script.php?script_id=2280 
  25.  
  26. # Set font name and size 
  27. $psISE.Options.FontName = 'Courier New' 
  28. $psISE.Options.FontSize = 16 
  29.  
  30. # Set colours for output pane 
  31. $psISE.Options.OutputPaneBackgroundColor     = '#FF000000' 
  32. $psISE.Options.OutputPaneTextBackgroundColor = '#FF000000' 
  33. $psISE.Options.OutputPaneForegroundColor     = '#FFFFFFFF' 
  34.  
  35. # Set colours for command pane 
  36. $psISE.Options.CommandPaneBackgroundColor    = '#FF000000' 
  37.  
  38. # Set colours for script pane 
  39.  
  40. $psise.options.ScriptPaneBackgroundColor    ='#FF000000' 
  41.  
  42. # Set colours for tokens in Script Pane 
  43. $psISE.Options.TokenColors['Command'] = '#FFFFFF60' 
  44. $psISE.Options.TokenColors['Unknown'] = '#FFFFFFFF' 
  45. $psISE.Options.TokenColors['Member'] = '#FFFFFFFF' 
  46. $psISE.Options.TokenColors['Position'] = '#FFFFFFFF' 
  47. $psISE.Options.TokenColors['GroupEnd'] = '#FFFFFFFF' 
  48. $psISE.Options.TokenColors['GroupStart'] = '#FFFFFFFF' 
  49. $psISE.Options.TokenColors['LineContinuation'] = '#FFFFFFFF' 
  50. $psISE.Options.TokenColors['NewLine'] = '#FFFFFFFF' 
  51. $psISE.Options.TokenColors['StatementSeparator'] = '#FFFFFFFF' 
  52. $psISE.Options.TokenColors['Comment'] = '#FFAEAEAE' 
  53. $psISE.Options.TokenColors['String'] = '#FF00D42D' 
  54. $psISE.Options.TokenColors['Keyword'] = '#FFFFDE00' 
  55. $psISE.Options.TokenColors['Attribute'] = '#FF84A7C1' 
  56. $psISE.Options.TokenColors['Type'] = '#FF84A7C1' 
  57. $psISE.Options.TokenColors['Variable'] = '#FF00D42D' 
  58. $psISE.Options.TokenColors['CommandParameter'] = '#FFFFDE00' 
  59. $psISE.Options.TokenColors['CommandArgument'] = '#FFFFFFFF' 
  60. $psISE.Options.TokenColors['Number'] = '#FF98FE1E' 
Technorati Tags: ,,,

Set-ISEThemeDefault.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script resets the ISE to default ‘theme’. 
  4. .DESCRIPTION 
  5.     This script sets the key values in $PsIse.Options to their default 
  6.     options in $Psise.Options.DefaultOptions. This script is useful if you 
  7.     are playing with ISE options and don't quite get it right - just run this  
  8.     script to set things back to default.        
  9. .NOTES 
  10.     File Name  : Set-ISEThemeDefault.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell Version 2.0 (ISE only) 
  13. .LINK 
  14.     This script posted to: 
  15.         http://bit.ly/gJpu2W  
  16. .EXAMPLE 
  17.     This script when run just resets the theme to the default. To view, run from 
  18.     the ISE after changing options! 
  19. #> 
  20.  
  21. # Set Basic options 
  22. $psise.options.SelectedScriptPaneState = $psise.options.DefaultOptions.SelectedScriptPaneState 
  23. $psise.options.ShowToolBar             = $psise.options.DefaultOptions.ShowToolBar 
  24. $psise.options.FontSize                = $psise.options.DefaultOptions.FontSize 
  25. $psise.options.Fontname                = $psise.options.DefaultOptions.Fontname 
  26.  
  27. # set colouring 
  28. $psise.options.ErrorForegroundColor          = $psise.options.DefaultOptions.ErrorForegroundColor 
  29. $psise.options.ErrorBackgroundColor          = $psise.options.DefaultOptions.ErrorBackgroundColor 
  30. $psise.options.WarningForegroundColor        = $psise.options.DefaultOptions.WarningForegroundColor 
  31. $psise.options.WarningBackgroundColor        = $psise.options.DefaultOptions.WarningBackgroundColor 
  32. $psise.options.VerboseForegroundColor        = $psise.options.DefaultOptions.VerboseForegroundColor 
  33. $psise.options.VerboseBackgroundColor        = $psise.options.DefaultOptions.VerboseBackgroundColor 
  34. $psise.options.DebugBackgroundColor          = $psise.options.DefaultOptions.DebugBackgroundColor 
  35. $psise.options.DebugForegroundColor          = $psise.options.DefaultOptions.DebugForegroundColor 
  36. $psise.options.OutputPaneBackgroundColor     = $psise.options.DefaultOptions.OutputPaneBackgroundColor 
  37. $psise.options.OutputPaneTextBackgroundColor = $psise.options.DefaultOptions.OutputPaneTextBackgroundColor 
  38. $psise.options.OutputPaneForegroundColor     = $psise.options.DefaultOptions.OutPutPaneForegroundColor 
  39. $psise.options.CommandPaneBackgroundColor    = $psise.options.DefaultOptions.CommandPaneBackgroundColor 
  40. $psise.options.ScriptPaneBackgroundColor     = $psise.options.DefaultOptions.ScriptPaneBackgroundColor  
  41. $psise.options.ScriptPaneForegroundColor     = $psise.options.DefaultOptions.ScriptPaneForegroundColor  
  42.  
  43. # More options 
  44. $psise.options.ShowWarningForDuplicateFiles  = $psise.options.DefaultOptions.ShowWarningForDuplicateFiles   
  45. $psise.options.ShowWarningBeforeSavingOnRun  = $psise.options.DefaultOptions.ShowWarningBeforeSavingOnRun 
  46. $psise.options.UseLocalHelp                  = $psise.options.DefaultOpitons.UseLocalHelp 
  47. $psise.options.CommandPaneUp                 = $psise.options.DefaultOptions.CommandPaneUp 
  48.  
  49. # Reset Tokens Colors 
  50. $psISE.Options.TokenColors['Attribute']          = $psISE.Options.DefaultOptions.TokenColors['Attribute'] 
  51. $psISE.Options.TokenColors['Command']            = $psISE.Options.DefaultOptions.TokenColors['Command'] 
  52. $psISE.Options.TokenColors['CommandArgument']    = $psISE.Options.DefaultOPtions.TokenColors['CommandArgument'] 
  53. $psISE.Options.TokenColors['CommandParameter']   = $psISE.Options.DefaultOptions.TokenColors['CommandParameter'] 
  54. $psISE.Options.TokenColors['Comment']            = $psISE.Options.DefaultOptions.TokenColors['Comment'] 
  55. $psISE.Options.TokenColors['GroupEnd']           = $psISE.Options.DefaultOptions.TokenColors['GroupEnd'] 
  56. $psISE.Options.TokenColors['GroupStart']         = $psISE.Options.DefaultOptions.TokenColors['GroupStart'] 
  57. $psISE.Options.TokenColors['Keyword']            = $psISE.Options.DefaultOptions.TokenColors['Keyword'] 
  58. $psISE.Options.TokenColors['LineContinuation']   = $psISE.Options.DefaultOptions.TokenColors['LineContinuation'] 
  59. $psISE.Options.TokenColors['LoopLabel']          = $psISE.Options.DefaultOptions.TokenColors['LoopLabel'] 
  60. $psISE.Options.TokenColors['Member']             = $psISE.Options.DefaultOptions.TokenColors['Member'] 
  61. $psISE.Options.TokenColors['NewLine']            = $psISE.Options.DefaultOptions.TokenColors['NewLine'] 
  62. $psISE.Options.TokenColors['Number']             = $psISE.Options.DefaultOPtions.TokenColors['Number'] 
  63. $psISE.Options.TokenColors['Position']           = $psISE.Options.DefaultOptions.TokenColors['Position'] 
  64. $psISE.Options.TokenColors['StatementSeparator'] = $psISE.Options.DefaultOptions.TokenColors['StatementSeparator'] 
  65. $psISE.Options.TokenColors['String']             = $psISE.Options.DefaultOptions.TokenColors['String'] 
  66. $psISE.Options.TokenColors['Type']               = $psISE.Options.DefaultOptions.TokenColors['Type']  
  67. $psISE.Options.TokenColors['Unknown']            = $psISE.Options.DefaultOptions.TokenColors['Unknown'] 
  68. $psISE.Options.TokenColors['Variable']           = $psISE.Options.DefaultOptions.TokenColors['Variable'] 
  69.  
  70. # Done 

Thursday, 7 October 2010

Get-WMINameSpace.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     This script displays all the WMI namespaces within a Windows system 
  4. .DESCRIPTION 
  5.     This script uses Get-WMIObject to retrieve the names of all the namespaces 
  6.     within a system. 
  7. .NOTES 
  8.     File Name  : Get-WMINameSpace.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/10/get-wminamespaceps1.html
  14. .EXAMPLE 
  15.     PSH [C:\foo]: .\Get-WMINameSpace.ps1 
  16.     37 Namespaces on: Cookham8 
  17.  
  18.     Namespace 
  19.     --------- 
  20.     ROOT 
  21.     ROOT\aspnet 
  22.     ROOT\CIMV2 
  23.     ROOT\CIMV2\Security 
  24.     ROOT\CIMV2\Security\MicrosoftTpm 
  25.     ... {Remainder of list snipped to save space on this page} 
  26. #> 
  27.  
  28. # Set computer name
  29. $comp = "." 
  30. # Get the name spaces on the local computer, and the local computer name 
  31. $Namespace = get-wmiobject __namespace -namespace 'root' -list -recurse -computer $comp  
  32. $hostname = hostname 
  33.  
  34. # Display number of and names of the namespaces 
  35. "{0} Namespaces on: {1}" -f $namespace.count, $hostname 
  36. $NameSpace| sort __namespace  | Format-Table @{Expression = "__Namespace"; Label = "Namespace"
Technorati Tags: ,,,

Wednesday, 6 October 2010

Get-LoopBack.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script checks whether a parameter is a Loopback Address  
  4. .DESCRIPTION 
  5.     This script checks to see if the passsed string is an IPV4
  6.     or an IPv6 loopback address and if so, displays details.
  7. .NOTES 
  8.     File Name  : Get-LoopBack.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell Version 2.0 
  11. .LINK 
  12.     This script posted to: 
  13.         http://www.pshscripts.blogspot.com  
  14.     MSDN sample posted to: 
  15.         http://msdn.microsoft.com/en-us/library/system.net.ipaddress.isloopback.aspx  
  16. .EXAMPLE 
  17.     PSH [C:\foo]: .\Get-LoopBack.ps1 
  18.     Your input address: \127.0.0.1\ is an IPv4 loopback address whose internal format is: 127.0.0.1. 
  19. .EXAMPLE 
  20.     PSH [C:\foo]: .\Get-LoopBack.ps1 ::1 
  21.     Your input address: \::1\ is an IPv6 loopback address whose internal format is: ::1. 
  22. .EXAMPLE 
  23.     PSH [C:\foo]: .\Get-LoopBack.ps1 131.107.2.200 
  24.     Your input address: \131.107.2.200\ is not a loopback address. 
  25. .PARAM 
  26.     $IPAddress - Address to look up to see if it's Loopback 
  27. #> 
  28. param
  29. [String] $IpAddress = "127.0.0.1" 
  30. )  
  31. # Setup Default answer! 
  32. $loopBack=" is not a loopback address."
  33.  
  34. # Perform syntax check by parsing the address string entered by the user. 
  35. $Address = [System.Net.IPAddress]::Parse($IpAddress); 
  36.  
  37. # Perform semantic check by verifying that the address is a valid IPv4  
  38. # or IPv6 loopback address.  
  39. if([System.Net.IPAddress]::IsLoopback($Address) -and ($address.AddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetworkV6) ) { 
  40.         $loopBack" is an IPv6 loopback address "
  41.                     "whose internal format is: " + $Address.ToString() + "."
  42. }            

Tuesday, 5 October 2010

Get-HostByName.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script gets and displays basic DNS Information about a host. 
  4. .DESCRIPTION 
  5.     This script just gets and displays host details returnd by GetHostByName. 
  6. .NOTES 
  7.     File Name  : Get-ByName.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://www.pshscripts.blogspot.com 
  13.     MSDN sample posted tot: 
  14.         http://msdn.microsoft.com/en-us/library/system.net.dns.aspx 
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\Get-HostByName.ps1 
  17.  
  18.     HostName    : contoso.com 
  19.     Aliases     : {www.contoso.com} 
  20.     AddressList : {207.46.197.32, 207.46.232.182} 
  21. #> 
  22. $hostInfo = [system.net.Dns]::GetHostByName("www.contoso.com"); 
  23. $hostinfo | fl * -force 

Monday, 4 October 2010

Remove-FtpFile.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script deletes a file from an FTP Server 
  4. .DESCRIPTION 
  5.     This script is a rewrite of an MSDN Sample 
  6. .NOTES 
  7.     File Name  : Remove-FtpFile.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://www.pshscripts.blogspot.com 
  13.     MSDN sample posted tot: 
  14.         http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx 
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\Remove-FtpFile.ps1 
  17.     Delete status: 250 DELE command successful. 
  18. #> 
  19. $ServerUri = New-Object System.Uri "ftp://www.reskit.net/powershell/foo.txt" 
  20. # The serverUri parameter should use the ftp:// scheme. 
  21. # It contains the name of the server file that is to be deleted. 
  22. # Example: ftp://contoso.com/someFile.txt. 
  23.  
  24. if ($ServerUri.Scheme -ne [system.Uri]::UriSchemeFtp) { 
  25.         " Bad URI"; return 
  26.  
  27. # Get the object used to communicate with the server. 
  28. $request = [system.Net.FtpWebRequest]::Create($serverUri
  29. $request.Method = [System.Net.WebRequestMethods+ftp]::Deletefile 
  30. $Request.Credentials = New-Object System.Net.NetworkCredential "anonymous","tfl@psp.co.uk" 
  31.  
  32. $response = $request.GetResponse() 
  33. "Delete status: {0}" -f $response.StatusDescription 
  34. $response.Close(); 

Sunday, 3 October 2010

Copy-FileToFtp.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     This script Uploads a text file to an FTP Server using PowerShell.  
  4. .DESCRIPTION 
  5.     This script first creates an FTP 'web' request to upload a file. Then the  
  6.     source file is read from disk and written up to the FTP Server. A response 
  7.     is then displayed. This is a rewrite of an MSDN Sample. 
  8. .NOTES 
  9.     File Name  : Copy-FileToFtp.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/10/copy-filetoftpps1.html
  15.     MSDN sample posted tot: 
  16.         http://msdn.microsoft.com/en-us/library/ms229715.aspx
  17. .EXAMPLE 
  18.     PSH [C:\foo]: .Copy-FileToFtp.ps1 
  19.     Upload File Complete, status 226
  20.     226 Transfer complete. 
  21. #> 
  22.   
  23. # Get the object used to communicate with the server. 
  24. $Request = [System.Net.FtpWebRequest]::Create("ftp://www.reskit.net/powershell/Greetings.Txt"
  25. $Request.Method = $Request.Method = [System.Net.WebRequestMethods+ftp]::UploadFile 
  26.  
  27. # This example assumes the FTP site uses anonymous logon. 
  28. $Request.Credentials = New-Object System.Net.NetworkCredential "anonymous","tfl@psp.co.uk" 
  29.  
  30. # Copy the contents of the file to the request stream. 
  31. $FileContents = [System.IO.File]::ReadAllBytes("C:\foo\scriptlib.zip"
  32. $Request.ContentLength = $fileContents.Length
  33. $RequestStream = $request.GetRequestStream() 
  34. $RequestStream.Write($FileContents, 0, $FileContents.Length) 
  35. $RequestStream.Close()
  36. $Response = $Request.GetResponse() 
  37. "Upload File Complete, status {0}" -f $Response.StatusDescription 
  38. $Response.Close()