Thursday, 14 November 2013

Fix-FileName.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Defines a function to remove 'invalid' characters 
  4.     from a file name. 
  5. .DESCRIPTION 
  6.     Some programs do not like certain 'invalid' characters 
  7.     in a file name used by that application. The function 
  8.     takes a look at each the file name and replaces some invalid 
  9.     characters with '-'
  10.  
  11.     This function takes a file name and 'fixes' it and returns 
  12.     the 'fixed' file name. Needless to say the characters to match 
  13.     and what to replace them with is an application specific decision! 
  14. .NOTES 
  15.     File Name  : Fix-FileName.ps1 
  16.     Author     : Thomas Lee - tfl@psp.co.uk 
  17.     Requires   : PowerShell Version 3.0 
  18. .LINK 
  19.     This script posted to: 
  20.         http://www.pshscripts.blogspot.com 
  21. .EXAMPLE 
  22.     Psh> .\Fix-FileName.ps1 
  23.     File name was: 123{}{{{|[\] 
  24.     Fixed name is: 123-------- 
  25.  
  26. #> 
  27.  
  28.  
  29. Function Fix-FileName { 
  30. [CMdletbinding()] 
  31. Param ( 
  32. $fn = $(throw 'no file name specified - returning'
  33.  
  34. Switch -Regex ($fn) { 
  35.   "}"  { $fn = $fn -replace '{','-'  } 
  36.   "}"  { $fn = $fn -replace '}','-'  } 
  37.   "\]" { $fn = $fn -replace ']','-'  } 
  38.   "\[" { $fn = $fn -replace '\[','-'
  39.   "\\" { $fn = $fn -replace '\\','-' } 
  40.   "\|" { $fn = $fn -replace '\|','-' } 
  41. } 
  42. $fn 
  43. } 
  44.  
  45. $fn = "123{}{{{|[\]" 
  46. $fnf = Fix-FileName $fn 
  47. "File name was: $fn" 
  48. "Fixed name is: $fnf

Wednesday, 16 October 2013

Get-SQLServer2.ps1

  1. #Requires -Version 3.0 
  2. <# 
  3. .SYNOPSIS 
  4.     This script Gets a list of SQL Severs on the Subnet 
  5. .DESCRIPTION 
  6.     This script uses SMO to Find all the local SQL Servers  
  7.     and displays them 
  8.  
  9. .NOTES 
  10.     File Name  : Get-SQLServer2.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell Version 3.0 
  13. .LINK 
  14.     This script posted to: 
  15.         http://www.pshscripts.blogspot.com 
  16. .EXAMPLE 
  17.     PS>  # On a Lync Server looking at Lync Implementation 
  18.     PS>  Get-SQLServer2 
  19.     There are 7 SQL Server(s) on the Local Subnet 
  20.  
  21.     ServerName      InstanceName Version      
  22.     ----------      ------------ -------      
  23.     2013-LYNC-MGT   MON          10.50.2500.0 
  24.     2013-LYNC-MGT   SCOM         10.50.2500.0 
  25.     2013-TS         RTCLOCAL     11.0.2100.60 
  26.     2013-SHAREPOINT SPSDB        11.0.3000.0  
  27.     2013-LYNC-FE    RTC          11.0.2100.60 
  28.     2013-LYNC-FE    RTCLOCAL     11.0.2100.60 
  29.     2013-LYNC-FE    LYNCLOCAL    11.0.2100.60 
  30.      
  31. #> 
  32. Import-Module SQLPS 
  33.  
  34. # Now get all the database servers on the local subnet 
  35.  
  36. $SQLservers = [System.Data.Sql.SqlDataSourceEnumerator]::Instance.GetDataSources() 
  37. $Srvs= @() 
  38.  
  39. # Convert collection to an array 
  40. Foreach ($srv in $SQLservers) { 
  41. $srvs += $srv 
  42.  
  43. # Now display results 
  44. If ($Srvs.count -LE 0) { 
  45. "There are no SQL Servers on the Local Subnet" 
  46. return
  47.  
  48. # Now print server details 
  49. "There are {0} SQL Server(s) on the Local Subnet" -f $Srvs.count 
  50. $Srvs | Select ServerName, InstanceName, Version | Format-Table -AutoSize 

Saturday, 12 October 2013

Get-OLCalendarItem

  1. Function Get-OLCalendarItem { 
  2.  
  3.  
  4. <# 
  5. .SYNOPSIS 
  6.     A function to retreive Outlook Calender items between two dates.  
  7.     Returns PSobjects containing each item. 
  8. .DESCRIPTION 
  9.     The function opens one's outlook calender, then retrieves the items.  
  10.     The function takes 2 parameter: start, end - items are returned that  
  11.     start betweween these two dates. 
  12. .NOTES 
  13.     File Name  : Get-OLCalendarItem 
  14.     Author     : Thomas Lee - tfl@psp.co.uk 
  15.     Requires   : PowerShell Version 3.0 
  16. .LINK 
  17.     This script posted to: 
  18.         http://www.pshscripts.blogspot.com 
  19.      
  20. .EXAMPLE 
  21.     Left as an exercise for the reader      
  22.  
  23. #> 
  24.  
  25. [CmdletBinding()] 
  26. Param ( 
  27. $start = $(Get-Date) , 
  28. $end   = $((Get-date).AddMonths(1)) 
  29.  
  30. Write-Verbose "Returning objects between: $($start.tostring()) and $($end.tostring())" 
  31. # Load Outlook interop and Outlook iteslf 
  32. [Reflection.Assembly]::LoadWithPartialname("Microsoft.Office.Interop.Outlook") | out-null 
  33. $Outlook = new-object -comobject outlook.application 
  34.  
  35. # Get OL default folders 
  36. $OlFolders = "Microsoft.Office.Interop.Outlook.OlDefaultFolders" -as [type] 
  37. $Namespace = $Outlook.GetNameSpace("MAPI"
  38. $Calendar = $namespace.GetDefaultFolder($olFolders::olFolderCalendar) 
  39. Write-Verbose "There are $($calendar.items.count) items in the calender in total" 
  40.  
  41. # Now return psobjects for all items between 2 dates 
  42. ForEach ($citem in ($Calendar.Items | sort start)) { 
  43. #Write-Verbose "Processing [$($citem.Subject)]  Starting: [$($Citem.Start)]" 
  44.  
  45. If ($citem.start -ge $start -and $citem.start -LE $end) {  
  46.  
  47. $CalHT =[ordered]  @{ 
  48. Subject          =  $($Citem.Subject) 
  49. Location         =  $($Citem.Location); 
  50. Start            =  $(Get-Date $Citem.Start); 
  51. StartUTC         =  $(Get-Date $Citem.StartUTC);                                   
  52. End              =  $(Get-Date $Citem.End); 
  53. EndUTC           =  $(Get-Date $Citem.EndUTC); 
  54. Duration         =  $($Citem.Duration); 
  55. Importance       =  $($Citem.Importance); 
  56. IsRecurring      =  $($Citem.IsRecurring); 
  57. AllDayEvent      =  $($citem.AllDayEvent); 
  58. Sensitivity      =  $($Citem.Sensitivity); 
  59. ReminderSet      =  $($Citem.ReminderSet); 
  60. CreationTime     =  $($Citem.CreationTime); 
  61. LastModificationTime = $($Citem.LastModificationTime); 
  62. Body             =  $($Citem.Body); 
  63.  
  64.  
  65. # Write the item out as a custom item 
  66. $o=New-Object PSobject -Property $CalHT 
  67. Write-Output $o 
  68.  
  69. } #End of foreach 
  70.  
  71. }  # End of function 
  72.  
  73. Set-Alias GCALI Get-OLCalendarItem  
Technorati Tags: ,

Wednesday, 28 August 2013

Get-DHCPPerf.Ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script gets and displays perf counters for DHCP 
  4. .DESCRIPTION 
  5.     This script uses the Get-Counter cmdlet to get all 
  6.     the counters for DHCP. The function does this for one 
  7.     DHCP server at a time. 
  8. .NOTES 
  9.     File Name  : Show-.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 2.0 
  12. .LINK 
  13.     This script posted to: 
  14.         http://www.pshscripts.blogspot.com 
  15. .EXAMPLE 
  16.     C:\Foo> Get-DHCPPerformanceCounters -comp dhcp2 
  17.      Performance counters for: dhcp2 
  18.      \\dhcp1\dhcp server\failover: bndupd dropped.                                           0 
  19.      \\dhcp1\dhcp server\failover: transitions to recover state.                             8 
  20.      \\dhcp1\dhcp server\failover: transitions to partner-down state.                        0 
  21.      \\dhcp1\dhcp server\failover: transitions to communication-interrupted state.           9 
  22.      \\dhcp1\dhcp server\failover: bndupd pending in outbound queue.                         0 
  23.      \\dhcp1\dhcp server\failover: bndack received/sec.                                      0 
  24.      \\dhcp1\dhcp server\failover: bndack sent/sec.                                          0 
  25.      \\dhcp1\dhcp server\failover: bndupd received/sec.                                      0 
  26.      \\dhcp1\dhcp server\failover: bndupd sent/sec.                                          0 
  27.      \\dhcp1\dhcp server\denied due to nonmatch.                                             0 
  28.      \\dhcp1\dhcp server\denied due to match.                                                0 
  29.      \\dhcp1\dhcp server\offer queue length                                                  0 
  30.      \\dhcp1\dhcp server\releases/sec                                                        0 
  31.      \\dhcp1\dhcp server\declines/sec                                                        0 
  32.      \\dhcp1\dhcp server\nacks/sec                                                           0 
  33.      \\dhcp1\dhcp server\acks/sec                                                            0 
  34.      \\dhcp1\dhcp server\informs/sec                                                         0 
  35.      \\dhcp1\dhcp server\requests/sec                                                        0 
  36.      \\dhcp1\dhcp server\offers/sec                                                          0 
  37.      \\dhcp1\dhcp server\discovers/sec                                                       0 
  38.      \\dhcp1\dhcp server\conflict check queue length                                         0 
  39.      \\dhcp1\dhcp server\active queue length                                                 0 
  40.      \\dhcp1\dhcp server\milliseconds per packet (avg).                                     28 
  41.      \\dhcp1\dhcp server\packets expired/sec                                                 0 
  42.      \\dhcp1\dhcp server\duplicates dropped/sec                                              0 
  43. #> 
  44.  
  45.  
  46.  
  47.  
  48. # Get-DHCPPerformanceCounters function 
  49.  
  50. Function Get-DHCPPerformanceCounters { 
  51. Param ( 
  52. $comp = 'localhost'
  53.  
  54. # Get DHCP Counters 
  55. $set = get-counter -listset "DHCP Server" 
  56. $ctrs = $set.counter 
  57.  
  58. " Performance counters for: {0}" -f $comp 
  59. # For each counter, get the sample 
  60. foreach ($ctr in $ctrs) { 
  61.  $sample = (get-counter $ctr).countersamples 
  62.  
  63.  
  64.  "{0,-78} {1,10}" -f $sample.path, $sample.cookedvalue 
  65.  
  66.  
  67.  
  68. # Now test it 
  69. Get-DHCPPerformanceCounters -comp dhcp1 
  70. Get-DHCPPerformanceCounters -comp dhcp2 
Technorati Tags:

Monday, 29 July 2013

Get-Zip.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script demonstrates the use of the Zip lib in .NET 
  4. .DESCRIPTION 
  5.     This script is a re-write of an MSDN sample, using PowerShell 
  6. .NOTES 
  7.     File Name  : Show-ZIP.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 3.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://www.pshscripts.blogspot.com 
  13.     MSDN sample posted to: 
  14.          http://msdn.microsoft.com/en-us/library/system.io.compression.zipfile.aspx 
  15. .EXAMPLE 
  16.     PSH> .\Show-Zip 
  17.      
  18.     Directory: C:\example 
  19. Mode        LastWriteTime  Length Name                                                                                                                            
  20. ----        -------------  ------ ----                                                                                                                            
  21. d---- 7/29/2013   4:35 PM        extract                                                                                                                         
  22. d---- 7/29/2013   4:29 PM        start                                                                                                                           
  23. -a--- 7/29/2013   4:35 PM   1668 result.zip                                                                                                                      
  24.  
  25.  
  26.     Directory: C:\example\extract 
  27. Mode        LastWriteTime  Length Name                                                                                                                            
  28. ----        -------------  ------ ----                                                                                                                            
  29. -a---  7/29/2013  4:28 PM    5609 d1.txt                                                                                                                          
  30. -a---  7/29/2013  4:29 PM   67308 d2.txt                                                                                                                          
  31. -a---  7/29/2013  4:29 PM   67308 d3.txt                                                                                                                          
  32. -a---  7/29/2013  4:29 PM   67308 d4.txt                                                                                                                          
  33. -a---  7/29/2013  4:29 PM   67308 d5.txt                                                                                                                          
  34.  
  35.  
  36.     Directory: C:\example\start 
  37. Mode        LastWriteTime     Length Name                                                                                                                            
  38. ----        ------------     ------ ----                                                                                                                            
  39. -a---  7/29/2013  4:28 PM    5609 d1.txt                                                                                                                          
  40. -a---  7/29/2013  4:29 PM   67308 d2.txt                                                                                                                          
  41. -a---  7/29/2013  4:29 PM   67308 d3.txt                                                                                                                          
  42. -a---  7/29/2013  4:29 PM   67308 d4.txt                                                                                                                          
  43. -a---  7/29/2013  4:29 PM   67308 d5.txt                                                                                                                          
  44. #> 
  45.  
  46. # Load the compression namespace  
  47. # And yes, I know this usage is obsolete - but it works.  
  48. # Ignore the output  
  49. [System.Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') | out-null 
  50.  
  51. # Set locations 
  52. $startPath = "c:\example\start" 
  53. $zipPath = "c:\example\result.zip" 
  54. $extractPath = "c:\example\extract" 
  55. Remove-Item $zipPath -ea SilentlyContinue 
  56. Remove-Item -Path $extractPath -inc * -Recurse -ea SilentlyContinue 
  57.  
  58. # Create the zip file 
  59. [System.IO.Compression.ZipFile]::CreateFromDirectory($startPath, $zipPath
  60.  
  61. # Extract from zip and show what's all there 
  62. [System.IO.Compression.ZipFile]::ExtractToDirectory($zipPath,$extractPath); 
  63. ls c:\example -Recurse 
  64.          

Sunday, 28 July 2013

Show-NumberGroupSizes.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script demonstrates the use of the NumberGroupSizes 
  4. .DESCRIPTION 
  5.     This script is a re-write of an MSDN sample, using PowerShell 
  6. .NOTES 
  7.     File Name  : Show-NumberGroupSizes.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 3.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://www.pshscripts.blogspot.com 
  13.     MSDN sample posted to: 
  14.          http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.numbergroupsizes%28v=vs.100%29.aspx 
  15. .EXAMPLE 
  16.     PSH> .\Show-NumberGroupSizes.ps1 
  17.     123,456,789,012,345.00 
  18.     12,3456,7890,123,45.00 
  19.     1234567890,123,45.00 
  20. #> 
  21.  
  22. # Get Number Format 
  23. $nf  = New-Object System.Globalization.CultureInfo  "en-US", $False  
  24. $nfi = $nf.NumberFormat 
  25.  
  26. [Int64] $myInt = 123456789012345 
  27. $myInt.ToString( "N", $nfi
  28.  
  29. # Display the same value with different groupings 
  30. [int[]] $MySizes1 = 2,3,4 
  31. [int[]] $MySizes2 = 2,3,0 
  32.  
  33. $nfi.NumberGroupSizes = $mySizes1 
  34. $myInt.ToString( "N",$nfi
  35. $nfi.NumberGroupSizes = $mySizes2 
  36. $myInt.ToString( "N", $nfi )  

Friday, 28 June 2013

Show-CurrencyFormatting.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script re-implements an MSDN Sample showing the  
  4.     use of the NumberFormat class to nicely format things 
  5.     in this case, currency. 
  6. .DESCRIPTION 
  7.     This script iterates through the Windows cultures and 
  8.     displays those whose 2-letter ISO code is 'en' and  
  9.     displays how Windows formats currency in that culture.  
  10. .NOTES 
  11.     File Name  : Show-CurrencyFormatting.ps1 
  12.     Author     : Thomas Lee - tfl@psp.co.uk 
  13.     Requires   : PowerShell Version 2.0 
  14. .LINK 
  15.     This script posted to: 
  16.         http://www.pshscripts.blogspot.com 
  17.     MSDN sample posted to: 
  18.         http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.aspx 
  19. .EXAMPLE 
  20.     Psh > .\Show-CurrencyFormatting.ps1 
  21.     The currency symbol for 'English (United States)' is '$' 
  22.     The currency symbol for 'English (United Kingdom)' is '£' 
  23.     The currency symbol for 'English (Australia)' is '$' 
  24.     The currency symbol for 'English (Canada)' is '$' 
  25.     The currency symbol for 'English (New Zealand)' is '$' 
  26.     The currency symbol for 'English (Ireland)' is '€' 
  27.     The currency symbol for 'English (South Africa)' is 'R' 
  28.     The currency symbol for 'English (Jamaica)' is 'J$' 
  29.     The currency symbol for 'English (Caribbean)' is '$' 
  30.     The currency symbol for 'English (Belize)' is 'BZ$' 
  31.     The currency symbol for 'English (Trinidad and Tobago)' is 'TT$' 
  32.     The currency symbol for 'English (Zimbabwe)' is 'Z$' 
  33.     The currency symbol for 'English (Republic of the Philippines)' is 'Php' 
  34.     The currency symbol for 'English (Singapore)' is '$' 
  35.     The currency symbol for 'English (Malaysia)' is 'RM' 
  36.     The currency symbol for 'English (India)' is 'Rs.' 
  37.  
  38. #> 
  39.  
  40. #  Loop through all the specific cultures known to the CLR. 
  41. foreach ($ci in [System.Globalization.CultureInfo]::GetCultures([System.Globalization.CultureTypes]::SpecificCultures))  
  42.    { 
  43.      # Only show the currency symbols for cultures that speak English. 
  44.      if ($ci.TwoLetterISOLanguageName -eq "en") { 
  45.      # Display the culture name and currency symbol. 
  46.         $nfi = $ci.NumberFormat 
  47.         "The currency symbol for '{0}' is '{1}'" -f $ci.DisplayName, $nfi.CurrencySymbol 
  48.      } 
  49.    } 

Thursday, 20 June 2013

Show-CurrencyGroupSize.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script reimplements a code sample from MSDN in PowerShell. 
  4.     This sample formats and display currency using standard and 
  5.     different currency groupings.  
  6. .DESCRIPTION 
  7.     This script displays a currency using standard, then two 
  8.     custom CurrencyGroupSizes. 
  9. .NOTES 
  10.     File Name  : Show-CurrencyGroupSize.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell Version 2.0 
  13. .LINK 
  14.     This script posted to: 
  15.         http://www.pshscripts.blogspot.com 
  16.     MSDN sample posted to: 
  17.         http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.currencygroupsizes.aspx 
  18. .EXAMPLE 
  19.     PSH:> .\Show-CurrencyGroupSize.ps1 
  20.     Default numeric format string "C" 
  21.     $123,456,789,012,345.00 
  22.  
  23.     Display with array = 2,3,4 
  24.     $12,3456,7890,123,45.00 
  25.  
  26.     Display with array = 2,3,0 
  27.     $1234567890,123,45.00 
  28. #> 
  29.  
  30. #     Get a NumberFormatInfo associated with the en-US culture. 
  31. $fi = new-object System.Globalization.CultureInfo "en-US", false 
  32. $nfi = $fi.NumberFormat 
  33.  
  34. #     Display a value with the default separator (".") 
  35. "Default numeric format string `"C`"" 
  36. [Int64] $myInt = 123456789012345 
  37. $myInt.ToString( "C", $nfi
  38.  
  39. #    Display the same value with different groupings. 
  40. [int[]] $mySizes1 = (2,3,4) 
  41. $mySizes = 2,3,0 
  42.  
  43. "";"Display with array = 2,3,4" 
  44. $nfi.CurrencyGroupSizes = $mySizes1 
  45. $myInt.ToString( "C", $nfi
  46.  
  47. "";"Display with array = 2,3,0" 
  48. $nfi.CurrencyGroupSizes = $mySizes2 
  49. $myInt.ToString( "C", $nfi

Thursday, 16 May 2013

Show-CurrencyDecimalDigits.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script demonstrates the use of the CurrencyDecimalDigits 
  4. .DESCRIPTION 
  5.     This script is a re-write of an MSDN sample, using PowerShell 
  6. .NOTES 
  7.     File Name  : Show-CurrencyDecimalDigits.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 3.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://www.pshscripts.blogspot.com 
  13.     MSDN sample posted to: 
  14.          http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.currencydecimaldigits%28v=vs.100%29.aspx 
  15. .EXAMPLE 
  16.     PSH> .\Show-CurrencyDecimalDigits.ps1 
  17.     ($1,234.00) 
  18.     ($1,234.0000) 
  19. #> 
  20.  
  21. # Get Number Format 
  22. $nf  = New-Object System.Globalization.CultureInfo  "en-US", $False  
  23. $nfi = $nf.NumberFormat 
  24.  
  25. # Display a negative value with the default number of decimal digits (2). 
  26. [Int64] $myInt = -1234 
  27. $myInt.ToString( "C", $nfi
  28.  
  29. # Displays the same value with four decimal digits. 
  30. $nfi.CurrencyDecimalDigits = 4 
  31. $myInt.ToString( "C", $nfi

Wednesday, 17 April 2013

Show-NumberDecimalSeparator.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script demonstrates the use of the NumberDecimalSeparator
  4. .DESCRIPTION 
  5.     This script is a re-write of an MSDN sample, using PowerShell 
  6. .NOTES 
  7.     File Name  : Show-NumberDecimaleparatort.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 3.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://www.pshscripts.blogspot.com 
  13.     MSDN sample posted to: 
  14.          http://msdn.microsoft.com/en-us/library/b74zyt45%28v=vs.100%29.aspx 
  15. .EXAMPLE 
  16.     PSH> .\Show-NumberDecimalSeparator.ps1 
  17.     123,456,789.00 
  18.     123,456,789 00 
  19. #> 
  20.  
  21. # Get Number Format 
  22. $nf  = New-Object System.Globalization.CultureInfo  "en-US", $False  
  23. $nfi = $nf.NumberFormat 
  24.  
  25. [Int64] $myInt = 123456789 
  26. $myInt.ToString( "N", $nfi
  27.  
  28. $nfi.NumberDecimalSeparator = " " 
  29. $myInt.ToString( "N", $nfi

Wednesday, 13 March 2013

Show-CurrencyGroupSeparator.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script demonstrates the use of the CurrencyGroupSeparator 
  4. .DESCRIPTION 
  5.     This script is a re-write of an MSDN sample, using PowerShell 
  6. .NOTES 
  7.     File Name  : Show-CurrencyGroupSeparator.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 3.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://www.pshscripts.blogspot.com 
  13.     MSDN sample posted to: 
  14.          http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.currencygroupseparator%28v=vs.100%29.aspx 
  15. .EXAMPLE 
  16.     PSH> .\Show-NumberGroupSizes.ps1 
  17.     $123,456,789,012,345.00 
  18.     $123 456 789 012 345.00 
  19. #> 
  20.  
  21. # Get Number Format 
  22. $nf  = New-Object System.Globalization.CultureInfo  "en-US", $False  
  23. $nfi = $nf.NumberFormat 
  24.  
  25. # Display a value with the default separator (","). 
  26. [Int64] $myInt = 123456789012345 
  27. $myInt.ToString( "C", $nfi
  28.  
  29. # Displays the same value with a blank as the separator. 
  30. $nfi.CurrencyGroupSeparator = " " 
  31. $myInt.ToString( "C", $nfi

Sunday, 24 February 2013

New-ZipFromDirectory.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     Creates a new zip file from an existing folder 
  4. .DESCRIPTION 
  5.     This script uses the .NET 4.5 zipfile class  
  6.     to create a zip file, getting contents from  
  7.     a folder. 
  8. .NOTES 
  9.     File Name  : New-ZipfromDirectory 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 3.0 and .NET 4.5 
  12. .LINK 
  13.     This script posted to: 
  14.         http://www.pshscripts.blogspot.com 
  15. .EXAMPLE 
  16.     Psh> C:\foo\new-zip.ps1 
  17.     Zip file created: 
  18.      
  19.     Directory: C:\foo 
  20.          
  21.     Mode                LastWriteTime     Length Name 
  22.     ----                -------------     ------ ---- 
  23.     -a---         2/24/2013   3:00 PM     291182 ScriptLib.ZIP 
  24.  
  25. #> 
  26.  
  27. # Load the compression namespace 
  28. # and yes, I know this usage is obsolete - but it works. 
  29. # Ignore the output 
  30. [System.Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') | Out-Null  
  31.  
  32. # Create a type accellerator for the zipfile class 
  33. [System.Type] $TypeAcceleratorsType=[System.Management.Automation.PSObject].Assembly.GetType('System.Management.Automation.TypeAccelerators',$True,$True) 
  34. $TypeAcceleratorsType::Add('Zipfile','System.IO.Compression.Zipfile'
  35.  
  36. # Now create a zip file 
  37. # Set target zip flie and source folder 
  38. $Folder  = 'E:\PowerShellScriptLib' 
  39. $Zipfile = 'C:\foo\ScriptLib.ZIP' 
  40.  
  41. # Ensure file does NOT exist and fodler DOES exist 
  42. If (Test-Path $zipfile -EA -0) { 
  43.    Throw "$Zipfile exists - not safe to continue"
  44. If (!(Test-Path $folder)) { 
  45.    Throw "$Folder does not seem to exist"
  46.     
  47. # Now create the Zip file 
  48. Try { 
  49.   [Zipfile]::CreateFromDirectory( $folder, $zipfile) 
  50.   "Zip file created:";ls $zipfile} 
  51. Catch { 
  52.   "Zip File NOT created" 
  53.   $Error[0]} 

Monday, 4 February 2013

Configure-DC1-2.ps1

  1. #### 
  2. # Configure-DC1-2 
  3. # Configures DC1 after dcpromo is completed 
  4. # 
  5. # Version    Date         What Changed 
  6. # -------    -----------  ------------------------------------------- 
  7. # 1.0.0      14 Jan 2013  Initial release 
  8. # 1.1.0      24 Jan 2013  Added code to count how long it all took, 
  9. #                         Added checkpoint at the end of this script 
  10. # 1.1.1      25 Jan 2013  Added auto admin logon 
  11. # 1.1.2      5  Feb 2013  Added forced reboot of DC1-1 at script end  
  12. #### 
  13.  
  14. #     Configuration block 
  15. $Conf = { 
  16.  
  17. $StartTime = Get-Date 
  18. Write-Host "Starting at: $StartTime" 
  19.  
  20. #    Set Credentials for use in this configuration block 
  21. $User       = "Reskit\Administrator" 
  22. $Password   = 'Pa$$w0rd' 
  23. $PasswordSS = ConvertTo-SecureString  -String $Password –AsPlainText `
  24.               -Force 
  25. $Dom        = 'Reskit' 
  26. $CredRK     = New-Object  `
  27.     -Typename System.Management.Automation.PSCredential  `
  28.      -Argumentlist $User,$PasswordSS 
  29.  
  30. #    Define registry path for autologon, then set admin logon 
  31. $RegPath  = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon' 
  32. Set-ItemProperty -Path  $RegPath -Name AutoAdminLogon   ` 
  33.                  -Value 1      -EA 0   
  34. Set-ItemProperty -Path  $RegPath -Name DefaultUserName   ` 
  35.                  -Value $User  -EA 0   
  36. Set-ItemProperty -Path  $RegPath -Name DefaultPassword   ` 
  37.                  -Value $Password -EA 0 
  38. Set-ItemProperty -Path  $RegPath -Name DefaultDomainName  ` 
  39.                  -Value $Dom -EA 0  
  40.  
  41. #    Install key Windows features for labs 
  42. Write-Verbose 'Installing Windows fetures needed for DC1' 
  43. $Features = @('PowerShell-ISE'
  44.               'Hyper-V-PowerShell'
  45.               'Rsat-AD-PowerShell',               
  46.               'Web-Server','Web-Mgmt-Tools'
  47.               'Web-Mgmt-Console'
  48.               'Web-Scripting-Tools'
  49.               'Telnet-Client'
  50. Install-WindowsFeature @Features -IncludeManagementTools -Verbose 
  51.  
  52. #    Install and configure DHCP 
  53. Write-Verbose -Message 'Adding and then configuring DHCP' 
  54. Install-WindowsFeature DHCP -IncludeManagementTools 
  55. Add-DhcpServerV4Scope -Name "ReskitNet0"
  56.                       -StartRange 10.0.0.100 ` 
  57.                       -EndRange 10.0.0.119 ` 
  58.                       -SubnetMask 255.255.255.0 
  59. Set-DhcpServerV4OptionValue -DnsDomain Reskit.Org ` 
  60.                             -DnsServer 10.0.0.10 
  61. Add-DhcpServerInDC -DnsName Dc1.reskit.org 
  62.  
  63. #    Add users to the AD and then add them to some groups 
  64. #    Hash table for common new user paraemters 
  65. Write-Verbose -Message 
  66. $NewUserHT  = @{AccountPassword       = $PasswordSS
  67.                 Enabled               = $true
  68.                 PasswordNeverExpires  = $true
  69.                 ChangePasswordAtLogon = $false 
  70.                 } 
  71.  
  72. #     Create one new user (me!) and add to enterprise and domain admins security groups 
  73. New-ADUser @NewUserHT -SamAccountName tfl ` 
  74.                       -UserPrincipalName 'tfl@reskit.org'
  75.                       -Name "tfl"
  76.                       -DisplayName 'Thomas Lee' 
  77. Add-ADPrincipalGroupMembership `
  78.        -Identity "CN=tfl,CN=Users,DC=reskit,DC=org"
  79.        -MemberOf "CN=Enterprise Admins,CN=Users,DC=reskit,DC=org"
  80.                  "CN=Domain Admins,CN=Users,DC=reskit,DC=org"  
  81.  
  82. #     Say nice things and finish 
  83. $FinishTime = Get-Date 
  84. Write-Verbose "Finished at: $FinishTime" 
  85. Write-Verbose "DC1 Configuration took $(($FinishTime - $StartTime).TotalSeconds.ToString('n2')) seconds" 
  86.  
  87. } # End Conf configuration script block 
  88.  
  89. #    Start of script proper 
  90.  
  91. #    Set Credentials 
  92. $Username   = "Reskit\administrator" 
  93. $Password   = 'Pa$$w0rd' 
  94. $PasswordSS = ConvertTo-SecureString  -String $Password -AsPlainText
  95.               -Force 
  96. $CredRK     = New-Object -Typename System.Management.Automation.PSCredential `
  97.         -Argumentlist $Username,$PasswordSS 
  98.  
  99. #    Following code used to test the credentials. Remove the comments on next two lines the first time you  
  100. #    run this script 
  101. Invoke-Command -ComputerName DC1 -ScriptBlock {hostname} -Credential $Credrk -verbose 
  102. Pause 
  103.  
  104. Invoke-Command -ComputerName DC1 -Scriptblock $conf -Credential $CredRK `
  105.                -Verbose 
  106.  
  107. #     OK - script block has completed - reboot the system and wait till it comes up 
  108. Restart-Computer -ComputerName DC1  -Wait -For PowerShell –Force
  109.                  -Credential $CredRK 
  110.   
  111. #    Finally, run a post-DCPromo snapshot 
  112. Checkpoint-VM -VM $(Get-VM DC1) `
  113. -SnapshotName "DC1 - post configuration by ConfigureDC1-2.ps1"