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