Saturday, 22 December 2012

Show-Exceptions.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script gets all the exceptions you can trap by PowerShell 
  4. .DESCRIPTION 
  5.     This script looks at all the loaded assemblies to get all 
  6.     the exceptions you can trap/catch using PowerShell. The 
  7.     display only covers those parts of the .NET framework are loaded. 
  8. .NOTES 
  9.     File Name  : Show-Exceptions.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.     Psh> .\Show-Exceptions.ps1 -Summary 
  17.     In 69 loaded assemblies, you have 418 exceptions: 
  18.  
  19. .EXAMPLE 
  20.     Psh> .\Show-Exceptions.ps1 
  21.     In 69 loaded assemblies, you have 418 exceptions: 
  22.  
  23.     Name                     FullName                                                 
  24.     ----                     --------                                                 
  25.     _Exception               System.Runtime.InteropServices._Exception                
  26.     AbandonedMutexException  System.Threading.AbandonedMutexException                 
  27.     AccessViolationException System.AccessViolationException                      
  28.     ... 
  29.      
  30. #> 
  31.  
  32. [CMDLETBINDING()] 
  33. Param ( 
  34. [switch] $summary 
  35. #    Get all the exceptions 
  36. $assemblies = [System.AppDomain]::CurrentDomain.GetAssemblies() 
  37. $exceptions = $Assemblies | ForEach { 
  38.      $_.GetTypes() | where { $_.FullName -Match "System$filter.*Exception$" } } 
  39.  
  40. # Now display the numbers checking for summary flag 
  41. "In {0} loaded assemblies, you have {1} exceptions:" -f $assemblies.count, $exceptions.count 
  42. If (-not $summary) { 
  43. $Exceptions | sort name | format-table name, fullname 
Technorati Tags:

Sunday, 9 December 2012

Get-LyncAutoLogonStatus.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script reports on whether Lync should 
  4.     automatically start on a machine when a user 
  5.     logs in
  6. .DESCRIPTION 
  7.     This script looks in the registry at a chosen machine 
  8.     to determine if the Lync client should automatically 
  9.     attempt to login when a user logs onto that system. 
  10. .NOTES 
  11.     File Name  : Get-LyncAutoLogonStatus 
  12.     Author     : Thomas Lee - tfl@psp.co.uk 
  13.     Requires   : PowerShell Version 2.0 
  14.                  Lync 2010 or later 
  15. .LINK 
  16.     This script posted to: 
  17.         http://www.pshscripts.blogspot.com 
  18. .EXAMPLE 
  19.     Psh> .\Get-LyncAutoLogonStatus     
  20.     Automatically start Lync when I log on to Windows: True 
  21. #> 
  22.  
  23.  
  24. [Cmdletbinding()] 
  25. Param ( 
  26. [string] $computer = "Cookham8.Cookham.net"
  27.  
  28.  
  29. # Get the relevant registry key 
  30. $registry = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("CurrentUser", $computer
  31. $key = $registry.OpenSubKey("SOFTWARE\Microsoft\Communicator", $True
  32.   
  33. # now write to host the details 
  34. Write-Host "Automatically start Lync when I log on to Windows:",` 
  35.     ([boolean] $key.GetValue("AutoRunWhenLogonToWindows",$null)) 
Technorati Tags: ,

Sunday, 2 December 2012

Set-AdminLogon.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script defines a function that sets autologon 
  4. .DESCRIPTION 
  5.     Autologon enables the system to logon after a reboot without 
  6.     you needing to enter credentials. This is an ideal scenario 
  7.     for lab or training room systems. This script defines 
  8.     a function that sets a userid/password and autologon.
  9. .NOTES 
  10.     File Name  : Set-AdminLogon 
  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. .EXAMPLE 
  17.     Psh> New-AdminLogon -user cookham\tfl -password 'JerryGarciaR0cks!' 
  18.     Auto logon created for [Cookham\tfl] with password: [JerryGarciaR0cks]          
  19.  
  20. #> 
  21.  
  22. Function New-AdminLogon { 
  23.  
  24. [cmdletbinding()] 
  25. Param( 
  26. [string] $User     = $(Throw 'No user id specified'), 
  27. [string] $Password = $(Throw 'No password specified'
  28.  
  29. # Define registry path for autologon 
  30. $RegPath = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon' 
  31.  
  32. # Set autologon 
  33. Set-ItemProperty -Path $RegPath -Name AutoAdminLogon -Value 1  
  34.  
  35. # Set userid and password 
  36. Set-ItemProperty -Path $RegPath -Name DefaultUserName -Value $User  
  37. Set-ItemProperty -Path $regPath -Name DefaultPassword -Value $Password  
  38.  
  39. # Say nice things and exit! 
  40. Write-Host ("Auto logon [{0}] set to password: [{1}]" -f $user, $password
  41.  
  42. Set-AdminLogon -User 'Cookham\tfl' -Password 'JerryGarciaR0cks' 
Technorati Tags: ,

Remove-AdminLogon.ps1

  1. <#
  2. .SYNOPSIS
  3. This script defines a function that removes autologon
  4. .DESCRIPTION
  5. Autologon enables the system to logon after a reboot without
  6. you needing to enter credentials. This is an ideal scenario
  7. for lab or training room systems. This script defines
  8. a function that removes the autologon registry keys
  9. .NOTES
  10. File Name : Remove-AdminLogon
  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. .EXAMPLE
  17. Psh> Remove-AdminLogon
  18. Auto logon settings removed
  19. #>
  20. Function Remove-AdminLogon {
  21. [Cmdletbinding()]
  22. Param()
  23. # Define registry path for autologon
  24. $RegPath = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon'
  25. # Remove autologon property
  26. Remove-ItemProperty -Path $RegPath -Name AutoAdminLogon
  27. # Remove userid and password
  28. Remove-ItemProperty -Path $RegPath -Name DefaultUserName
  29. Remove-ItemProperty -Path $RegPath -Name DefaultPassword
  30. # Say nice things and exit!
  31. Write-Host ("Auto logon removed")
  32. }
  33. Remove-AdminLogon
Technorati Tags: ,

New-AutoAdminLogon.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script defines a function that sets autologon 
  4. .DESCRIPTION 
  5.     Autologon enables the system to logon after a reboot without 
  6.     you needing to enter credentials. This is an ideal scenario 
  7.     for lab or training room systems. This script defines 
  8.     a function that sets a userid/password and autologon. It does NOT 
  9.     check to see if the value entries already exist. 
  10. .NOTES 
  11.     File Name  : Set-AutoAdminLogon 
  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. .EXAMPLE 
  18.     Psh> Set-AutoAdminLogon -user cookham\tfl -password 'JerryGarciaR0cks!' 
  19.     Auto logon set for [Cookham\tfl] with password: [JerryGarciaR0cks]          
  20.  
  21. #> 
  22.  
  23. Function Set-AdminLogon { 
  24.  
  25. [cmdletbinding()] 
  26. Param( 
  27. [string] $User     = $(Throw 'No user id specified'), 
  28. [string] $Password = $(Throw 'No password specified'
  29.  
  30. # Define registry path for autologon 
  31. $RegPath = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon' 
  32.  
  33. # Set autologon 
  34. New-ItemProperty -Path $RegPath -Name AutoAdminLogon -Value 1 -EA 0 
  35.  
  36. # Set userid and password 
  37. New-ItemProperty -Path $RegPath -Name DefaultUserName -Value $User -EA 0 
  38. New-ItemProperty -Path $regPath -Name DefaultPassword -Value $Password -EA 0 
  39.  
  40. # Say nice things and exit! 
  41. Write-Host ("Auto logon set for [{0}] with password: [{1}]" -f $user, $password
  42.  
  43. Set-AdminLogon -User 'Cookham\tfl' -Password 'JerryGarciaR0cks' 
Technorati Tags: ,

Friday, 23 November 2012

Convert-PptxToPDF.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This function converts a PPTx file into a PDF file 
  4. .DESCRIPTION 
  5.     The Convert-PptxToPDF function first creates an  
  6.     instance of PowerPoint, opens the $ifile and saves 
  7.     this to $ofile as a PDF file. 
  8. .NOTES 
  9.     File Name  : Convert-PptxToPDF 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 3.0, Office 2010 
  12. .LINK 
  13.     This script posted to: 
  14.         http://www.pshscripts.blogspot.com 
  15.      
  16. .EXAMPLE 
  17.     There is nothing to see, except a set of new PDF Files in the output folder         
  18.  
  19. #> 
  20.  
  21. Function Convert-PptxToPDF { 
  22.  
  23. [CmdletBinding()] 
  24. Param( 
  25. $IFile
  26. $OFile 
  27.  
  28. # add key assemblies 
  29. Add-type -AssemblyName office -ErrorAction SilentlyContinue 
  30. Add-Type -AssemblyName microsoft.office.interop.powerpoint -ErrorAction SilentlyContinue 
  31.  
  32. # Open PowerPoint 
  33. $ppt = new-object -com powerpoint.application 
  34. $ppt.visible = [Microsoft.Office.Core.MsoTriState]::msoFalse 
  35.  
  36.  
  37. # Open the $Ifile presentation 
  38. $pres = $ppt.Presentations.Open($ifile
  39.  
  40. # Now save it away as PDF 
  41. $opt= [Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType]::ppSaveAsPDF 
  42. $pres.SaveAs($ofile,$opt
  43.  
  44. # and Tidy-up 
  45. $pres.Close() 
  46. $ppt.Quit() 
  47. $ppt=$null 
  48.  
  49.  
  50.  
  51. # Test it 
  52.  
  53. $ipath = "E:\SkyDrive\PowerShell V3 Geek Week\" 
  54.  
  55. Foreach ($ifile in $(ls $ipath -Filter "*.pptx")) { 
  56.   # Build name of output file 
  57.   $pathname = split-path $ifile 
  58.   $filename = split-path $ifile -leaf  
  59.   $file     = $filename.split(".")[0] 
  60.   $ofile    = $pathname + $file + ".pdf" 
  61.  
  62.   # Convert _this_ file to PDF 
  63.    Convert-PptxToPDF -ifile $ifile -OFile $ofile 
Technorati Tags: ,,,

Wednesday, 3 October 2012

Get-GuiMode.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script gets the current GUI mode of a Windows 
  4.     Server 2012 system, including current display resolution. 
  5. .DESCRIPTION 
  6.     This script creates a custom object, then populates it 
  7.     with the current mode (Server Core, MinShell, Full Shell) 
  8.     and with the current resolution (resolution plus h/w). The 
  9.     display information object is then returned. 
  10. .NOTES 
  11.     File Name  : Get-GuiMode.ps1 
  12.     Author     : Thomas Lee - tfl@psp.co.uk 
  13.     Requires   : PowerShell Version 3.0  
  14.                  Windows Server 2012 
  15. .LINK 
  16.     This script posted to: 
  17.         http://www.pshscripts.blogspot.com 
  18. .EXAMPLE 
  19.     Psh>  ./Get-GuiMode 
  20.     ComputerName : S1 
  21.     ServerCore   : True 
  22.     FullServer   : False 
  23.     MinShell     : False 
  24.     Resolution   : 1024x768 
  25.     Height       : 768 
  26.     Width        : 1024 
  27.  
  28. #> 
  29.  
  30.  
  31. Function Get-GUIMode { 
  32.  
  33. # Create a new object to return 
  34. $guimode = new-object psobject 
  35.  
  36. # Add ComputerName to the object 
  37. $guimode |Add-Member -MemberType NoteProperty -Name ComputerName -Value $(hostname) 
  38.  
  39.  
  40. # now determine what's installed 
  41. $sgs  = (Get-WindowsFeature Server-Gui-Shell).Installed 
  42. $mif  = (Get-WindowsFeature Server-Gui-Mgmt-Infra).Installed 
  43.  
  44. If (!$sgs -and !$mif# True Server Core  
  45.   { 
  46.     $guimode |Add-Member -MemberType NoteProperty -Name ServerCore -Value $True 
  47.     $guimode |Add-Member -MemberType NoteProperty -Name FullServer -Value $False 
  48.     $guimode |Add-Member -MemberType NoteProperty -Name MinShell   -Value $False 
  49.   } 
  50. Elseif ($sgs -and !$mif) # MinShell 
  51.   { 
  52.     $guimode |Add-Member -MemberType NoteProperty -Name ServerCore -Value $False 
  53.     $guimode |Add-Member -MemberType NoteProperty -Name FullServer -Value $False 
  54.     $guimode |Add-Member -MemberType NoteProperty -Name MinShell   -Value $True 
  55.   } 
  56. Elseif ($sgs -and $mif
  57.   { 
  58.     $guimode |Add-Member -MemberType NoteProperty -Name ServerCore -Value $False 
  59.     $guimode |Add-Member -MemberType NoteProperty -Name FullServer -Value $True 
  60.     $guimode |Add-Member -MemberType NoteProperty -Name MinShell   -Value $False 
  61.   } 
  62.  
  63. # now resolution 
  64. If ($rx=get-command Get-DisplayResolution) { 
  65.   $res  = (Get-DisplayResolution)[0] 
  66.   $reslen = $res.length 
  67.   $r = [string]"" 
  68.   for ($i = 0; $i -lt $reslen; $i++) 
  69.    {  
  70.      If ($res.substring($i,1) -ne "") { $r += $res.substring($i,1) } 
  71.    } 
  72.  
  73.     
  74.   $guimode |Add-Member -MemberType NoteProperty -Name Resolution -Value $r 
  75.  
  76.   $h = $r.split("x")[1]  # height 
  77.   $w = $r.split("x")[0]  #  
  78.    
  79.   $guimode |Add-Member -MemberType NoteProperty -Name Height -Value $h 
  80.   $guimode |Add-Member -MemberType NoteProperty -Name Width  -Value $w 
  81.  
  82. # Ok - squirt out what we have! 
  83. $guimode 
  84.  
  85. # Here Test it out 
  86. Get-GUiMode 

Set-PowerShellAsShell

  1. <# 
  2. .Synopsis 
  3.    Creates a function to set PowerShell as GUI in Server 2012 
  4. .DESCRIPTION 
  5.    The function in this script sets PowerShell as the  
  6.    default shell in Server 2012. When the server is rebooted, 
  7.    it runs PowerShell.exe by default. When PowerShell starts, it 
  8.    displays the $PSVersionTable variable. 
  9. .NOTES 
  10.     File Name   : Set-PowerShellAsGui.ps1 
  11.     Author      : Thomas Lee - tfl@psp.co.uk 
  12.     Requires    : Server 2012 
  13. .LINK 
  14.     This script posted to: 
  15.         http://www.pshscripts.blogspot.com   
  16. .EXAMPLE 
  17.     Left as an exercise to the reader 
  18. #> 
  19.  
  20.  
  21. Function Set-PowerShellAsShell { 
  22.  
  23. [CmdletBinding()] 
  24. Param ( 
  25. [switch] $Reboot = $false 
  26.  
  27. # Create Registry Path variable 
  28. $RegPath =  "Microsoft.PowerShell.Core\Registry::"  
  29. $RegPath += "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\" 
  30. $RegPath += "Windows NT\CurrentVersion\winlogon" 
  31.  
  32. # Create splatted parameter hash table 
  33. $parm  =  @{Path    = $regpath}           # key 
  34. $parm +=  @{Name    = 'Shell'}            # value name 
  35. $parm +=  @{Value   = 'PowerShell.exe –NoExit 
               -Command "
    $psversiontable"'}   # value’s value 
  36.  
  37. # Set Registry value entry 
  38. Set-ItemProperty @parm  
  39.  
  40. # And restart to see PowerShell 
  41. if ($Reboot) {Restart-Computer -confirm} 

Monday, 1 October 2012

Show-FolderCreation.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This example shows how to create a new directory and  
  4.     subdirectory, and then delete only the subdirectory. 
  5. .DESCRIPTION 
  6.     This sample is a re-write of an MSDN Sample,  
  7.     but in PowerShell. The sample firsts creates then removes 
  8.     a folder then looks to see what is left. The target 
  9.     folder is removed, but intermediate folders remain. 
  10. .NOTES 
  11.     File Name  : Show-FolderCreation.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/62t64db3.aspx 
  19. .EXAMPLE 
  20.     Psh>  Show-FolderCreation.ps1 
  21.     Created: C:\NewDirectory\NewSubDirectory 
  22.     Deleted: C:\NewDirectory\NewSubDirectory 
  23.     Top-level directory exists:  True 
  24.     Sub-directory exists      :  False  
  25. #> 
  26.  
  27. [CmdletBinding()] 
  28. Param ( 
  29. $Path = "C:\NewDirectory\NewSubDirectory" 
  30.  
  31. Try 
  32.   { 
  33. # Create then remove directory 
  34.      $result = [System.IO.Directory]::CreateDirectory($Path
  35.      "Created: $path"    
  36.      [System.IO.Directory]::Delete($Path
  37.      "Deleted: $path" 
  38.  
  39. # Check existance for top and sub dirs then display results 
  40.      $directoryExists    = [System.Io.Directory]::Exists("C:\NewDirectory"
  41.      $subDirectoryExists = [System.Io.Directory]::Exists($Path
  42.      "Top-level directory exists:  $directoryExists" 
  43.      "Sub-directory exists      :  $subDirectoryExists" 
  44.    } 
  45. Catch  
  46.    { 
  47.        "The process failed: {0}" -f $($error[0].Message) 
  48.    }    

Sunday, 23 September 2012

EchoArgs.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script contains a function, EchoArgs, that returns a list of 
  4.     the arguments passed and their type. It is used to demonstrate 
  5.     the use of the --% operator when calling a function or cmdlet, a new 
  6.     feature in PowerShell v3. 
  7. .DESCRIPTION 
  8.     The EchoArgs function takes the arguments passed, via $args, and
  9.     displays each argument and its type. Then, this function is called
  10.     first with a normal calling sequence and then using --%. 
  11. .NOTES 
  12.     File Name  : EchoArgs.ps1 
  13.     Author     : Thomas Lee - tfl@psp.co.uk 
  14.     Requires   : PowerShell Version 2.0 
  15. .LINK 
  16.     This script posted to: 
  17.         http://www.pshscripts.blogspot.com 
  18.     MSDN sample posted to: 
  19.          http://msdn.microsoft.com/en-us/library. 
  20. .EXAMPLE 
  21.     Psh[Cookham8:C:\foo]>E:\PowerShellScriptLib\SERVER2012FEATURES\EchoArgs.ps1 
  22.     Calling Echoargs with 'fasdf $(LS) 2334 {asdf}' 
  23.     Argument [0]: [fasdf] Type:System.String 
  24.     Argument [1]: [System.Object[]] Type:System.Object[] 
  25.     Argument [2]: [2334] Type:System.Int32 
  26.     Argument [3]: [asdf] Type:System.Management.Automation.ScriptBlock 
  27.  
  28.     Calling Echoargs with '--%  asdf; {asfd}-a asdf' 
  29.     Argument [0]: [--%] Type:System.String 
  30.     Argument [1]: [asdf; $(ls) {asfd} - a asdf] Type:System.String        
  31. #> 
  32.  
  33. # EchoArgs function 
  34. Function EchoArgs { 
  35. #Loop through and display each argument passed 
  36.   For ($i = 0; $i -ilt $Args.count; $i ++) { 
  37.   "Argument [{0}]: [{1}] Type:{2}" -f $I, $args[$i],$($args[$i].GetType().FullName) 
  38.  
  39. # Test it 
  40. "Calling Echoargs with 'fasdf `$(LS) 2334 {asdf}'" 
  41. Echoargs  fasdf $(ls) 2334 {asdf} 
  42. "";"Calling Echoargs with '--%  asdf; {asfd}-a asdf'" 
  43. Echoargs  --%  asdf; $(ls) {asfd} - a asdf 

Tuesday, 18 September 2012

Disable-Gui.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script defines a function, Disable-Gui which 
  4.     disables the GUI on Windows Server 2012 Server Core 
  5. .DESCRIPTION 
  6.     The Disable-GUI function enables the GUI in Server 2012 
  7.     Server Core by Removing two windows features. The 
  8.     script add a Shell setting to ensure that when 
  9.     Server 2012 restarts, it starts with PowerShell.  
  10. .NOTES 
  11.     File Name  : Disable-GUI.ps1 
  12.     Author     : Thomas Lee - tfl@psp.co.uk 
  13.     Requires   : PowerShell Version 3.0 and Windows Server 2012. 
  14. .LINK 
  15.     This script posted to: 
  16.         http://www.pshscripts.blogspot.com 
  17. .EXAMPLE 
  18.     Psh> Disable-GUI -Verbose 
  19.     Removing Windows Feature: Server-GUI-Shell, Server-Gui-Mgmt-Infra 
  20.     Setting Shell Registry setting to PowerShell 
  21.     Restarting the computer - please be patient 
  22.     < after reboot, full GUI is added > 
  23. #> 
  24.  
  25. Function Disable-GUI { 
  26.  
  27. # No parameters - but maybe later 
  28. # Turn on CmdletBinding to enable -Verbose 
  29.  
  30. [Cmdletbinding()] 
  31. Param() 
  32.  
  33. # Remove features from main Server core and downgrade GUI to Server Core 
  34. Write-Verbose "Removing Windows Feature: Server-GUI-Shell, Server-Gui-Mgmt-Infra" 
  35. Uninstall-WindowsFeature Server-Gui-Shell, Server-Gui-Mgmt-Infra 
  36.  
  37. # Add in PowerShell as the shell 
  38. Write-Verbose "Setting Shell Registry setting to PowerShell" 
  39. $RegPath = "Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\winlogon"  
  40. Set-ItemProperty -Path $RegPath -Name Shell -Value 'PowerShell.exe -noExit -Command "$psversiontable"'  -Force 
  41.  
  42. # Now restart the system 
  43. Write-Verbose "Restarting the computer - please be patient" 
  44. Restart-Computer 

Enable-GUI.ps1

  1. <#
  2. .SYNOPSIS
  3. This script defines a function, Enable-Gui which
  4. enables the GUI on Windows Server 2012 Server Core
  5. .DESCRIPTION
  6. The Enable-GUI function enables the Gui in Server 2012
  7. Server Core by adding in two windows features. The
  8. script removes any Shell setting to ensure that when
  9. Server 2012 restarts, it starts with the full Desktop.
  10. .NOTES
  11. File Name : Enable-GUI.ps1
  12. Author : Thomas Lee - tfl@psp.co.uk
  13. Requires : PowerShell Version 3.0 and Windows Server 2012.
  14. .LINK
  15. This script posted to:
  16. http://www.pshscripts.blogspot.com
  17. .EXAMPLE
  18. Psh> Enable-GUI -Verbose
  19. Installing Windows Feature: Server-GUI-Shell, Server-Gui-Mgmt-Infra
  20. Removing Shell Registry Setting
  21. Finished installation, now rebooting
  22. < after reboot, full GUI is added >
  23. #>
  24. Function Enable-GUI {
  25. # No parameters - but maybe later
  26. # Turn on CmdletBinding to enable -Verbose
  27. [Cmdletbinding()]
  28. Param()
  29. # Install the GUI
  30. Write-Verbose "Installing Windows Feature: Server-GUI-Shell, Server-Gui-Mgmt-Infra"
  31. Install-WindowsFeature Server-Gui-Shell, Server-Gui-Mgmt-Infra -Source d:\sources\sxs
  32. # Remove the Setting For Shell to force back to CMD.EXE
  33. Write-Verbose 'Removing Shell Registry Setting'
  34. $RegPath = "Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\winlogon"
  35. Remove-ItemProperty -Confirm -Path $RegPath -Name Shell -ErrorAction SilentlyContinue
  36. # And reboot the system
  37. Write-Verbose "Finished installation, now rebooting"
  38. Restart-Computer
  39. }

Sunday, 16 September 2012

Set-HyperVHostDefault.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script demonstrates setting default values for Local Hyper-V host. 
  4. .DESCRIPTION 
  5.     This script imports the Hyper-V module then uses it 
  6.     to set certain default values for this hyper-V Host 
  7. .NOTES 
  8.     File Name  : Set-HyperVHostDefault.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell Version 3.0 and Windows 8/Server 2012 
  11. .LINK 
  12.     This script posted to: 
  13.         http://www.pshscripts.blogspot.com 
  14. .EXAMPLE 
  15.    Left as an exercise for the reader 
  16.     
  17. #> 
  18.  
  19.  
  20. # Import Hyper-V Module 
  21. Import-Module Hyper-V 
  22.  
  23. Write-Verbose "$((gcm -module hyper-v).Count) cmdlets imported in Hyper-V Module" 
  24.  
  25. # Create $parm hash table! 
  26. $parm = @{} 
  27.  
  28. # Specify the Computername 
  29. $parm += @{ComputerName = "Win8.Cookham.Net"
  30.  
  31. # Specify the Hard Disk Path 
  32. $parm += @{VirtualMachinePath = "E:\hyperv"
  33.  
  34. # Specify the VHD Disk Path 
  35. $parm += @{VirtualHardDiskPath = "E:\hyperv"
  36.  
  37. # Set the VM host accordingly 
  38. Write-Verbose "Setting parameters as follows:";$parm 
  39. Set-VmHost @parm 
  40.  
  41. # And Display Details 
  42.  
  43. Get-VMHost 
  44.  
  45. # End Set-HyperVHostDefaults 

New-InternalSwitch.ps1

  1. <#
  2. .SYNOPSIS
  3.     This script demonstrates creating a Hyper-V Switch
  4. .DESCRIPTION
  5.     This script imports the Hyper-V module then uses it
  6.     to create an Internal Switch for use in future provisioning.
  7. .NOTES
  8.     File Name : New-InternalSwitch.ps1
  9.     Author : Thomas Lee - tfl@psp.co.uk
  10.     Requires : PowerShell Version 3.0 and Windows 8/Server 2012
  11. .LINK
  12.     This script posted to:
  13.     http://www.pshscripts.blogspot.com
  14. .EXAMPLE
  15.     C:\foo\> .New-InternalSwitch.ps1
  16.     VERBOSE: New-InternalSwitch will create a new virtual network.
  17.     Name      SwitchType NetAdapterInterfaceDescription
  18.     ----      ---------- ------------------------------
  19.     Internal  Internal
  20. #>
  21. # Import Hyper-V Module
  22. Import-Module Hyper-V
  23. Try {New-VMSwitch -Name Internal -SwitchType Internal -ComputerName LocalHost -Verbose}
  24. Catch { "Failed to create switch"; $error[0] }
  25. # End New-InternalSwitch.ps1

Tuesday, 7 August 2012

Show-Formatting1.ps1

  1. <#
  2. .SYNOPSIS
  3. MSDN Sample Recoded in PowerShell demonstrating formatting
  4. .DESCRIPTION
  5. This sample recodes an MSDN Sample into PowerShell that
  6. shows some of the options of formatting using ToString() and
  7. various .NET formatting strings
  8. .NOTES
  9. File Name : Show-Formatting1.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/0c899ak8.aspx
  17. .EXAMPLE
  18. Psh> .\Show-Formatting1.ps1\
  19. 00123
  20. 1.20
  21. 01.20
  22. 01,20
  23. 0.6
  24. 1,234,567,890
  25. 1.234.567.890
  26. 1,234,567,890.1
  27. 1,234.57
  28. #>
  29. ## Start script
  30. [double] $value = 123;
  31. $value.ToString("00000")
  32. # Displays 00123
  33. $value = 1.2;
  34. $value.ToString("0.00", [System.Globalization.CultureInfo]::InvariantCulture)
  35. # Displays 1.20
  36. $value.ToString("00.00",[System.Globalization.CultureInfo]::InvariantCulture)
  37. # Displays 01.20
  38. $value.ToString("00.00",
  39. [System.Globalization.CultureInfo]::CreateSpecificCulture("da-DK"))
  40. # Displays 01,20
  41. $value = .56
  42. $value.ToString("0.0", [System.Globalization.CultureInfo]::InvariantCulture)
  43. # Displays 0.6
  44. $value = 1234567890
  45. $value.ToString("0,0", [System.Globalization.CultureInfo]::InvariantCulture)
  46. # Displays 1,234,567,890
  47. $value.ToString("0,0",
  48. [System.Globalization.CultureInfo]::CreateSpecificCulture("el-GR"))
  49. # Displays 1.234.567.890
  50. $value = 1234567890.123456;
  51. $value.ToString("0,0.0", [System.Globalization.CultureInfo]::InvariantCulture)
  52. # Displays 1,234,567,890.1
  53. $value = 1234.567890;
  54. $value.ToString("0,0.00", [System.Globalization.CultureInfo]::InvariantCulture)
  55. # Displays 1,234.57

Thursday, 5 July 2012

Manage-ServerCoreGui.ps1

  1. <# 
  2. .Synopsis 
  3.    This file contains two simple functions to manage the GUI
  4.    in Server Core Windows Server 2012. 
  5. .DESCRIPTION 
  6.    There are two functions: Enable-ServerCoreGui that enables
  7.    the GUI in a server core installation. Diable-ServerCoreGuI
  8.    disables the GUI and returns the installation to
  9.    traditional Server Core. 
  10.  
  11.    Enable-ServerCoreGui also checks to see if some or all
  12.    parts are already installed andwarns accordingly. 
  13. .EXAMPLE 
  14.    c:\foo> Enable-ServerCoreGui 
  15.     
  16.    This enables the GUI and reboots the Server into FUll GUI mode. 
  17.  
  18. .EXAMPLE 
  19.    c:\foo> Disable-ServerCoreGui 
  20.     
  21.    This disables the GUI and reboots the Server back to Server Core mode. 
  22. #> 
  23.  
  24.  
  25. Function Enable-ServerCoreGui { 
  26.  
  27. #    Import the module 
  28. Import-Module -Name DISM -ErrorAction Stop 
  29.  
  30. # is it already enabled?? 
  31. if ((Get-WindowsOptionalfeature -online -Feature ServerCore-FullServer).state -eq 'Enabled'){ 
  32.   "Servercore-FullServer is already enabled" 
  33.  
  34. if ((Get-WindowsOptionalfeature -online -Feature Server-GUI-Shell).state -eq 'Enabled'){ 
  35.   "Server-GUI Shell is already enabled" 
  36.  
  37. if ((Get-WindowsOptionalfeature -online -Feature Server-Gui-Mgmt).state -eq 'Enabled'){ 
  38.   "Server-Gui-Mgmt is already enabled" 
  39.  
  40. # Enable the features needed to add the GUI 
  41. Enable-WindowsOptionalFeature –Online -NoRestart ` 
  42.       -Featurename ServerCore-FullServer, Server-Gui-Shell,Server-Gui-Mgmt 
  43.  
  44. # So Restart the computer 
  45. Restart-computer 
  46.  
  47. Function Disable-ServerCoreGui { 
  48.  
  49. #    Import the module 
  50. Import-Module -Name DISM -ErrorAction Stop 
  51.  
  52. # Disable all the features needed to add the GUI 
  53. # if any are already disabled, oh well - they're still disabled. 
  54. Disable-WindowsOptionalFeature –Online -NoRestart ` 
  55.       -Featurename ServerCore-FullServer, Server-Gui-Shell,Server-Gui-Mgmt 
  56.  
  57. # Finally Restart the computer 
  58. Restart-computer 

Monday, 30 April 2012

Show-DnsConfiguration.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script shows the DNS Configuration  of NICs  
  4.     in your system 
  5. .DESCRIPTION 
  6.     This script is a re-write of an MSDN Sample  
  7.     using PowerShell./ The script gets all network 
  8.     active network interfaces then prints out that 
  9.     interfaces' DNS Properties. 
  10. .NOTES 
  11.     File Name  : Show-DnsConfiguration.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.net.networkinformation.networkinterface.getallnetworkinterfaces.aspx    
  19. .EXAMPLE 
  20.     Psh[C:\foo]> .\Show-DnsConfiguration.ps1 
  21.     Broadcom NetXtreme 57xx Gigabit Controller 
  22.       DNS suffix .............................. : cookham.net 
  23.       DNS enabled ............................. : False 
  24.       Dynamically configured DNS .............. : True 
  25.  
  26.     ... more interfaces snipped for brevity!     
  27. #> 
  28.  
  29. # Get the adapters than iterate over the collection and display DNS configuration 
  30. $adapters = [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces() 
  31. ForEach ($adapter in $adapters)   { 
  32.   $properties = $adapter.GetIPProperties() 
  33.   $adapter.Description 
  34.   "  DNS suffix .............................. : {0}" -f $properties.DnsSuffix 
  35.   "  DNS enabled ............................. : {0}" -f $properties.IsDnsEnabled 
  36.   "  Dynamically configured DNS .............. : {0}" -f $properties.IsDynamicDnsEnabled