Monday, 23 February 2009

New-CustomEventLog.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Creates a new Event Log 
  4. .DESCRIPTION 
  5.     This script first checks to see if a log called NewPSHLog exists. if not, it creates 
  6.     it then exits. if the log exists, the script writes an entry ot the log and then 
  7.     displays the log entries. A separate script clears and deletes the log! 
  8. .NOTES 
  9.     File Name  : New-CustomEventLog.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell V2 CTP3 
  12. .LINK 
  13.     Script posted to:
  14.     http://www.pshscripts.blogspot.com 
  15. .EXAMPLE 
  16.     First time the script runs: 
  17.     PSH [C:\foo]: .\New-CustomEventLog.ps1' 
  18.     CreatedEventSource 
  19.     Exiting, execute the script a second time to use the source. 
  20.      
  21.     Second time the script runs: 
  22.     PSH [C:\foo]: .\New-CustomEventLog.ps1' 
  23.     PowerShell Eventlog exists 
  24.  
  25.        Index Time          EntryType   Source      InstanceID Message 
  26.        ----- ----          ---------   ------      ---------- ------- 
  27.           12 Feb 22 16:11  Information NewPshLog            0 Writing to new PowerShell even... 
  28. #> 
  29.  
  30. ### 
  31. # Start of Script 
  32. ### 
  33.  
  34. if (![system.diagnostics.eventlog]::SourceExists("NewPSHLog"))  { 
  35.  
  36. # An event log source should not be created and immediately used. 
  37. # There is a latency time to enable the source, it should be created 
  38. # prior to executing the application that uses the source. 
  39. # So let's execute this sample a second time to use the new source. 
  40.             [system.diagnostics.EventLog]::CreateEventSource("MySource", "NewPSHLog"
  41.             "CreatedEventSource" 
  42.             "Exiting, execute the script a second time to use the source." 
  43. # The source is created.  Exit the application to allow it to be registered. 
  44.             return 
  45. else
  46. "NewPSHLog Eventlog exists" 
  47.  
  48. # With log created, create an EventLog instance and assign its source. 
  49. $mylog = new-object System.diagnostics.Eventlog 
  50. $myLog.Source = "NewPshLog" 
  51.  
  52. # Write an informational entry to the event log.     
  53. $myLog.WriteEntry("Writing to new PowerShell event log."
  54.  
  55. # Display log events 
  56. get-eventlog "NewPSHLog" 
  57. # End of Script 

Sunday, 22 February 2009

Get-MachineConfig

  1. <# 
  2. .SYNOPSIS 
  3.     Displays summary of machine.config 
  4. .DESCRIPTION 
  5.     This script is a re-write of an MSDN sample which fetchs the machine.config 
  6.     file, prints out file path, and key sections. Also shows how many sections 
  7.     exist in the machine.config file. Also fixed errors in original C# code 
  8.     and improved the layout of the results a bit. 
  9. .NOTES 
  10.     File Name  : get-machineconfig.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell V2 CTP3 
  13. .LINK 
  14.     Updated Powershell script posted to: 
  15.     http://pshscripts.blogspot.com/2009/02/get-machineconfig.html
  16.     MSDN Sample at: 
  17.     http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.openmachineconfiguration(VS.80).aspx 
  18. .EXAMPLE 
  19.     PSH [C:\foo]: .\get-machinefoncif.ps1 
  20.     File path: C:\Windows\Microsoft.NET\Framework\v2.0.50727\Config\machine.config 
  21.      
  22.        Name                                Allow Definition 
  23.        ----                                ---------------- 
  24.     system.data                        MachineToApplication 
  25.     windows                            MachineToApplication 
  26.     system.webServer                   MachineToApplication 
  27.     mscorlib                           MachineToApplication 
  28.     system.data.oledb                  MachineToApplication 
  29.     system.data.oracleclient           MachineToApplication 
  30.     system.data.sqlclient              MachineToApplication 
  31.     configProtectedData                MachineToApplication 
  32.     satelliteassemblies                MachineToApplication 
  33.     system.data.dataset                MachineToApplication 
  34.     startup                            MachineToApplication 
  35.     system.data.odbc                   MachineToApplication 
  36.     system.diagnostics                 MachineToApplication 
  37.     runtime                            MachineToApplication 
  38.     system.codedom                     MachineToApplication 
  39.     system.runtime.remoting            MachineToApplication 
  40.     connectionStrings                  MachineToApplication 
  41.     assemblyBinding                    MachineToApplication 
  42.     appSettings                        MachineToApplication 
  43.     system.windows.forms               MachineToApplication 
  44.     Total number of sections: 20 
  45. #> 
  46.  
  47. ### 
  48. #  Start of Script 
  49. # Get the machine.config file. 
  50. #### 
  51.  
  52. # Get config file 
  53. $config = [System.Configuration.ConfigurationManager]::OpenMachineConfiguration() 
  54.  
  55. #Display machine.config path. 
  56. "";"File path: {0}" -f $config.FilePath; "" 
  57.  
  58. # Loop to get the sections and display basic information. 
  59. "{0,-25}     {1,25}"  -f "   Name", "   Allow Definition" 
  60. "{0,-25}     {1,25}"  -f "   ----", "   ----------------" 
  61. $i = 0 
  62. foreach ($section in $config.Sections)  { 
  63. "{0,-25}     {1,25}" -f $section.SectionInformation.Name,$section.SectionInformation.AllowExeDefinition 
  64. $i++ 
  65. # Display total sections     
  66. "Total number of sections: {0}" -f $i  

Saturday, 7 February 2009

Disable-NetworkCard.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Disables active network cards 
  4. .DESCRIPTION 
  5.     This script looks at each network card that is currently IP enabled, and 
  6.     disables it by releasing the DHCP Lease. To re-enable the network interrace, 
  7.     you just run IPCONFIG /RENEW. 
  8.      
  9.     This script is an MSDN Sample, rewritten using PowerShell 
  10. .NOTES 
  11.     File Name  : Disable-NetworkCard.ps1 
  12.     Author     : Thomas Lee - tfl@psp.co.uk 
  13.     Requires   : PowerShell V2 CTP3 
  14. .LINK 
  15.     PowerShell script posted to: 
  16.        http://pshscripts.blogspot.com/2009/02/disable-networkcardps1.html
  17.     Original MSDN Sample at: 
  18.        http://msdn.microsoft.com/en-us/library/aa394595(VS.85).aspx 
  19. .EXAMPLE 
  20.     PSH [C:\foo]: .\Disable-NetworkCard.ps1 
  21.     Releasing lease on: [00000006] Broadcom NetXtreme Gigabit Ethernet 
  22.     Releasing lease on: [00000012] Microsoft Virtual Network switch Adapte 
  23. #> 
  24.    
  25. ### 
  26. #  Starting Script 
  27. ### 
  28.   
  29. $Computer = "." 
  30. $net = Get-WMIObject -class Win32_NetworkAdapterConfiguration -ComputerName $computer 
  31. $netenabled = $net | where {$_.IPenabled} 
  32. foreach ($NetCard in $netenabled) { 
  33.     "Releasing lease on: {0}" -f $netcard.caption 
  34.     $netcard.ReleaseDHCPLease() 
  35. # End of Script 

Wednesday, 4 February 2009

PowerShellPlus v2.1 Beta

This is sort of exciting - PowerShellPlus v2.1 Beta is live! I’ve used PowerShell Plus for some time and love it. But there are things that I’d like to see, in particular the CTP3 support. Well, now we have that – and I’m downloading it as I write this post. The features in 2.1 that catch my eye are: VBS support, STA MOde, Code Sharing and of course, CTP3 support. I’ll be posting more once I have the code running and have a chance to give it a good test!

Technorati Tags: ,,

Monday, 2 February 2009

Updates on A Grateful Dead PowerShell Script

I really love mixing two of my favourite things: the good old Grateful Deal and PowerShell. I’ve posted a couple of scripts that examine my live show archive. Yesterday, I was able to spend some time updating my Count-GDSHows script. In Count-GDShows2.ps1, I added a couple of features. First I worked out how many shows are recording using the SHN format and how many in FLAC (plus how many are not noted in the folder name). Second, I timed the script, although the timing varies widely thanks to disk caching!

My GD (and my Jerry Garcia) archive is stored on a couple of external USB disks. I have separated Jerry shows from GD shows and have both stored on disks plugged into separate machines. I use the folder name of each show to define the date, type (aud vs sbd) and encoding format. So a show in “gd69-04-22.sbd.clugston.68.sbeok.shnf” is a soundboard encoded in the Shorten format, for a show on April 22 1969. Were you even alive then?

I wonder how many of this blog’s readers collect Grateful Dead shows? I have a standing offer that I’ll repeat: If you want the shows in my collection, send me a big disk and I’ll xcopy!

Technorati Tags: ,

Sunday, 1 February 2009

Count-GDShows2.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Counts the Grateful Dead shows in my archives and displays 
  4.     details of the collection. 
  5. .DESCRIPTION 
  6.     This script looks at my GD archive, and uses folder names to 
  7.     determine show type (aud, sbd, etc), and checks to see what 
  8.     shows have checked MD5s. The script also times itself. 
  9.      
  10.     This is an update to an earlier script that adds separate  
  11.     counts for FLAC and SHN shows. 
  12. .NOTES 
  13.     File Name  : Count-GDShows2.ps1 
  14.     Author     : Thomas Lee - tfl@psp.co.uk 
  15.     Requires   : PowerShell V2 CTP3 
  16. .LINK 
  17.     http://pshscripts.blogspot.com/2009/02/count-gdshows2ps1.html
  18. .EXAMPLE 
  19.     PS C:\foo> Count-GDShows2.ps1 
  20.     Count.ps1 - v 2.0.2 
  21.     +---------------------------+ 
  22.     ! Dead Show Base  :  M:\gd  ! 
  23.     +---------------------------+ 
  24.  
  25.     Grateful Dead Show Summary 
  26.     -------------------------- 
  27.     Total shows:   1146 
  28.     Soundboards:   800 
  29.     Auds       :   70 
  30.     Unknown    :   29 
  31.     Partial    :   9 
  32.     SHN        :   962 
  33.     FLAC       :   137 
  34.     No coding  :   47 
  35.     Broken     :   4 
  36.     MD5's check:   489 (42.67 %) 
  37.     It took 0 minutes, 1 seconds 
  38.      
  39. #> 
  40. ### 
  41. # Start of Archive 
  42. ## 
  43.  
  44. # Constants: 
  45. # $GDDiskRoot      - where to find shows 
  46. # $DeadShowBase  - folder at top of gd shows 
  47.  
  48. $GDDiskRoot = "M:" 
  49. $DeadShowBase   = $GDDiskRoot + "\gd" 
  50.  
  51. # Announce Ourselves 
  52. "Count.ps1 - v 2.0.2" 
  53. "+---------------------------+" 
  54. "! Dead Show Base  :  $DeadShowBase  !" 
  55. "+---------------------------+" 
  56. "" 
  57. # Get start time 
  58. $starttime = Get-Date 
  59.  
  60. # Count the Dead shows 
  61.  
  62. $Dir= ls $DeadShowBase  | where {$_.psiscontainer} 
  63. $DeadShows=$Dir.count 
  64. if ($DeadSHows -le 0) {"no shows found - check constants"} 
  65.  
  66. #Create subsets based on names of the folders 
  67. $deadsbds   = $dir | where {$_.name -match ".sbd" } 
  68. $deadbrkn   = $dir | where {$_.name -match "broken" } 
  69. $deadpart   = $dir | where {$_.name -match "partial" } 
  70. $deadauds   = $dir | where {$_.name -match ".aud" } 
  71. $deadunkw   = $dir | where {$_.name -match ".unk"} 
  72. # workout recording type 
  73. $deadshn    = $dir | where {$_.name -match ".shn"} 
  74. $deadflac   = $dir | where {$_.name -match ".flac"} 
  75. $deadnocoding = $dir | where {$_.name -notmatch ".shn" -and $_.name -notmatch ".flac"} 
  76.  
  77. #and see how many have the md5ok's file? 
  78. $DeadMD5Checked=0 
  79. foreach ($d in $dir) 
  80. {  
  81.   $sn=$d.fullname + "\md5check_ok" 
  82.   $md5ok= ls $sn -ea silentlycontinue 
  83.   if ($md5ok )  
  84.      {$DeadMD5Checked++} 
  85.  
  86. # Get finishtime and calc elapsed 
  87. $finishtime=Get-Date 
  88. $executiontime = $finishtime - $starttime 
  89. $min = $executiontime.minutes 
  90. $sec = $executiontime.seconds 
  91.  
  92. #Display results 
  93.  
  94. "Grateful Dead Show Summary" 
  95. "--------------------------" 
  96. "Total shows:   $deadshows" 
  97. "Soundboards:   $($deadsbds.count)" 
  98. "Auds       :   $($deadauds.count)" 
  99. "Unknown    :   $($deadunkw.count)" 
  100. "Partial    :   $($deadpart.count)" 
  101. "SHN        :   $($deadshn.count)" 
  102. "FLAC       :   $($deadflac.count)" 
  103. "No coding  :   $($deadnocoding.count)" 
  104. "Broken     :   $($deadbrkn.count)" 
  105. $DeadPctChecked=($DeadMD5checked/$DeadShows).tostring("P"
  106. "MD5's check:   $DeadMD5checked ($DeadPctChecked)" 
  107. "It took {0} minutes, {1} second(s)" -f $min,$sec 
  108. "" 

Saturday, 31 January 2009

Count-GDShows.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Counts the Grateful Dead shows in my archives 
  4. .DESCRIPTION 
  5.     This script looks at my GD archive, and uses folder names to 
  6.     determine show type (aud, sbd, etc), and checks to see what 
  7.     shows have checked MD5s 
  8. .NOTES 
  9.     File Name  : count-gdshows.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell V2 CTP3 
  12. .LINK 
  13.     http://pshscripts.blogspot.com/2009/01/count-gdshowsps1.html 
  14. .EXAMPLE 
  15.     PS C:\foo> count-gdshows.ps1 
  16.     Count.ps1 - v 2.0.1 
  17.     +---------------------------+ 
  18.     ! Dead Show Base  :  M:\gd  ! 
  19.     +---------------------------+ 
  20.  
  21.     Grateful Dead Show Summary 
  22.     -------------------------- 
  23.     Total shows:   1146 
  24.     Soundboards:   800 
  25.     Auds       :   70 
  26.     Unknown    :   29 
  27.     Partial    :   9 
  28.     Broken     :   4 
  29.     MD5's check:   489 (42.67 %) 
  30. #> 
  31. ### 
  32. # Start of Archive 
  33. ## 
  34.  
  35. # Constants: 
  36. # $GDDiskRoot    - where to find shows 
  37. # $DeadShowBase  - folder at top of gd shows 
  38.  
  39. $GDDiskRoot     = "M:" 
  40. $DeadShowBase   = $GDDiskRoot + "\gd" 
  41.  
  42. # Announce Ourselves 
  43. "Count.ps1 - v 2.0.1" 
  44. "+---------------------------+" 
  45. "! Dead Show Base  :  $DeadShowBase  !" 
  46. "+---------------------------+" 
  47. "" 
  48.  
  49. # Count the Dead shows 
  50.  
  51. $Dir= ls $DeadShowBase  | where {$_.psiscontainer} 
  52. $DeadShows=$Dir.count 
  53. if ($DeadSHows -le 0) {"no shows found - check constants"} 
  54.  
  55. #Create subsets based on names of the folders 
  56. $deadsbds=$dir | where {$_.name -match ".sbd" } 
  57. $deadbrkn=$dir | where {$_.name -match "broken" } 
  58. $deadpart=$dir | where {$_.name -match "partial" } 
  59. $deadauds=$dir | where {$_.name -match ".aud" } 
  60. $deadunkw=$dir | where {$_.name -Match ".unk"} 
  61.  
  62. #and see how many have the md5ok's file? 
  63.  
  64. $DeadMD5Checked=0 
  65. foreach ($d in $dir) 
  66. {  
  67.   $sn=$d.fullname + "\md5check_ok" 
  68.   $md5ok= ls $sn -ea silentlycontinue 
  69.   if ($md5ok )  
  70.      {$DeadMD5Checked++} 
  71.  
  72. #Display results 
  73.  
  74. "Grateful Dead Show Summary" 
  75. "--------------------------" 
  76. "Total shows:   $deadshows" 
  77. "Soundboards:   $($deadsbds.count)" 
  78. "Auds       :   $($deadauds.count)" 
  79. "Unknown    :   $($deadunkw.count)" 
  80. "Partial    :   $($deadpart.count)" 
  81. "Broken     :   $($deadbrkn.count)" 
  82. $DeadPctChecked=($DeadMD5checked/$DeadShows).tostring("P"
  83. "MD5's check:   $DeadMD5checked ($DeadPctChecked)" 
  84. "" 

Thursday, 29 January 2009

Get-IPAddress.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     Gets and displays the IP address of a computer 
  4. .DESCRIPTION 
  5.     This script uses Win32_NetworkAdapterConfiguration to 
  6.     obtain, then display, a system's IP addresses. 
  7.     NB: this only works on XP or later versions of Windows. 
  8. .NOTES 
  9.     File Name : Get-IPAddress.ps1 
  10.     Author : Thomas Lee - tfl@psp.co.uk 
  11.     Requires : PowerShell V2 
  12. .LINK 
  13.     Script postesd to: 
  14.     http://www.pshscripts.blogspot.com 
  15.     MSDN Sample at: 
  16.     http://msdn.microsoft.com/en-us/library/aa394590(VS.85).aspx 
  17. .EXAMPLE 
  18.     [ps] c:\foo> .\Get-IPAddress.ps1 
  19.     IP Address   : 10.10.1.115 
  20.     IP Address   : fe80::3953:f67b:2f1c:1323 
  21.     IP Address   : 10.10.1.120 
  22.     IP Address   : fe80::d8ed:afe2:2a97:a596
  23.     4 IP addresses found on this system
  24. #> 
  25.    
  26. ## 
  27. # Start of Script 
  28. ## 
  29.    
  30. # Get Networking Adapter Configuration 
  31. $Computer = "." 
  32. $IPconfigset = Get-WmiObject Win32_NetworkAdapterConfiguration 
  33.    
  34. # Iterate and get IP address 
  35. $count = 0 
  36. foreach ($IPConfig in $IPConfigSet) { 
  37.    if ($Ipconfig.IPaddress) { 
  38.       foreach ($addr in $Ipconfig.Ipaddress) { 
  39.       "IP Address   : {0}" -f  $addr
  40.       $count++  
  41.       } 
  42.    } 
  43. if ($count -eq 0) {"No IP addresses found"
  44. else {"$Count IP addresses found on this system"
  45. #End of Script 

Wednesday, 28 January 2009

Get-TimeZoneInfo.ps1

  1. <#
  2. .SYNOPSIS
  3. Gets and displays time zone information via WMI
  4. .DESCRIPTION
  5. This script uses the Win32_TimeZone class to obtain, then
  6. display time zone information for a computer.
  7. This script is a MSDN sample recoded in PowerShell
  8. .NOTES
  9. File Name : Get-TimeZoneInfo.ps1
  10. Author : Thomas Lee - tfl@psp.co.uk
  11. Requires : PowerShell V2 CTP3
  12. .LINK
  13. Script posted to:
  14. http://www.pshscripts.blogspot.com/2009/01/get-timezoneinfops1.html
  15. MSDN Sample at:
  16. http://msdn.microsoft.com/en-us/library/aa394590(VS.85).aspx
  17. .EXAMPLE
  18. [ps] c:\foo> .\Get-TimeZoneInfo.ps1
  19. Time zone information on computer "Win7"
  20. Time Zone Description : (UTC) Dublin, Edinburgh, Lisbon, London
  21. Daylight Name : GMT Daylight Time
  22. Standard Name : GMT Standard Time
  23. #>

  24. ###
  25. # Start of Script
  26. ##

  27. # Get Time Zone on a computer
  28. $Computer = "."
  29. $timezone = Get-WMIObject -class Win32_TimeZone -ComputerName $computer

  30. # Display details
  31. if ($computer -eq ".") {$computer = Hostname}
  32. "Time zone information on computer `"{0}`"" -f $computer
  33. "Time Zone Description : {0}" -f $timezone.Description
  34. "Daylight Name : {0}" -f $timezone.DaylightName
  35. "Standard Name : {0}" -f $timezone.StandardName
  36. #End of Script

Monday, 26 January 2009

Get-WMILocalTime.ps1

  1. <# 
  2. .SYNOPSIS 
  3.   Gets time from WMI and displays it   
  4. .DESCRIPTION 
  5.    This script gets Win32_LocalTime and then displays  
  6.    the details. 
  7. .NOTES 
  8.     File Name  : Get-WMILocalTime.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell V2 CTP3 
  11. .LINK 
  12.     Script posted to:
  13.     http://pshscripts.blogspot.com/2009/01/get-wmilocaltimeps1.html
  14.     MSDN Sample at:
  15.     http://msdn.microsoft.com/en-us/library/aa394590(VS.85).aspx
  16. .EXAMPLE 
  17.     [ps] c:\foo> .\Get-WMILocaltimeps1 
  18.     Day          : 26 
  19.     Day Of Week  : 1 
  20.     Hour         : 20 
  21.     Minute       : 59 
  22.     Month        : 1 
  23.     Quarter      : 1 
  24.     Second       : 53 
  25.     Week In Month: 5 
  26.     Year         : 2009 
  27. #> 
  28.  
  29. ### 
  30. # Start of Script 
  31. ## 
  32.  
  33. # Speficy computer and get Local Time 
  34. $Computer = "." 
  35. $times = Get-WmiObject Win32_LocalTime -computer $computer 
  36.  
  37. # Now display the result 
  38.  
  39. Foreach ($time in $times) { 
  40. "Day          : {0}"  -f $Time.Day 
  41. "Day Of Week  : {0}"  -f $Time.DayOfWeek 
  42. "Hour         : {0}"  -f $Time.Hour 
  43. "Minute       : {0}"  -f $Time.Minute 
  44. "Month        : {0}"  -f $Time.Month 
  45. "Quarter      : {0}"  -f $Time.Quarter 
  46. "Second       : {0}"  -f $time.Second  
  47. "Week In Month: {0}"  -f $Time.WeekInMonth  
  48. "Year         : {0}"  -f $Time.Year  
  49. # End of Script