<#
.SYNOPSIS
MSDN sample showing push and other stack processing using PowerShell
.DESCRIPTION
This script creates a script then performs stack operations.
.NOTES
File Name : Get-Stack1.p1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell V2
.LINK
http://www.pshscripts.blogspot.com
.EXAMPLE
PSH [C:\foo]: .\get-stack1.ps1'
Stack at start:
fox
quick
The
(Pop) fox
Stack value after Pop:
brown
quick
The
(Pop) brown
Stack values after 2nd pop:
quick
The
(Peek) quick
Stack values after a peek:
quick
The
#>
##
# start of script
###
# Create and initialise a new stack object
$mystack = new-object system.collections.stack
$myStack.Push( "The" )
$myStack.Push( "quick" )
$myStack.Push( "brown" )
$myStack.Push( "fox" )
# Display the Stack
"Stack at start:"
$myStack
""# Pop an element from the Stack.
"(Pop)`t`t{0}" -f $myStack.Pop()
"Stack value after Pop:"
$myStack
""
# Pop another element from the Stack
"(Pop)`t`t{0}" -f $myStack.Pop()
# Display the Stack after 2nd pop
"Stack values after 2nd pop:"
$myStack
""
# Peek at the front
"(Peek)`t`t{0}" -f $myStack.peek()
# Display the Stack after the peek
"Stack values after a peek:"
$myStack
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Showing posts with label powershell. Show all posts
Showing posts with label powershell. Show all posts
Friday, 28 November 2014
Get-Stack1.ps1
Labels:
powershell,
system.collections.stack
Monday, 4 February 2013
Configure-DC1-2.ps1
- ####
- # Configure-DC1-2
- # Configures DC1 after dcpromo is completed
- #
- # Version Date What Changed
- # ------- ----------- -------------------------------------------
- # 1.0.0 14 Jan 2013 Initial release
- # 1.1.0 24 Jan 2013 Added code to count how long it all took,
- # Added checkpoint at the end of this script
- # 1.1.1 25 Jan 2013 Added auto admin logon
- # 1.1.2 5 Feb 2013 Added forced reboot of DC1-1 at script end
- ####
- # Configuration block
- $Conf = {
- $StartTime = Get-Date
- Write-Host "Starting at: $StartTime"
- # Set Credentials for use in this configuration block
- $User = "Reskit\Administrator"
- $Password = 'Pa$$w0rd'
- $PasswordSS = ConvertTo-SecureString -String $Password –AsPlainText `
- -Force
- $Dom = 'Reskit'
- $CredRK = New-Object `
- -Typename System.Management.Automation.PSCredential `
- -Argumentlist $User,$PasswordSS
- # Define registry path for autologon, then set admin logon
- $RegPath = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon'
- Set-ItemProperty -Path $RegPath -Name AutoAdminLogon `
- -Value 1 -EA 0
- Set-ItemProperty -Path $RegPath -Name DefaultUserName `
- -Value $User -EA 0
- Set-ItemProperty -Path $RegPath -Name DefaultPassword `
- -Value $Password -EA 0
- Set-ItemProperty -Path $RegPath -Name DefaultDomainName `
- -Value $Dom -EA 0
- # Install key Windows features for labs
- Write-Verbose 'Installing Windows fetures needed for DC1'
- $Features = @('PowerShell-ISE',
- 'Hyper-V-PowerShell',
- 'Rsat-AD-PowerShell',
- 'Web-Server','Web-Mgmt-Tools',
- 'Web-Mgmt-Console',
- 'Web-Scripting-Tools',
- 'Telnet-Client')
- Install-WindowsFeature @Features -IncludeManagementTools -Verbose
- # Install and configure DHCP
- Write-Verbose -Message 'Adding and then configuring DHCP'
- Install-WindowsFeature DHCP -IncludeManagementTools
- Add-DhcpServerV4Scope -Name "ReskitNet0" `
- -StartRange 10.0.0.100 `
- -EndRange 10.0.0.119 `
- -SubnetMask 255.255.255.0
- Set-DhcpServerV4OptionValue -DnsDomain Reskit.Org `
- -DnsServer 10.0.0.10
- Add-DhcpServerInDC -DnsName Dc1.reskit.org
- # Add users to the AD and then add them to some groups
- # Hash table for common new user paraemters
- Write-Verbose -Message
- $NewUserHT = @{AccountPassword = $PasswordSS;
- Enabled = $true;
- PasswordNeverExpires = $true;
- ChangePasswordAtLogon = $false
- }
- # Create one new user (me!) and add to enterprise and domain admins security groups
- New-ADUser @NewUserHT -SamAccountName tfl `
- -UserPrincipalName 'tfl@reskit.org' `
- -Name "tfl" `
- -DisplayName 'Thomas Lee'
- Add-ADPrincipalGroupMembership `
- -Identity "CN=tfl,CN=Users,DC=reskit,DC=org" `
- -MemberOf "CN=Enterprise Admins,CN=Users,DC=reskit,DC=org" ,
- "CN=Domain Admins,CN=Users,DC=reskit,DC=org"
- # Say nice things and finish
- $FinishTime = Get-Date
- Write-Verbose "Finished at: $FinishTime"
- Write-Verbose "DC1 Configuration took $(($FinishTime - $StartTime).TotalSeconds.ToString('n2')) seconds"
- } # End Conf configuration script block
- # Start of script proper
- # Set Credentials
- $Username = "Reskit\administrator"
- $Password = 'Pa$$w0rd'
- $PasswordSS = ConvertTo-SecureString -String $Password -AsPlainText
- -Force
- $CredRK = New-Object -Typename System.Management.Automation.PSCredential `
- -Argumentlist $Username,$PasswordSS
- # Following code used to test the credentials. Remove the comments on next two lines the first time you
- # run this script
- Invoke-Command -ComputerName DC1 -ScriptBlock {hostname} -Credential $Credrk -verbose
- Pause
- Invoke-Command -ComputerName DC1 -Scriptblock $conf -Credential $CredRK `
- -Verbose
- # OK - script block has completed - reboot the system and wait till it comes up
- Restart-Computer -ComputerName DC1 -Wait -For PowerShell –Force
- -Credential $CredRK
- # Finally, run a post-DCPromo snapshot
- Checkpoint-VM -VM $(Get-VM DC1) `
- -SnapshotName "DC1 - post configuration by ConfigureDC1-2.ps1"
Labels:
powershell,
PowerShell scripts,
PowerShell v3
Wednesday, 23 January 2013
Configure-DC1-1.ps1
- # Configure-DC1-1.ps1
- # Converts Server to DC
- # Version 1.0.0 - 14 Jan 2013
- # See http://tfl09.blogspot.com for more details
- # Config script block
- $conf = {
- # Install the AD - the reboot is or should be automagic
- Install-Windowsfeature AD-Domain-Services -IncludeManagementTools
- # Now install the AD to DC1
- $PasswordSS = ConvertTo-SecureString -string 'Pa$$w0rd' -AsPlainText -Force
- Install-ADDSForest -DomainName Reskit.Org -SafeModeAdministratorPassword $PasswordSS -force -InstallDNS -DomainMode Win2012 -ForestMode Win2012
- }
- # Here is start of script
- $Username = "DC1\administrator"
- $PasswordSS = ConvertTo-SecureString 'Pa$$w0rd' -AsPlainText -Force
- $Creddc1 = New-Object system.management.Automation.PSCredential $username,$PasswordSS
- # First, run a simple script block to check that the server is actually running and is the one we think it is
- Invoke-Command -ComputerName DC1 -ScriptBlock { ipconfig;hostname} -Credential $Creddc1 -verbose
- Pause
- # Now add create our forest/domain/dc
- Invoke-Command -ComputerName DC1 -Scriptblock $conf -Credential $Creddc1 -verbose
Labels:
deployment scripts,
Hyper-V,
powershell,
PowerShell scripts
Monday, 21 January 2013
Create-VM.PS1
- # Create-VM.ps1
- # Script that creates VMs
- # Version 1.0.0 - 20 Jan 2013
- # See http://tfl09.blogspot.co.uk/2013/01/building-hyper-v-test-lab-on-windows-8.html
- # First define the Create-VM Function
- Function Create-VM {
- #===================================================
- # Create a New VM
- #===================================================
- # Parameters are Name, Virtual Machine Path, path to reference Vhdx,
- # network switch to use, VM Memory, Unattend file, IP address and DNS
- # Server to set. Default values are specified in the Param block,
- # but these are normally overridden in the call to Create0VM
- [Cmdletbinding()]
- Param (
- $Name = "Server",
- $VmPath = "C:\v3",
- $ReferenceVHD = "C:\v3\Ref2012.vhdx",
- $Network = "Internal",
- $VMMemory = 512mb,
- $UnattendXML = "C:\v3\unattend.xml",
- $IPAddr = '10.0.0.250/24',
- $DnsSvr = '10.0.0.10'
- )
- $Starttime = Get-Date
- Write-Verbose "Starting Create-VM at $Starttime"
- Write-verbose "Creating VM: [$name]"
- Write-verbose "Path to VM : [$VMpath]"
- # Set path to differencing disk location
- $path = "$vmpath\$name.vhdx"
- Write-Verbose "Creating Disk at [$path]"
- # Add a new differencing VHDX, Based on parent parent
- $vmDisk01 = New-VHD –Path $path -Differencing –ParentPath $ReferenceVHD -ErrorAction Stop
- Write-Verbose "Added VM Disk [$VMdisk01], pointing to [ReferenceVHD]"
- # Create a New VM
- $VM = New-VM –Name $name –MemoryStartupBytes $VMMemory –VHDPath $VMDisk01.path -SwitchName $Network -Path $vmPath
- Write-Verbose "VM [$name] created"
- # Mount the Disk into the VM
- Mount-DiskImage -ImagePath $path
- $VHDDisk = Get-DiskImage -ImagePath $path | Get-Disk
- $VHDPart = Get-Partition -DiskNumber $VHDDisk.Number
- $VHDVolumeName = [string]$VHDPart.DriveLetter
- $VHDVolume = [string]$VHDPart.DriveLetter + ":"
- Write-verbose "Volume [$Volumename] created in VM [$name]"
- # Get Unattended.XML file
- Write-Verbose "Using Unattended XML file [$unattendXML]"
- # Open XML file
- $Xml = [xml](get-content $UnattendXML)
- # Change ComputerName
- Write-Verbose "Setting VM ComputerName to: [$name]"
- $Xml.unattend.settings.component | Where-Object { $_.Name -eq "Microsoft-Windows-Shell-Setup" } |
- ForEach-Object {
- if($_.ComputerName) {
- $_.ComputerName = $name
- }
- }
- # Change IP address
- Write-Verbose "Setting VM ComputerName to: [$name]"
- $Xml.unattend.settings.component | Where-Object { $_.Name -eq "Microsoft-Windows-TCPIP" } |
- ForEach-Object {
- if($_.Interfaces) {
- $ht='#text'
- $_.interfaces.interface.unicastIPaddresses.ipaddress.$ht = $IPAddr
- }
- }
- # Change DNS Server address
- # Use obscure way to create the #TEXT node
- Write-Verbose "Setting VM DNS address to: [$DNSSvr]"
- $Xml.Unattend.Settings.Component | Where-Object { $_.Name -eq "Microsoft-Windows-DNS-Client" } |
- ForEach-Object {
- if($_.Interfaces) {
- $ht='#text'
- $_.Interfaces.Interface.DNSServerSearchOrder.Ipaddress.$ht = $DNSSvr
- }
- }
- # Save XML File on Mounted VHDX differencing disk
- $xml.Save("$VHDVolume\Unattend.XML")
- Write-Verbose "Unattended XML file saved to vhd [$vhdvolume\unattend.xml]"
- # Dismount VHDX
- Write-Verbose "Dismounting disk image: [$Path]"
- Dismount-DiskImage -ImagePath $path
- # Update additional VM settings
- Write-Verbose 'Setting additional VM settings'
- Set-VM -Name $name -DynamicMemory
- Set-VM -Name $name -MemoryMinimumBytes $VMMemory
- Set-VM -Name $name -AutomaticStartAction Nothing
- Set-Vm -Name $name -AutomaticStopAction ShutDown
- # Show what has been created!
- "VM Created:"
- Get-VM -Name $name | fl *
- # Start VM
- Write-verbose "VM [$Name] being started"
- Start-VM -Name $name
- # Now work out and write how long it took to create the VM
- $Finishtime = Get-Date
- Write-Verbose ("Creating VB ($name) took {0} seconds" -f ($FinishTime - $Starttime).totalseconds)
- } # End of Create-VM function
- #######################################################################################################
- # CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS #
- # CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS #
- # CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS #
- # Location of Server 2012 DVD Iso Image
- $iso = 'c:\Builds\9200.16384.120725-1247_x64frev_Server_Datacenter_VL_HRM_SSS_X64FREV_EN-US_DVD.iso'
- # Where we put the reference VHDX
- $ref = 'c:\v3\Ref2012.vhdx'
- # Path were VMs, VHDXs and unattend.txt files live
- $path = 'c:\V3'
- # Location of Unattend.xml - first for workstation systems, second for domain joined systems
- $una = 'c:\V3\UnAttend.xml'
- $unadj = 'c:\V3\UnAttend.dj.xml'
- # CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS #
- # CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS #
- # CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS #
- #######################################################################################################
- # Now run the script to create the VMs as appropriate.
- $Start = Get-Date
- "Create-VM --- Started at: $Start"
- ####################################################################
- # Comment out VMs you do NOT want to create then run the entire script
- # To comment out a VM creation, just add a '#" at the start of the line.
- # Removing the comment line means you want to create that VM.
- # BE creful! If you make a mistake, stop the script. Kill any VMs created, then remove the
- # storage for the VMs.
- #######################################################################################################
- # Create the DC - NON-domained joined
- # Create-VM -name "DC1" -VmPath $path -ReferenceVHD $ref -Network "Internal" -UnattendXML $una -Verbose -IPAddr '10.0.0.10/24' -DNSSvr 10.0.0.10 -VMMemory 1gb
- # Remaining VMs use the domain-join version of unattend.xml
- # Create-VM -name "Srv1" -VmPath $path -ReferenceVHD $ref -Network "Internal" -UnattendXML $unadj -Verbose -IPAddr '10.0.0.30/24' -DNSSvr 10.0.0.10 -VMMemory 512mb
- # Create-VM -name "Srv2" -VmPath $path -ReferenceVHD $ref -Network "Internal" -UnattendXML $unadj -Verbose -IPAddr '10.0.0.31/24' -DNSSvr 10.0.0.10 -VMMemory 512mb
- # Create-VM -name "Sql1" -VmPath $path -ReferenceVHD $ref -Network "Internal" -UnattendXML $unadj -Verbose -IPAddr '10.0.0.20/24' -DNSSvr 10.0.0.10 -VMMemory 768mb
- # Create-VM -name "Exch1" -VmPath $path -ReferenceVHD $ref -Network "Internal" -UnattendXML $unadj -Verbose -IPAddr '10.0.0.21/24' -DNSSvr 10.0.0.10 -VMMemory 768mb
- # DHCP 1,2 for advanced networking class
- # Create-VM -name "DHCP1" -VmPath $path -ReferenceVHD $ref -Network "Internal" -UnattendXML $unadj -Verbose -IPAddr '10.0.0.51/24' -DNSSvr 10.0.0.10 -VMMemory 512mb
- # Create-VM -name "DHCP2" -VmPath $path -ReferenceVHD $ref -Network "Internal" -UnattendXML $unadj -Verbose -IPAddr '10.0.0.52/24' -DNSSvr 10.0.0.10 -VMMemory 512mb
- # Create a second DC for reskit.org for advanced class
- # Create-VM -name "DC2" -vmPath $path -ReferenceVHD $ref -network "Internal" -UnattendXML $unadj -Verbose -IPAddr '10.0.0.11/24' -DNSSvr 10.0.0.10 -VMMemory 512mb
- # script is all done - just say nice things and quit.
- $Finish = Get-Date
- "Create-VM --- Finished at: $Finish"
- "Elapsed Time : $(($Finish-$Start).totalseconds) seconds"
Sunday, 20 January 2013
Open-FunctionInISE
- Function Open-FunctioninISE {
- <#
- .SYNOPSIS
- Opens a function in ISE
- .DESCRIPTION
- This enables you to specify the function to open. The definition
- comes from (Get-Command <command>).definition.
- You specify the name of the function to open, or select it
- in the ISE
- .NOTES
- File Name : Open-FunctionInIse.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 3.0
- Based on a technet script published at http://gallery.technet.microsoft.com/scriptcenter/Open-defined-functions-in-22788d0f
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- .PARAMETER Function
- The name of a defined function to open in new PowerShell ISE tab.
- .EXAMPLE
- C:\Psh> Open-FunctionInIse Open-FunctioninISE
- Opens Open-FunctionInIse in a new PowerShell ISE tab
- .EXAMPLE
- Select a function name in an open edit window, then hit
- Ctrl+Shift+E (or use the Add-ons menu)
- #>
- # Define parameters
- [Cmdletbinding()]
- Param(
- [Parameter(Position=0)]
- [ValidateScript({ Get-Command -commandtype function -name $_ })]
- [String] $function
- )
- # Start of the function.
- Process{
- # Get the function name (i.e. the selected text) and ensure it's not empty
- $fn = $psise.currentfile.Editor.selectedText
- # if nothing selected, see if we got called with it
- If (!$fn -or ($fn.length -LE 0)) {
- if ($function -and ($function.length -GT 0)){
- $fn = $function}
- Else {
- $fn = Read-Host -Prompt "Enter function name to view"
- }
- }
- If (!$Fn) {'No function to edit';return}
- # Get the definition, if there is one
- $definition = (Get-Command -commandtype function -name $fn).definition
- If (!$definition -or ($Definition.Length -le 0)) {return "Function [$fn] not found"}
- # Create What to see
- $FunctionHeader = "Function $fn {`n"
- $comments = "`# Description : $($definition.description)`n"
- $comments += "`# Module : $($definition.module)`n`n"
- $FunctionTrailer = "`n}"
- # Wrap it all up
- $definition = $functionHeader + $Comments + $Definition + $FunctionTrailer
- "function $fn {" + $definition + "}"
- # Add a tab, add text to the tab, set caret position to first character
- $tab = $Psise.CurrentPowerShellTab.Files.Add()
- $tab.Editor.text = $definition
- $tab.Editor.SetCaretPosition(1,1)
- # Sleep for a moment. Ran into issues without this.
- Start-Sleep -Milliseconds 200
- } # End process block
- } # End Function
- Set-ALias OFISE .\Open-FunctionInISE
- # Here we could test the function from the command line.
- # Function test123 {'testing 1-2-3'}
- # Open-FunctioninISE test123
- # I assume you'll just use this function so I've left the following
- # Code in place to add this as a menu item
- # Here add to the ISE as long as it's not there already!
- $x = ($psise.CurrentPowerShellTab.AddOnsMenu.Submenus).displayname
- if (! ($x -contains "_Edit Function in ISE")) {
- $Psise.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("_Edit Function in ISE", {Open-FunctioninISE},
- "Ctrl+Shift+E") | Out-Null
- }
- Else {
- 'Function already added in ISE'
- }
Labels:
powershell,
Powershell ISE,
PowerShell v3
Tuesday, 15 January 2013
Create-ReferenceVHDX.ps1
- # Create-ReferenceVHDX.ps1
- # Script that will create a reference VHDX for later VM Creation
- # Version 1.0.0 - 14 Jan 2013
- # Define a function to create a reference VHDX.
- Function Create-ReferenceVHDX {
- [Cmdletbinding()]
- Param (
- # ISO of OS
- [string] $Iso = 'C:\downloads\9200.16384.120725-1247_x64frev_Server_Datacenter_VL_HRM_SSS_X64FREV_EN-US_DVD.iso',
- # Path to reference VHD
- [string] $RefVHDXPath = "C:\vhd\Ref2012.vhdx"
- )
- # Get start time
- $StartTime = Get-Date
- Write-Verbose "Beginning at $StartTime"
- #--------------------------------------------------+
- # Mount an ISO and check out available OS versions!
- #--------------------------------------------------+
- # Import the DISM module
- Write-Verbose 'Loading DISM module' -Verbose:$false
- Import-Module -Name DISM -Verbose:$False
- # Mount the OS ISO image onto the local machine
- Write-Verbose "Mounting ISO image [$iso]"
- Mount-DiskImage -ImagePath $iso
- # Get the Volume the Image is mounted to
- Write-Verbose 'Getting disk image of the ISO'
- $ISOImage = Get-DiskImage -ImagePath $ISO | Get-Volume
- Write-Verbose "Got disk image [$($ISOImage.DriveLetter)]"
- # And get the drive Letter of the dirve where the image is mounted
- # add the drive letter separator (:)
- $ISODrive = [string]$ISOImage.DriveLetter+":"
- Write-Verbose "OS ISO mounted on drive letter [$ISODrive]"
- # Next we will get the installation versions from the install.wim.
- # $Indexlist is the index of WIMs on the DVD - display the versions
- # available in the DVD and let user select the one to serve as the base
- # image - probably DataCentre Full Install
- $IndexList = Get-WindowsImage -ImagePath $ISODrive\sources\install.wim
- Write-Verbose 'Got Index list - displaying it now!'
- # Display the list and return the index
- $item = $IndexList | Out-GridView -OutputMode Single
- $index = $item.ImageIndex
- Write-Verbose "Selected image index [$index]"
- #---------------------------------+
- # Create a Reference Image !
- #---------------------------------+
- # Create the VHDX for the reference image
- $VMDisk01 = New-VHD –Path $RefVHDXPath -SizeBytes 15GB
- Write-Verbose "Created VHDX File [$($vmdisk01.path)]"
- # Get the disk number
- Mount-DiskImage -ImagePath $RefVHDXPath
- $VHDDisk = Get-DiskImage -ImagePath $RefVHDXPath | Get-Disk
- $VHDDiskNumber = [string]$VHDDisk.Number
- Write-Verbose "IReference image is on disk number [$VhddiskNumber]"
- # Create a New Partition
- Initialize-Disk -Number $VHDDiskNumber -PartitionStyle MBR
- $VHDDrive = New-Partition -DiskNumber $VHDDiskNumber -UseMaximumSize -AssignDriveLetter -IsActive | Format-Volume -Confirm:$false
- $VHDVolume = [string]$VHDDrive.DriveLetter+":"
- Write-Verbose "VHD drive [$vhddrive], Vhd volume [$vhdvolume]"
- # Execute DISM to apply image to base disk
- Write-Verbose 'Using DISM to apply image to the volume'
- Write-Verbose 'This will take some time'
- Dism.exe /apply-Image /ImageFile:$ISODrive\Sources\install.wim /index:$Index /ApplyDir:$VHDVolume\
- # Execute BCDBoot so volume will boot
- Write-Verbose 'Setting BCDBoot'
- BCDBoot.exe $VHDVolume\Windows /s $VHDVolume /f BIOS
- # Dismount the Images
- Write-Verbose "Dismounting ISO and new disk"
- Dismount-DiskImage -ImagePath $ISO
- Dismount-DiskImage -ImagePath $RefVHDXPath
- Write-Verbose "Created Reference Disk [$RefVHDXPath]"
- Get-ChildItem $RefVHDXPath
- $FinishTime = Get-Date
- $tt= $FinishTime - $StartTime
- Write-Verbose "Finishing at $FinishTime"
- Write-verbose "Creating base image took [$($tt.totalminutes)] minutes"
- } # End of Create-ReferenceVHDX
- ################################################################################################################
- # CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS #
- # CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS #
- # CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS #
- # Path to Server 2012 DVD
- $iso = 'C:\Builds\9200.16384.120725-1247_x64frev_Server_Datacenter_VL_HRM_SSS_X64FREV_EN-US_DVD.iso'
- # PathTo the reference VDHX is to go
- $refvhdxpath = 'C:\V3\ref2012.vhdx'
- # CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS #
- # CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS #
- # CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS #
- #################################################################################################################
- Create-ReferenceVHDX -iso $iso -RefVHDXPath $RefVHDXPath -Verbose
Labels:
deployment scripts,
powershell,
PowerShell v3
Wednesday, 3 October 2012
Get-GuiMode.ps1
- <#
- .SYNOPSIS
- This script gets the current GUI mode of a Windows
- Server 2012 system, including current display resolution.
- .DESCRIPTION
- This script creates a custom object, then populates it
- with the current mode (Server Core, MinShell, Full Shell)
- and with the current resolution (resolution plus h/w). The
- display information object is then returned.
- .NOTES
- File Name : Get-GuiMode.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 3.0
- Windows Server 2012
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- .EXAMPLE
- Psh> ./Get-GuiMode
- ComputerName : S1
- ServerCore : True
- FullServer : False
- MinShell : False
- Resolution : 1024x768
- Height : 768
- Width : 1024
- #>
- Function Get-GUIMode {
- # Create a new object to return
- $guimode = new-object psobject
- # Add ComputerName to the object
- $guimode |Add-Member -MemberType NoteProperty -Name ComputerName -Value $(hostname)
- # now determine what's installed
- $sgs = (Get-WindowsFeature Server-Gui-Shell).Installed
- $mif = (Get-WindowsFeature Server-Gui-Mgmt-Infra).Installed
- If (!$sgs -and !$mif) # True Server Core
- {
- $guimode |Add-Member -MemberType NoteProperty -Name ServerCore -Value $True
- $guimode |Add-Member -MemberType NoteProperty -Name FullServer -Value $False
- $guimode |Add-Member -MemberType NoteProperty -Name MinShell -Value $False
- }
- Elseif ($sgs -and !$mif) # MinShell
- {
- $guimode |Add-Member -MemberType NoteProperty -Name ServerCore -Value $False
- $guimode |Add-Member -MemberType NoteProperty -Name FullServer -Value $False
- $guimode |Add-Member -MemberType NoteProperty -Name MinShell -Value $True
- }
- Elseif ($sgs -and $mif)
- {
- $guimode |Add-Member -MemberType NoteProperty -Name ServerCore -Value $False
- $guimode |Add-Member -MemberType NoteProperty -Name FullServer -Value $True
- $guimode |Add-Member -MemberType NoteProperty -Name MinShell -Value $False
- }
- # now resolution
- If ($rx=get-command Get-DisplayResolution) {
- $res = (Get-DisplayResolution)[0]
- $reslen = $res.length
- $r = [string]""
- for ($i = 0; $i -lt $reslen; $i++)
- {
- If ($res.substring($i,1) -ne "") { $r += $res.substring($i,1) }
- }
- $guimode |Add-Member -MemberType NoteProperty -Name Resolution -Value $r
- $h = $r.split("x")[1] # height
- $w = $r.split("x")[0] #
- $guimode |Add-Member -MemberType NoteProperty -Name Height -Value $h
- $guimode |Add-Member -MemberType NoteProperty -Name Width -Value $w
- }
- # Ok - squirt out what we have!
- $guimode
- }
- # Here Test it out
- Get-GUiMode
Labels:
powershell,
PowerShell v3,
Server2012,
ServerCore
Set-PowerShellAsShell
- <#
- .Synopsis
- Creates a function to set PowerShell as GUI in Server 2012
- .DESCRIPTION
- The function in this script sets PowerShell as the
- default shell in Server 2012. When the server is rebooted,
- it runs PowerShell.exe by default. When PowerShell starts, it
- displays the $PSVersionTable variable.
- .NOTES
- File Name : Set-PowerShellAsGui.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : Server 2012
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- .EXAMPLE
- Left as an exercise to the reader
- #>
- Function Set-PowerShellAsShell {
- [CmdletBinding()]
- Param (
- [switch] $Reboot = $false
- )
- # Create Registry Path variable
- $RegPath = "Microsoft.PowerShell.Core\Registry::"
- $RegPath += "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\"
- $RegPath += "Windows NT\CurrentVersion\winlogon"
- # Create splatted parameter hash table
- $parm = @{Path = $regpath} # key
- $parm += @{Name = 'Shell'} # value name
- $parm += @{Value = 'PowerShell.exe –NoExit
-Command "$psversiontable"'} # value’s value - # Set Registry value entry
- Set-ItemProperty @parm
- # And restart to see PowerShell
- if ($Reboot) {Restart-Computer -confirm}
- }
Labels:
powershell,
PowerShell scripts,
PowerShell V2,
PowerShell v3,
Server,
Server2012,
ServerCore
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
Subscribe to:
Posts (Atom)