Monday, 12 September 2011

Get-WmiRegistryBinaryValue.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script Gets and displays a registry 
  4.     binary value using WMI. 
  5. .DESCRIPTION 
  6.     This script uses WMI to get, then display 
  7.     a binary registry Value. 
  8.     This is a re-write of a VB Sample Script. 
  9. .NOTES 
  10.     File Name  : Set-WmiRegistryBinaryValue.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/aa394600%28VS.85%29.aspx 
  18. .EXAMPLE 
  19.     Psh[C:\foo]>Get-WmiRegistryBinaryValue.ps1 
  20.     54 
  21.     46 
  22.     4c 
  23. #> 
  24.  
  25. # Define Constants 
  26. $HKEY_Local_Machine =2147483650  
  27. $computer ='.' 
  28.  
  29. # Get Class to call static methods on 
  30. $reg = [WMIClass]"ROOT\DEFAULT:StdRegProv" 
  31.  
  32. # Define key to create 
  33. $ValueName = "Example Binary Value" 
  34. $Values     = @(0x54, 0x46, 0x4C) 
  35. $Key       = "SOFTWARE\NewKey" 
  36.  
  37. # Create Value entry 
  38. $results   = $reg.GetBinaryValue($HKEY_LOCAL_MACHINE, $Key, $ValueName) 
  39. Foreach ($byte in $results.uvalue) {"{0}" -f $byte.tostring("x")} 

W

Sunday, 11 September 2011

Set-WmiRegistryBinaryValue

  1. <# 
  2. .SYNOPSIS 
  3.     This script sets a registry binary value 
  4.     using WMI. 
  5. .DESCRIPTION 
  6.     This script uses WMI to set a binary  
  7.     registry Value. 
  8.     This is a re-write of a VB Sample Script. 
  9. .NOTES 
  10.     File Name  : Set-WmiRegistryBinaryValue.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/aa394600%28VS.85%29.aspx 
  18. .EXAMPLE 
  19.     Psh[C:\foo]>New-WmiRegistryBinaryValue.ps1 
  20.     Value created 
  21. #> 
  22.  
  23. # Define Constants 
  24. $HKEY_Local_Machine =2147483650  
  25. $computer ='.' 
  26.  
  27. # Get Class to call static methods on 
  28. $reg = [WMIClass]"ROOT\DEFAULT:StdRegProv" 
  29.  
  30. # Define key to create 
  31. $ValueName = "Example Binary Value" 
  32. $Values     = @(0x54, 0x46, 0x4C) 
  33. $Key       = "SOFTWARE\NewKey" 
  34.  
  35. # Create Value entry 
  36. $results   = $reg.SetBinaryValue($HKEY_LOCAL_MACHINE, $Key, $ValueName, $Values) 
  37. If ($results.Returnvalue -eq 0) {"Value Set"}  

Saturday, 10 September 2011

New-WmiRegistryValue.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     This script creates a new registry Value using WMI. 
  4. .DESCRIPTION 
  5.     This script uses WMI to get create a new registry Value. 
  6.     This is a re-write of a VB Sample Script 
  7. .NOTES 
  8.     File Name  : New-RegistryKey.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/aa394600%28VS.85%29.aspx 
  16. .EXAMPLE 
  17.     Psh[C:\foo]>New-WmiRegistryValue.ps1 
  18.     Value created 
  19. #> 
  20.  
  21. # Define Constants 
  22. $HKEY_Local_Machine =2147483650  
  23. $computer ='.' 
  24.  
  25. # Get Class to call static methods on 
  26. $reg = [WMIClass]"ROOT\DEFAULT:StdRegProv" 
  27.  
  28. # Define key to create 
  29. $ValueName = "Example_Expanded_String_Value" 
  30. $Value     = "%PATHEXT%" 
  31. $Key       = "SOFTWARE\NewKey" 
  32.  
  33. # Create Value entry 
  34. $results   = $reg.SetExpandedStringValue($HKEY_LOCAL_MACHINE, $Key, $ValueName, $Value) 
  35. If ($results.Returnvalue -eq 0) {"Value created"}  

