Showing posts with label Enum. Show all posts
Showing posts with label Enum. Show all posts

Wednesday, 17 June 2009

Get-ConformanceLevelEnum.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     This script demonstrates the confirmance level enum. 
  4. .DESCRIPTION 
  5.     This script displays the values form the conformancelevel enum 
  6.     and checks it against a value. 
  7. .NOTES 
  8.     File Name  : Get-ConformanceLevelEnum.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell V2 CTP3 
  11. .LINK 
  12.     This script posted to: 
  13.         http://pshscripts.blogspot.com 
  14.     MSDN Sample posted at: 
  15.         http://msdn.microsoft.com/en-us/library/h2344bs2(VS.85).aspx 
  16. .EXAMPLE 
  17.     PSH [C:\foo]: .\Get-ConformanceLevelEnum.ps1' 
  18.     System.XML.ConformanceLevel enum has 3 possible values 
  19.     Value 0: Auto 
  20.     Value 1: Fragment 
  21.     Value 2: Document 
  22.      
  23.     $ToCheck1 is NOT document 
  24.     $ToCheck2 is Document 
  25. #> 
  26.  
  27. ## 
  28. # Start of script 
  29. ## 
  30. # Demonstrates the ConformanceLevel enum 
  31. # Thomas Lee - tfl@psp.co.uk 
  32. # Enumerate the enum 
  33. $enums=[enum]::GetValues([System.Xml.ConformanceLevel]) 
  34.  
  35. # Display 
  36. "System.XML.ConformanceLevel enum has {0} possible values" -f $enums.count 
  37. $i=0 
  38. $enums| %{"Value {0}: {1}" -f $i,$_.tostring();$i++} 
  39. "" 
  40. # Checking against a an enum value  
  41. $ToCheck1 = "somethingelse" 
  42. $Tocheck2 = "document" 
  43. if ($ToCheck1 -eq  [System.XML.ConformanceLevel]::Document) { 
  44.   "`$ToCheck1 is document"} 
  45. else { 
  46.   "`$ToCheck1 is NOT document" 
  47. } 
  48.    
  49. if ($ToCheck2 -eq  [System.XML.ConformanceLevel]::Document) { 
  50.  "`$ToCheck2 is Document"} 
  51. else { 
  52.  "`$ToCheck2 is NOT document" 
  53. }  
  54. #End of Script 

Monday, 22 December 2008

Get-ParsedString.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Formats a number using .NET - an MSDN Sample recoded in PowerShell 
  4. .DESCRIPTION 
  5.     This script createa a number and formats it in Hex. Then a negative number 
  6.     is created and formatted several ways. Uses int32.parse method to do the  
  7.     parsing. 
  8. .NOTES 
  9.     File Name  : Get-ParsedString 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell V2 CTP3 
  12. .LINK 
  13.     Posted to: 
  14.     http://pshscripts.blogspot.com/2008/12/get-parsedstringps1.html 
  15.     Script also posted to MSDN at: 
  16.     http://msdn.microsoft.com/en-us/library/system.globalization.numberstyles(VS.85).aspx 
  17. .EXAMPLE 
  18.     PS c:\foo> .\Get-ParsedString.ps1 
  19.     A in hex = 10 in decimal 
  20.     '    -42    ' parsed to an int32 is '-42'. 
  21.     '    (42)   ' parsed to an int32 is '-42'. 
  22. #> 
  23.  
  24.  
  25. ### 
  26. #  Start of Script 
  27. ### 
  28.  
  29. # Parse the string as a hex value and display the value as a decimal. 
  30. $num = "A" 
  31. $val = [system.int32]::Parse($num, [System.Globalization.NumberStyles]::HexNumber) 
  32.  
  33. "{0} in hex = {1} in decimal" -f $num,$val 
  34.  
  35. # Parse the string, allowing a leading sign, and ignoring leading and trailing white spaces. 
  36. $num = "    -42    " 
  37.  
  38. # create parsing value using value__ 
  39. $format  = [System.Globalization.NumberStyles]::AllowLeadingSign.value__+ 
  40.            [System.Globalization.NumberStyles]::AllowLeadingWhite.value__ + 
  41.            [System.Globalization.NumberStyles]::AllowTrailingWhite.value__   
  42. $parsing = [System.Globalization.NumberStyles] $format      
  43.  
  44. # parse and display 
  45. $val = [system.int32]::Parse($num,$parsing) 
  46. "'{0}' parsed to an int32 is '{1}'." -f $num, $val 
  47.  
  48. # Now try a negative number 
  49. $num = "    (42)   " 
  50. $format  = [System.Globalization.NumberStyles]::AllowParentheses.value__  + 
  51.            [System.Globalization.NumberStyles]::AllowLeadingSign.value__  + 
  52.            [System.Globalization.NumberStyles]::AllowLeadingWhite.value__ + 
  53.            [System.Globalization.NumberStyles]::AllowTrailingWhite.value__ 
  54. $parsing = [System.Globalization.NumberStyles] $format              
  55.  
  56. # And parse/display it 
  57. $val = [system.int32]::Parse($num, $parsing) 
  58. "'{0}' parsed to an int32 is '{1}'." -f $num, $val 

Thursday, 18 December 2008

Get-ForestModeEnumValues.ps1

 

  1. # Get-ForestModeEnumValues.ps1 
  2. # Prints out the values of the ForestMode Enum 
  3. # Thomas Lee - tfl@psp.co.uk 
  4.  
  5. # Enumerate System.DirectoryServices.ActiveDirectory.forestmode 
  6.  
  7. $enums=[enum]::GetValues([System.DirectoryServices.ActiveDirectory.ForestMode]) 
  8.  
  9. # Display values 
  10. "System.Net.DirectoryServices.ActiveDirectory.Forest mode enum has {0} possible values:" -f $enums.count 
  11. $i=1 
  12. $enums | %{"Value {0}: {1}" -f $i,$_.tostring();$i++} 
  13. ""  
  14.  
  15. # Checking against an enum value  
  16. $ToCheck = "Windows2008Forest" 
  17. if ($ToCheck -eq  [System.DirectoryServices.ActiveDirectory.ForestMode]::Windows2008Forest) { 
  18. "`$ToCheck is Windows2008Forest" 
  19.     } 
  20. else { 
  21.     "`$ToCheck is NOT Windows2008Forest" 
  22.     } 

This script produces the following output:

PS C:\foo> .\Get-ForestModeEnumValues.ps1
System.Net.DirectoryServices.ActiveDirectory.Forest mode enum has 4 possible values:
Value 1: Windows2000Forest
Value 2: Windows2003InterimForest
Value 3: Windows2003Forest
Value 4: Windows2008Forest

$ToCheck is Windows2008Forest