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. ""