#################################################################### # vNiklas Demo Collection TEC 2012 # Powershell is the way to handle Hyper-V 2012 # #################################################################### function start(){ Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force # Set the name for the DC Rename-Computer -NewName DC01 Restart-Computer } function SetIESecurity (){ # Change Security for IE http://www.vladcatrinescu.com/2012/06/how-to-disable-ie-enhanced-security.html Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A7-37EF-4b3f-8CFC-4F3A74704073}" -Name isinstalled -Value 0 Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A8-37EF-4b3f-8CFC-4F3A74704073}" -Name isinstalled -Value 0 Rundll32 iesetup.dll, IEHardenLMSettings,1,True Rundll32 iesetup.dll, IEHardenUser,1,True Rundll32 iesetup.dll, IEHardenAdmin,1,True If (Test-Path "HKCU:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A7-37EF-4b3f-8CFC-4F3A74704073}") { Remove-Item -Path "HKCU:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A7-37EF-4b3f-8CFC-4F3A74704073}" } If (Test-Path "HKCU:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A8-37EF-4b3f-8CFC-4F3A74704073}") { Remove-Item -Path "HKCU:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A8-37EF-4b3f-8CFC-4F3A74704073}" } #This doesn't always exist Remove-ItemProperty "HKCU:\SOFTWARE\Microsoft\Internet Explorer\Main" "First Home Page" -ErrorAction SilentlyContinue ######################################################## } function SetIPandAD(){ cd C:\Powershell ######################################################### # Correct IP for the DC and SMB server New-NetIPAddress -IPAddress 192.168.20.10 -InterfaceAlias "Ethernet" -AddressFamily IPv4 -PrefixLength 24 -DefaultGateway 192.168.20.2 # Create Domain Install-WindowsFeature AD-Domain-Services -IncludeManagementTools Install-ADDSForest -DomainName vniklas.com -InstallDns -SafeModeAdministratorPassword (convertto-securestring -string "RTS2011ab" -asplaintext -force) -Force ######################################################### } function CreateDHCP(){ cd C:\Powershell ############################################################## # Create DHCP Add-WindowsFeature -Name DHCP -IncludeManagementTools Add-DhcpServerv4Scope -Name "Test Vniklas" -StartRange 192.168.20.41 -EndRange 192.168.20.80 -SubnetMask 255.255.255.0 Set-DhcpServerv4OptionValue -ComputerName dc01.vniklas.com -DnsServer 192.168.20.10 -DnsDomain vniklas.com -Router 192.168.20.2 Add-DhcpServerInDC Set-DhcpServerv4DnsSetting -ComputerName dc01.vniklas.com -DynamicUpdates Always -DeleteDnsRRonLeaseExpiry $True } function Dedup(){ ################################################################ # Set dedup in action Get-WindowsFeature FS-Data-Deduplication | Add-WindowsFeature Enable-DedupVolume L: # Cannot run on Systemvolym Set-DedupVolume L: -MinimumFileAgeDays 0 Start-DedupJob L: -Type Optimization } function AddHost(){ cd C:\Powershell ################################################################ #winrm s winrm/config/client @{TrustedHosts="*"} # add Hyper-V host remote to AD (the hosts must have win-rm and trusted hosts configured $user = "localhost\administrator" $passwd = convertto-securestring -string "RTS2011ab" -asplaintext -force $cred = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $user, $passwd $user = "vniklas\administrator" $passwd = convertto-securestring -string "RTS2011ab" -asplaintext -force $Credential = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $user, $passwd $HostIPs = (Get-DhcpServerv4Lease -ScopeId 192.168.20.0).IPAddress.IPAddressToString foreach ($HostIP in $HostIPs){ $temp = $HostIP $index = "$($temp[-1])" $name = "HV0$index" Remove-ADComputer -Identity $name -Confirm:$false -ErrorAction SilentlyContinue Invoke-Command -ComputerName $HostIP -Credential $cred -ScriptBlock {Add-Computer -NewName $Using:name -Credential $Using:Credential -DomainName vniklas.com -Force ; Add-WindowsFeature Hyper-V -Restart -IncludeManagementTools} } } function RemoveHostAD(){ Invoke-Command -ComputerName (Get-DhcpServerv4Lease -ScopeId 192.168.20.0).IPAddress.IPAddressToString -Credential $cred -ScriptBlock {Remove-Computer -UnjoinDomainCredential $Using:Credential -Restart -Workgroup "Brun" -Force } } function ManageHyperV (){ ########################################################## # configure and manage hYpEr-V Get-WindowsFeature *rsat-hyp* | Add-WindowsFeature -IncludeAllSubFeature $Hosts = Get-ADObject -Filter 'Name -like "*Hyper-V"' | %{$_.DistinguishedName.Split(",")[1].replace("CN=","") } Set-VMhost -ComputerName $Hosts -VirtualMachinePath c:\VMs -VirtualHardDiskPath c:\VMs New-VMSwitch -ComputerName $Hosts -AllowManagementOS $true -Name Internal -NetAdapterName Ethernet Enable-VMMigration -ComputerName $Hosts Set-VMHost -ComputerName $Hosts –VirtualMachineMigrationAuthenticationType Kerberos -UseAnyNetworkForMigration $true } function SMBShare(){ ############################################################ # Create SMB share New-Item -Path C:\SMB3 -ItemType Directory New-SmbShare -Path C:\SMB3 -Name SMB3 -FullAccess vniklas\administrator,vniklas\hv01$ # Set rights on the folder also, not just the share (Get-SmbShare smb3).PresetPathAcl | Set-Acl } function SetDelegation(){ #################################################################################### # Enable KCD # Get-ADObject -Filter 'Name -like "HV01"' cd C:\Powershell $vHosts = Get-ADComputer -Filter 'Name -like "HV*"' $vHosts2 = $vHosts foreach ($vHost in $vHosts){ .\Set-KCD.ps1 -AdDN $vHost.DistinguishedName -HostFQDN "dc01.vniklas.com" -Service cifs -Add # Also Add all Hosts to be able to do live storage and migration between foreach ($vHost2 in $vHosts2){ if($vHost.Name -ne $vHost2.Name){ .\Set-KCD.ps1 -AdDN $vHost.DistinguishedName -HostFQDN $vHost2.DNSHostName -Service cifs -Add .\Set-KCD.ps1 -AdDN $vHost.DistinguishedName -HostFQDN $vHost2.DNSHostName -Service "Microsoft Virtual System Migration Service" -Add } } } } function createVMs(){ ################################################################ # Create VM´s .\Convert-WindowsImage.ps1 -SourcePath C:\ISOs\win2012.ISO -VHDPath c:\smb3\master.vhdx -SizeBytes 20GB -VHDFormat VHDX -VHDType Dynamic -Edition ServerStandardEvalCore .\Convert-WindowsImage.ps1 -SourcePath C:\ISOs\win2012.ISO -VHDPath c:\smb3\monitor.vhdx -SizeBytes 20GB -VHDFormat VHDX -VHDType Dynamic -Edition ServerStandardEval ############################################################### Move-Item C:\VMs\master.vhdx -Destination C:\SMB3\ .\InParallel.ps1 new-vm -ComputerName HV01 -NoVHD -Name Moffo -MemoryStartupBytes 130MB -SwitchName Internal -Path \\dc01\SMB3 new-vm -ComputerName HV01 -NoVHD -Name Groffo -MemoryStartupBytes 130MB -SwitchName Internal -Path C:\VMs } function createMonitorVM(){ ############################################################### # Add vm cluster monitoring move-item -Path C:\vms\monitor.vhdx -Destination C:\SMB3 new-vm -Name monitortest -MemoryStartupBytes 512MB -VHDPath \\dc01\SMB3\monitor.vhdx -Path \\dc01\SMB3 -SwitchName Internal -ComputerName hv01 Start-VM -VMName monitortest -ComputerName HV01 Get-VMIntegrationService -VMName monitortest -ComputerName HV01 # VM credentials : $user = "localhost\administrator" $passwd = convertto-securestring -string "RTS2011ab" -asplaintext -force $cred = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $user, $passwd # Host credentials $user = "vniklas\administrator" $passwd = convertto-securestring -string "RTS2011ab" -asplaintext -force $Credential = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $user, $passwd (Get-VMNetworkAdapter -VMName monitortest -ComputerName hv01).IPAddresses[0] Invoke-Command -ComputerName 192.168.20.42 -Credential $cred -ScriptBlock {Add-Computer -Credential $Using:Credential -DomainName vniklas.com -Restart -Force} get-vm -Name monitortest -ComputerName hv01 | Stop-VM } function MoveVMs(){ ############################################################### # Move VM´s live . .\Move-VMStorage2.ps1 Get-VM Moffo,Groffo -ComputerName HV01 | Start-VM Get-VM Groffo -ComputerName HV01 | Move-VMStorage -DestinationStoragePath \\DC01\SMB3\Groffo Move-VMStorage2 -VM Moffo -ComputerName HV01 -path c:\VMs\Moffo Move-VMStorage2 -VM Moffo -ComputerName HV01 -path \\dc01\smb3\Moffo Get-VM Moffo,Groffo -ComputerName HV01 | Stop-VM -TurnOff } function changeVMSettings(){ ############################################################### # Change VM Settings Set-VMBios TestVM* -StartupOrder @("Floppy", "LegacyNetworkAdapter", "CD", "IDE”) -ComputerName HV01 $VMs = get-vm test* -ComputerName hv01 Set-VM -VM $VMs -MemoryStartupBytes 132MB Get-VM -ComputerName HV01 | Get-VMNetworkAdapter | Set-VMNetworkAdapter -DhcpGuard on -RouterGuard on Get-VM test* -ComputerName HV01 | Start-VM Get-VM test* -ComputerName HV01 | Stop-VM -Force get-vm test* -ComputerName hv01 | Get-VMNetworkAdapter |%{Set-VMNetworkAdapter -VMNetworkAdapter $_ -StaticMacAddress $_.MacAddress} Get-VM test* -ComputerName HV01 | Start-VM } function SearchAD(){ ##################################################################################### # Search DC for VM´s in AD (that is registered to the AD Get-ADObject -Filter 'Name -like "*Virtual*"' | %{$_.DistinguishedName.Split(",")[1].replace("CN=","") } # Search after Hosts registered in the AD Get-ADObject -Filter 'Name -like "*Hyper-V"' | %{$_.DistinguishedName.Split(",")[1].replace("CN=","") } # Get the VM´s from the Hosts registerd in AD and get info and then do something with them Get-ADObject -Filter 'Name -like "*Hyper-V"' | %{$_.DistinguishedName.Split(",")[1].replace("CN=","") } | %{get-vm -ComputerName $_ -ErrorAction SilentlyContinue} | ft ComputerName,Name,State,Uptime -AutoSize Get-ADObject -Filter 'Name -like "*Hyper-V"' | %{$_.DistinguishedName.Split(",")[1].replace("CN=","") } | %{get-vm test* -ComputerName $_ -ErrorAction SilentlyContinue} | Stop-VM -Force } function Cluster(){ ######################################################### # Create cluster Get-WindowsFeature Failover-Clustering -ComputerName hv01 | Add-WindowsFeature -ComputerName hv01 Get-WindowsFeature rsat-cl* | Add-WindowsFeature New-Cluster -Name HYPCL30 -Node hv01 -NoStorage get-vm monitortest -ComputerName hv01 | Add-VMToCluster -Cluster hypcl30 #Its almost empty Get-VM -ClusterObject (Get-ClusterResource -Cluster hypcl30 | where ResourceType -eq "Virtual Machine") . .\Get-VMNotInCluster.ps1 $VMsOut = Get-VMNotInCluster -Cluster HYPCL30 $VMsOut | Add-VMToCluster -Cluster HYPCL30 } function MonitorVMService(){ #################################################### # These settings must be run on the VM for monitoring services #Set-NetFirewallRule -DisplayGroup "Virtual Machine Monitoring" -Enabled true #Set-NetFirewallRule -DisplayGroup "Remote Service Management" -Enabled true Get-VM monitortest -ComputerName HV01 | Start-VM Get-Service apache2 -ComputerName monitortest Add-ClusterVMMonitoredItem -Service apache2 -OverrideServiceRecoveryActions -VirtualMachine monitortest -Cluster hypcl30 Get-ClusterVMMonitoredItem -VirtualMachine monitortest -Cluster hypcl30 Invoke-Command -ComputerName monitortest -ScriptBlock {Get-Process httpd | Stop-Process -Force} Get-ClusterVMMonitoredItem -VirtualMachine monitortest -Cluster hypcl30 | Remove-ClusterVMMonitoredItem } function scheduleStuff(){ ############################################################### # Schedule stuff Enter-PSSession -ComputerName HV01 Get-command -Module *PSsch* $once = New-JobTrigger -Once -At 10:07AM Register-ScheduledJob -Name AddvCPU2 -ScriptBlock {$vm = Get-VM monitortest; Stop-VM -VM $vm ; Set-VM -VM $vm -ProcessorCount 2 ; Start-VM -VM $vm} -Trigger $once Import-Module PSScheduledJob Get-ScheduledJob | Remove-JobTrigger Get-ScheduledJob | Unregister-ScheduledJob Get-Job | Remove-Job Exit } function addWebAccess(){ ############################################### # Add Powershell web access Get-WindowsFeature WindowsPowerShellWebAccess | Add-WindowsFeature Install-PswaWebApplication -UseTestCertificate Add-PswaAuthorizationRule * * * } function DeathStar(){ ########################################################################## # Destruction cd C:\Powershell Get-VM -ClusterObject (Get-ClusterResource -Cluster HYPCL30 | where ResourceType -eq "Virtual Machine") | Remove-VMFromCluster -Cluster HYPCL30 -RemoveResources -Force Get-VM -ComputerName hv01 | %{ Stop-VM -VM $_ -Force; Remove-VM -vm $_ -Force ; Remove-Item -Path $_.Path -Recurse -Force} Get-VM -ComputerName hv01 | %{ Stop-VM -VM $_ -TurnOff -Force; Remove-VM -vm $_ -Force; .\Remove-VM2.ps1 -VM $_ } Stop-Computer -ComputerName (Get-Cluster hypcl30 | Get-ClusterNode).Name Get-ADuser -filter * | Disable-ADaccount logoff } function alliedforces (){ ############################################################################## # If you have access reboot with DVD cd d:\windows\system32\ copy utilman.exe utilman.exe_old copy cmd.exe utilman.exe reboot win+U net user Administrator /active:yes }