- <#
- .SYNOPSIS
- This example shows how to create a new directory and
- subdirectory, and then delete only the subdirectory.
- .DESCRIPTION
- This sample is a re-write of an MSDN Sample,
- but in PowerShell. The sample firsts creates then removes
- a folder then looks to see what is left. The target
- folder is removed, but intermediate folders remain.
- .NOTES
- File Name : Show-FolderCreation.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/62t64db3.aspx
- .EXAMPLE
- Psh> Show-FolderCreation.ps1
- Created: C:\NewDirectory\NewSubDirectory
- Deleted: C:\NewDirectory\NewSubDirectory
- Top-level directory exists: True
- Sub-directory exists : False
- #>
- [CmdletBinding()]
- Param (
- $Path = "C:\NewDirectory\NewSubDirectory"
- )
- Try
- {
- # Create then remove directory
- $result = [System.IO.Directory]::CreateDirectory($Path)
- "Created: $path"
- [System.IO.Directory]::Delete($Path)
- "Deleted: $path"
- # Check existance for top and sub dirs then display results
- $directoryExists = [System.Io.Directory]::Exists("C:\NewDirectory")
- $subDirectoryExists = [System.Io.Directory]::Exists($Path)
- "Top-level directory exists: $directoryExists"
- "Sub-directory exists : $subDirectoryExists"
- }
- Catch
- {
- "The process failed: {0}" -f $($error[0].Message)
- }
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Monday, 1 October 2012
Show-FolderCreation.ps1
Sunday, 23 September 2012
EchoArgs.ps1
- <#
- .SYNOPSIS
- This script contains a function, EchoArgs, that returns a list of
- the arguments passed and their type. It is used to demonstrate
- the use of the --% operator when calling a function or cmdlet, a new
- feature in PowerShell v3.
- .DESCRIPTION
- The EchoArgs function takes the arguments passed, via $args, and
- displays each argument and its type. Then, this function is called
- first with a normal calling sequence and then using --%.
- .NOTES
- File Name : EchoArgs.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library.
- .EXAMPLE
- Psh[Cookham8:C:\foo]>E:\PowerShellScriptLib\SERVER2012FEATURES\EchoArgs.ps1
- Calling Echoargs with 'fasdf $(LS) 2334 {asdf}'
- Argument [0]: [fasdf] Type:System.String
- Argument [1]: [System.Object[]] Type:System.Object[]
- Argument [2]: [2334] Type:System.Int32
- Argument [3]: [asdf] Type:System.Management.Automation.ScriptBlock
- Calling Echoargs with '--% asdf; {asfd}-a asdf'
- Argument [0]: [--%] Type:System.String
- Argument [1]: [asdf; $(ls) {asfd} - a asdf] Type:System.String
- #>
- # EchoArgs function
- Function EchoArgs {
- #Loop through and display each argument passed
- For ($i = 0; $i -ilt $Args.count; $i ++) {
- "Argument [{0}]: [{1}] Type:{2}" -f $I, $args[$i],$($args[$i].GetType().FullName)
- }
- }
- # Test it
- "Calling Echoargs with 'fasdf `$(LS) 2334 {asdf}'"
- Echoargs fasdf $(ls) 2334 {asdf}
- "";"Calling Echoargs with '--% asdf; {asfd}-a asdf'"
- Echoargs --% asdf; $(ls) {asfd} - a asdf
Technorati Tags: PowerShell,PowerShell V3. --% operator
Tuesday, 18 September 2012
Disable-Gui.ps1
- <#
- .SYNOPSIS
- This script defines a function, Disable-Gui which
- disables the GUI on Windows Server 2012 Server Core
- .DESCRIPTION
- The Disable-GUI function enables the GUI in Server 2012
- Server Core by Removing two windows features. The
- script add a Shell setting to ensure that when
- Server 2012 restarts, it starts with PowerShell.
- .NOTES
- File Name : Disable-GUI.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 3.0 and Windows Server 2012.
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- .EXAMPLE
- Psh> Disable-GUI -Verbose
- Removing Windows Feature: Server-GUI-Shell, Server-Gui-Mgmt-Infra
- Setting Shell Registry setting to PowerShell
- Restarting the computer - please be patient
- < after reboot, full GUI is added >
- #>
- Function Disable-GUI {
- # No parameters - but maybe later
- # Turn on CmdletBinding to enable -Verbose
- [Cmdletbinding()]
- Param()
- # Remove features from main Server core and downgrade GUI to Server Core
- Write-Verbose "Removing Windows Feature: Server-GUI-Shell, Server-Gui-Mgmt-Infra"
- Uninstall-WindowsFeature Server-Gui-Shell, Server-Gui-Mgmt-Infra
- # Add in PowerShell as the shell
- Write-Verbose "Setting Shell Registry setting to PowerShell"
- $RegPath = "Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\winlogon"
- Set-ItemProperty -Path $RegPath -Name Shell -Value 'PowerShell.exe -noExit -Command "$psversiontable"' -Force
- # Now restart the system
- Write-Verbose "Restarting the computer - please be patient"
- Restart-Computer
- }
Labels:
PowerShell v3,
Server2012,
ServerCore
Enable-GUI.ps1
- <#
- .SYNOPSIS
- This script defines a function, Enable-Gui which
- enables the GUI on Windows Server 2012 Server Core
- .DESCRIPTION
- The Enable-GUI function enables the Gui in Server 2012
- Server Core by adding in two windows features. The
- script removes any Shell setting to ensure that when
- Server 2012 restarts, it starts with the full Desktop.
- .NOTES
- File Name : Enable-GUI.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 3.0 and Windows Server 2012.
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- .EXAMPLE
- Psh> Enable-GUI -Verbose
- Installing Windows Feature: Server-GUI-Shell, Server-Gui-Mgmt-Infra
- Removing Shell Registry Setting
- Finished installation, now rebooting
- < after reboot, full GUI is added >
- #>
- Function Enable-GUI {
- # No parameters - but maybe later
- # Turn on CmdletBinding to enable -Verbose
- [Cmdletbinding()]
- Param()
- # Install the GUI
- Write-Verbose "Installing Windows Feature: Server-GUI-Shell, Server-Gui-Mgmt-Infra"
- Install-WindowsFeature Server-Gui-Shell, Server-Gui-Mgmt-Infra -Source d:\sources\sxs
- # Remove the Setting For Shell to force back to CMD.EXE
- Write-Verbose 'Removing Shell Registry Setting'
- $RegPath = "Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\winlogon"
- Remove-ItemProperty -Confirm -Path $RegPath -Name Shell -ErrorAction SilentlyContinue
- # And reboot the system
- Write-Verbose "Finished installation, now rebooting"
- Restart-Computer
- }
Labels:
PowerShell v3,
Server2012,
ServerCore
Sunday, 16 September 2012
Set-HyperVHostDefault.ps1
- <#
- .SYNOPSIS
- This script demonstrates setting default values for Local Hyper-V host.
- .DESCRIPTION
- This script imports the Hyper-V module then uses it
- to set certain default values for this hyper-V Host
- .NOTES
- File Name : Set-HyperVHostDefault.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 3.0 and Windows 8/Server 2012
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- .EXAMPLE
- Left as an exercise for the reader
- #>
- # Import Hyper-V Module
- Import-Module Hyper-V
- Write-Verbose "$((gcm -module hyper-v).Count) cmdlets imported in Hyper-V Module"
- # Create $parm hash table!
- $parm = @{}
- # Specify the Computername
- $parm += @{ComputerName = "Win8.Cookham.Net"}
- # Specify the Hard Disk Path
- $parm += @{VirtualMachinePath = "E:\hyperv"}
- # Specify the VHD Disk Path
- $parm += @{VirtualHardDiskPath = "E:\hyperv"}
- # Set the VM host accordingly
- Write-Verbose "Setting parameters as follows:";$parm
- Set-VmHost @parm
- # And Display Details
- Get-VMHost
- # End Set-HyperVHostDefaults
New-InternalSwitch.ps1
- <#
- .SYNOPSIS
- This script demonstrates creating a Hyper-V Switch
- .DESCRIPTION
- This script imports the Hyper-V module then uses it
- to create an Internal Switch for use in future provisioning.
- .NOTES
- File Name : New-InternalSwitch.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 3.0 and Windows 8/Server 2012
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- .EXAMPLE
- C:\foo\> .New-InternalSwitch.ps1
- VERBOSE: New-InternalSwitch will create a new virtual network.
- Name SwitchType NetAdapterInterfaceDescription
- ---- ---------- ------------------------------
- Internal Internal
- #>
- # Import Hyper-V Module
- Import-Module Hyper-V
- Try {New-VMSwitch -Name Internal -SwitchType Internal -ComputerName LocalHost -Verbose}
- Catch { "Failed to create switch"; $error[0] }
- # End New-InternalSwitch.ps1
Tuesday, 7 August 2012
Show-Formatting1.ps1
- <#
- .SYNOPSIS
- MSDN Sample Recoded in PowerShell demonstrating formatting
- .DESCRIPTION
- This sample recodes an MSDN Sample into PowerShell that
- shows some of the options of formatting using ToString() and
- various .NET formatting strings
- .NOTES
- File Name : Show-Formatting1.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/0c899ak8.aspx
- .EXAMPLE
- Psh> .\Show-Formatting1.ps1\
- 00123
- 1.20
- 01.20
- 01,20
- 0.6
- 1,234,567,890
- 1.234.567.890
- 1,234,567,890.1
- 1,234.57
- #>
- ## Start script
- [double] $value = 123;
- $value.ToString("00000")
- # Displays 00123
- $value = 1.2;
- $value.ToString("0.00", [System.Globalization.CultureInfo]::InvariantCulture)
- # Displays 1.20
- $value.ToString("00.00",[System.Globalization.CultureInfo]::InvariantCulture)
- # Displays 01.20
- $value.ToString("00.00",
- [System.Globalization.CultureInfo]::CreateSpecificCulture("da-DK"))
- # Displays 01,20
- $value = .56
- $value.ToString("0.0", [System.Globalization.CultureInfo]::InvariantCulture)
- # Displays 0.6
- $value = 1234567890
- $value.ToString("0,0", [System.Globalization.CultureInfo]::InvariantCulture)
- # Displays 1,234,567,890
- $value.ToString("0,0",
- [System.Globalization.CultureInfo]::CreateSpecificCulture("el-GR"))
- # Displays 1.234.567.890
- $value = 1234567890.123456;
- $value.ToString("0,0.0", [System.Globalization.CultureInfo]::InvariantCulture)
- # Displays 1,234,567,890.1
- $value = 1234.567890;
- $value.ToString("0,0.00", [System.Globalization.CultureInfo]::InvariantCulture)
- # Displays 1,234.57
Thursday, 5 July 2012
Manage-ServerCoreGui.ps1
- <#
- .Synopsis
- This file contains two simple functions to manage the GUI
- in Server Core Windows Server 2012.
- .DESCRIPTION
- There are two functions: Enable-ServerCoreGui that enables
- the GUI in a server core installation. Diable-ServerCoreGuI
- disables the GUI and returns the installation to
- traditional Server Core.
- Enable-ServerCoreGui also checks to see if some or all
- parts are already installed andwarns accordingly.
- .EXAMPLE
- c:\foo> Enable-ServerCoreGui
- This enables the GUI and reboots the Server into FUll GUI mode.
- .EXAMPLE
- c:\foo> Disable-ServerCoreGui
- This disables the GUI and reboots the Server back to Server Core mode.
- #>
- Function Enable-ServerCoreGui {
- # Import the module
- Import-Module -Name DISM -ErrorAction Stop
- # is it already enabled??
- if ((Get-WindowsOptionalfeature -online -Feature ServerCore-FullServer).state -eq 'Enabled'){
- "Servercore-FullServer is already enabled"
- }
- if ((Get-WindowsOptionalfeature -online -Feature Server-GUI-Shell).state -eq 'Enabled'){
- "Server-GUI Shell is already enabled"
- }
- if ((Get-WindowsOptionalfeature -online -Feature Server-Gui-Mgmt).state -eq 'Enabled'){
- "Server-Gui-Mgmt is already enabled"
- }
- # Enable the features needed to add the GUI
- Enable-WindowsOptionalFeature –Online -NoRestart `
- -Featurename ServerCore-FullServer, Server-Gui-Shell,Server-Gui-Mgmt
- # So Restart the computer
- Restart-computer
- }
- Function Disable-ServerCoreGui {
- # Import the module
- Import-Module -Name DISM -ErrorAction Stop
- # Disable all the features needed to add the GUI
- # if any are already disabled, oh well - they're still disabled.
- Disable-WindowsOptionalFeature –Online -NoRestart `
- -Featurename ServerCore-FullServer, Server-Gui-Shell,Server-Gui-Mgmt
- # Finally Restart the computer
- Restart-computer
- }
Monday, 30 April 2012
Show-DnsConfiguration.ps1
- <#
- .SYNOPSIS
- This script shows the DNS Configuration of NICs
- in your system
- .DESCRIPTION
- This script is a re-write of an MSDN Sample
- using PowerShell./ The script gets all network
- active network interfaces then prints out that
- interfaces' DNS Properties.
- .NOTES
- File Name : Show-DnsConfiguration.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface.getallnetworkinterfaces.aspx
- .EXAMPLE
- Psh[C:\foo]> .\Show-DnsConfiguration.ps1
- Broadcom NetXtreme 57xx Gigabit Controller
- DNS suffix .............................. : cookham.net
- DNS enabled ............................. : False
- Dynamically configured DNS .............. : True
- ... more interfaces snipped for brevity!
- #>
- # Get the adapters than iterate over the collection and display DNS configuration
- $adapters = [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces()
- ForEach ($adapter in $adapters) {
- $properties = $adapter.GetIPProperties()
- $adapter.Description
- " DNS suffix .............................. : {0}" -f $properties.DnsSuffix
- " DNS enabled ............................. : {0}" -f $properties.IsDnsEnabled
- " Dynamically configured DNS .............. : {0}" -f $properties.IsDynamicDnsEnabled
- }
Show-NetworkInterfaces2.ps1
- <#
- .SYNOPSIS
- This script, shows details about the Network Interfaces
- on a system. This is a re-write of an MSDN script into
- PowerShell
- .DESCRIPTION
- This script Uses several .NET classes to get the details
- of the interfaces on the system, then displays these details.
- Note in the MSDN Script, there were calls to other MSDN Samples.
- To make things simple, I have removed these calls.
- .NOTES
- File Name : Show-NetworkInterfaces2.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface.aspx
- .EXAMPLE
- Psh[C:\foo]> .\Show-NetworkInterfaces2.ps1
- Interface information for Cookham8.cookham.net
- Number of interfaces .................... : 2
- Broadcom NetXtreme 57xx Gigabit Controller
- ==========================================
- Interface type .......................... : Ethernet
- Physical Address ........................ : 001E4F955CC4
- Operational status ...................... : Up
- IP version .............................. : IPv4, IPv6
- DNS suffix .............................. : cookham.net
- MTU...................................... : 1500
- WINS Servers ............................ :
- 10.10.1.101
- DNS enabled ............................. : False
- Dynamically configured DNS .............. : True
- Receive Only ............................ : False
- Multicast ............................... : True
- Microsoft ISATAP Adapter
- ========================
- Interface type .......................... : Tunnel
- Physical Address ........................ : 00000000000000E0
- Operational status ...................... : Down
- IP version .............................. : IPv4, IPv6
- DNS suffix .............................. : cookham.net
- MTU...................................... :
- DNS enabled ............................. : False
- Dynamically configured DNS .............. : True
- Receive Only ............................ : False
- Multicast ............................... : False
- #>
- # First, get network properties of, and the nics in, this system
- $computerProperties = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()
- $nics = [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces()
- "Interface information for {0}.{1}" -f $computerProperties.HostName, $computerProperties.DomainName
- # Do we have nics??
- If (!$nics -or $nics.Length -lt 1)
- {
- " No network interfaces found."
- return
- }
- # Show interface details
- " Number of interfaces .................... : {0}" -f $nics.Length
- ForEach ($adapter in $nics)
- {
- $properties = $adapter.GetIPProperties()
- ""
- $adapter.Description
- "=" * $adapter.Description.Length
- " Interface type .......................... : {0}" -f $adapter.NetworkInterfaceType
- " Physical Address ........................ : {0}" -f $adapter.GetPhysicalAddress().ToString()
- " Operational status ...................... : {0}" -f $adapter.OperationalStatus
- # Create a display string for the supported IP versions.
- $versions =""
- if ($adapter.Supports([System.Net.NetworkInformation.NetworkInterfaceComponent]::IPV4 ))
- {
- $versions = "IPv4"
- }
- if ($adapter.Supports([System.Net.NetworkInformation.NetworkInterfaceComponent]::IPv6))
- {
- if ($versions.Length -gt 0)
- {
- $versions += ", ";
- }
- $versions += "IPv6";
- }
- " IP version .............................. : {0}" -f $versions
- # The remaining information is not useful for loopback adapters.
- if ($adapter.NetworkInterfaceType -eq [System.Net.NetworkInformation.NetworkInterfaceType]::Loopback)
- {
- continue
- }
- " DNS suffix .............................. : {0}" -f $properties.DnsSuffix
- if ($adapter.Supports([System.Net.NetworkInformation.NetworkInterfaceComponent]::IPv4))
- {
- $ipv4 = $properties.GetIPv4Properties()
- " MTU...................................... : {0}" -f $ipv4.Mtu
- if ($ipv4.UsesWins)
- {
- $winsServers = $properties.WinsServersAddresses
- if ($winsServers.Count -gt 0)
- {
- " WINS Servers ............................ :";
- foreach ($Winserver in $winsServers) {
- " {0}" -f $Winserver.IpAddressToString
- }
- }
- }
- }
- " DNS enabled ............................. : {0}" -f $properties.IsDnsEnabled
- " Dynamically configured DNS .............. : {0}" -f $properties.IsDynamicDnsEnabled
- " Receive Only ............................ : {0}" -f $adapter.IsReceiveOnly
- " Multicast ............................... : {0}" -f $adapter.SupportsMulticast
- } # End Foreach
Subscribe to:
Posts (Atom)