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