Set-WmiMultiStringValue.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script sets a registry binary value 
  4.     using WMI. 
  5. .DESCRIPTION 
  6.     This script uses WMI to set a multistring 
  7.     registry Value. 
  8.     This is a re-write of a VB Sample Script. 
  9. .NOTES 
  10.     File Name  : Set-WmiRegistryMultiStringValue.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/aa394600%28VS.85%29.aspx 
  18. .EXAMPLE 
  19.     Psh[C:\foo]>Set-MultiStringValue.ps1 
  20.     Value created 
  21. #> 
  22.  
  23. # Define Constants 
  24. $HKEY_Local_Machine =2147483650  
  25. $computer ='.' 
  26.  
  27. # Get Class to call static methods on 
  28. $reg = [WMIClass]"ROOT\DEFAULT:StdRegProv" 
  29.  
  30. # Define key to create 
  31. $Key       = "SOFTWARE\NewKey" 
  32. $ValueName = "Example MultiString Value" 
  33. $Values     = @("Thomas", "Susan", "Rebecca") 
  34. $Key       = "SOFTWARE\NewKey" 
  35.  
  36. # Create Value entry 
  37. $results   = $reg.SetMultiStringValue($HKEY_LOCAL_MACHINE, $Key, $ValueName, $Values) 
  38. If ($results.Returnvalue -eq 0) {"Value Set"}  

Friday, 9 September 2011

New-WmiRegistryKey.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     This script creates a new registry key using WMI. 
  4. .DESCRIPTION 
  5.     This script uses WMI to get create a new registry key. 
  6.     This is a re-write of a VB Sample Script 
  7. .NOTES 
  8.     File Name  : New-RegistryKey.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/aa394600%28VS.85%29.aspx 
  16. .EXAMPLE 
  17.     Psh[C:\foo]>New-WMIRegistryKey.ps1 
  18.     Key created 
  19. #> 
  20.  
  21. # Define Constants 
  22. $HKEY_Local_Machine =2147483650  
  23. $computer ='.' 
  24.  
  25. # Get Class to call static methods on 
  26. $reg = [WMIClass]"ROOT\DEFAULT:StdRegProv" 
  27.  
  28. # Define key to create 
  29. $Key     = "SOFTWARE\NewKey" 
  30.  
  31. # Create key and display reslts 
  32. $results   = $reg.CreateKey($HKEY_LOCAL_MACHINE, $Key) 
  33. If ($results.Returnvalue -eq 0) {"Key created"}  

Thursday, 8 September 2011

Get-WmiRegDword.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script gets a registry value using WMI. 
  4. .DESCRIPTION 
  5.     This script uses WMI to get then display a resistry value, using  
  6.     the SteRegProv class and the GetDWORDValue static method. 
  7.     This is a re-write of a VB Sample Script 
  8. .NOTES 
  9.     File Name  : Get-WMIRegDword.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.     MSDN sample posted to: 
  16.         http://msdn.microsoft.com/en-us/library/aa394600%28VS.85%29.aspx 
  17. .EXAMPLE 
  18.     Psh[C:\foo]>Get-WmiRegDword.ps1 
  19. Current History Buffer Size: 50 
  20.      
  21. #> 
  22.  
  23. # Define Constants 
  24. $HKEY_CURRENT_USER =2147483649   
  25. $computer ='.' 
  26.  
  27. # Get Class to call static methods on 
  28. $reg = [WMIClass]"ROOT\DEFAULT:StdRegProv" 
  29.  
  30. #defind key/value to get  
  31. $Key     = "Console" 
  32. $Value   = "HistoryBufferSize" 
  33.  
  34. # Get Value of buffer and display 
  35. $results   = $reg.GetDWORDValue($HKEY_CURRENT_USER, $Key, $value) 
  36. "Current History Buffer Size: {0}" -f $results.uValue 
Technorati Tags: ,,,

Sunday, 14 August 2011

Show-MajorPart.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script shows the Major part of the version number 
  4.     of file version object. 
  5. .DESCRIPTION 
  6.     This script is a re-implementation of an MSDN Sample script 
  7.     that uses System.Diagnostics.FileVersionInfo to get 
  8.     the major part of the version number of the file. 
  9. .NOTES 
  10.     File Name  : Show-MajorPart.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell Version 2.0 
  13. .LINK 
  14.     This script posted to: 
  15.         http://pshscripts.blogspot.com/2011/08/show-majorpartps1.html 
  16.     MSDN sample posted to: 
  17.          http://msdn.microsoft.com/en-us/library/system.diagnostics.fileversioninfo.filemajorpart.aspx   
  18. .EXAMPLE 
  19.     Psh> .\Show-MajorPart.ps1 
  20.     File Major Part for C:\Windows\system32\Notepad.exe is: 6             
  21. #> 
  22. # Set filename 
  23. $File = [System.Environment]::SystemDirectory + "\Notepad.exe" 
  24.  
  25. #Get Version information for this file 
  26. $myFileVersionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($File) 
  27.  
  28. # Print the Build details name 
  29. "File Major Part for {0} is: {1}"  -f $file,$myFileVersionInfo.FileMajorPart 

