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