Showing posts with label ServerCore. Show all posts
Showing posts with label ServerCore. Show all posts

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} 

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

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