- # 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
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Wednesday, 23 January 2013
Configure-DC1-1.ps1
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
Subscribe to:
Posts (Atom)