Thursday, 14 July 2011

Show-MinorPart.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script shows the Minor part of the version number 
  4.     of file version object. 
  5. .DESCRIPTION 
  6.     This script is a re-implementation of an MSDN Sample script 
  7.     that uses System.Diagnostics.FileVersionInfo to get 
  8.     the mainor part of the version number of the file. 
  9. .NOTES 
  10.     File Name  : Show-MinorPart.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell Version 2.0 
  13. .LINK 
  14.     This script posted to: 
  15.         http://http://pshscripts.blogspot.com 
  16.     MSDN sample posted to: 
  17.          http://msdn.microsoft.com/en-us/library/system.diagnostics.fileversioninfo.FileMinorpart.aspx  
  18. .EXAMPLE 
  19.     Psh> .\Show-MinorPart.ps1 
  20.     File Major Part for C:\Windows\system32\Notepad.exe is: 0             
  21. #> 
  22. # Set filename 
  23. $File = [System.Environment]::SystemDirectory + "\Notepad.exe" 
  24.  
  25. #Get Version information for this file 
  26. $myFileVersionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($File) 
  27.  
  28. # Print the Build details name 
  29. "File Minor Part for {0} is: {1}"  -f $file,$myFileVersionInfo.FileMinorPart 

Sunday, 19 June 2011

Get-ParsedInteger.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script parses several strings into integers, where possible.  
  4. .DESCRIPTION 
  5.     This script uses the TryParse method on [Int32] to attempt 
  6.     to parse a string into a number and writes the results. This script 
  7.     is a recoded MSDN sample using PowerShell 
  8. .NOTES 
  9.     File Name  : Get-ParsedInt32.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.     MSDN sample posted tot: 
  16.         http://msdn.microsoft.com/en-us/library/f02979c7.aspx 
  17. .EXAMPLE 
  18.     Psh[C:\foo]> .\get-parsedinteger.ps1 
  19. Attempted conversion of '' failed. 
  20. Converted '160519' to 160519. 
  21. Attempted conversion of '9432.0' failed. 
  22. Attempted conversion of '16,667' failed. 
  23. Converted '   -322   ' to -322. 
  24. Converted '+4302' to 4302. 
  25. Attempted conversion of '(100);' failed. 
  26. Attempted conversion of '01FA' failed.     
  27. #> 
  28.  
  29. # Define function to parse a string to integer 
  30. Function TryToParse { 
  31. # Parameter to parse into a number 
  32. Param ([string] $value) 
  33. # Define $number and try the parse 
  34. [int] $number = 0 
  35. $result = [System.Int32]::TryParse($value, [ref] $number); 
  36. if ($result)  { 
  37.       "Converted '{0}' to {1}." -f $value, $number  
  38.               } 
  39.       else    { 
  40.         if ($value -eq $null) {$value = ""}  
  41.        "Attempted conversion of '{0}' failed." -f $value 
  42.               } 
  43. } 
  44.  
  45. # Now call the function to see if the string will parse 
  46. TryToParse($null) 
  47. TryToParse("160519"); 
  48. TryToParse("9432.0"); 
  49. TryToParse("16,667"); 
  50. TryToParse("   -322   "); 
  51. TryToParse("+4302"); 
  52. TryToParse("(100);"); 
  53. TryToParse("01FA"); 
Technorati Tags: ,

Tuesday, 29 March 2011

New-Credential.ps1

  1. <# 
  2. .SYNOPSIS 
  3.    A function to create a credential object from script. 
  4. .DESCRIPTION 
  5.    Enables you to create a credential objects from stored details. 
  6. .NOTES 
  7.     File Name  : New-Credential.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://pshscripts.blogspot.com/2011/03/new-credentialps1.html 
  13. .PARAMETER UserId 
  14.    The userid in the form of "domain\user" 
  15. .PARAMETER Password 
  16.    The password for this user 
  17. .EXAMPLE 
  18.    New-Credential contoso\administrator  Pa$$w0rd 
  19. #> 
  20.  
  21. function New-Credential { 
  22. param ( 
  23. [string] $Userid, 
  24. [string] $Pwd 
  25. ) 
  26. # Create the credential 
  27. $spwd = ConvertTo-SecureString -AsPlainText $pwd -Force  
  28. $cred = New-Object System.Management.Automation.PSCredential $userid,$spwd 
  29. # Now return it to the caller 
  30. return $cred 
  31. } 
  32.  
  33. # Call the function to demostrate example 
  34. New-Credential "contoso\administrator" "Pa$$w0rd" 
Technorati Tags: ,,