Skip to content

Instantly share code, notes, and snippets.

View tomtorggler's full-sized avatar
😁
gruntled

Thomas Torggler tomtorggler

😁
gruntled
View GitHub Profile
@tomtorggler
tomtorggler / Create-NetFirewallRuleSQL.ps1
Last active February 17, 2016 08:16
Create NetFirewall Rules for SQL Server and SQL Browser executables
# get SQL services and path to executable
$services = Get-CimInstance -query 'select * from win32_service where name="sqlbrowser" or name="mssqlserver"'
# Create Firewall Rule for each of the services executables
# use splatting for better readability
foreach ($service in $services) {
$params = @{
DisplayName = $service.Name;
Action = "Allow";
Description ="Allow $($service.name)";
# Set Location to my working directory and import helper module for Nano Server image generation
Set-Location C:\Nano
Import-Module .\NanoServerImageGenerator.psm1
# Harvest four domain join blobs for the Nano Servers.
# Servernames will be n01 through n04
1..4 | ForEach-Object {
djoin /Provision /Domain vdi.local /Machine n0$_ /SaveFile n0$_.txt
}
# Import the VHD files as WDS Install Images
1..4 | ForEach-Object {
$params = @{
Path= "C:\Nano\Nano_compute_n0$_.vhd";
NewImageName = "Nano-n0$_-10.0.10586"
}
Import-WdsInstallImage @params
}
# Create CIM Sessions to the Nano Servers
$cimSessions = New-CimSession -ComputerName n01,n02,n03,n04
# Get system uptime via CIM to verify the servers
$params = @{
CimSession = $cimSessions;
ClassName = "Win32_OperatingSystem"
}
Get-CimInstance @params | Select-Object PsComputerName,@{Name='UpTime';Expression={$_.LocalDateTime - $_.LastBootUpTime}}
# create CimSessions to each of the servers to be configured
$cimSessions = New-CimSession -ComputerName n01,n02,n03,n04
# foreach CimSession configure an IPv4 Address on the interface with Alias Ethernet 2
# the IP Address is derived from the ComputerName property of the CimSession
$cimSessions | ForEach-Object {
$params = @{
IPAddress = "192.168.1."+$PSItem.ComputerName.Substring(2,1);
InterfaceAlias = 'Ethernet 2';
PrefixLength = '24';
# Create Storage Pool
Invoke-Command -ComputerName n01.vdi.local -ScriptBlock {
$params = @{
StorageSubSystemFriendlyName = "Cluste*";
FriendlyName = "s2d-pool";
PhysicalDisks = (Get-PhysicalDisk -StorageSubSystem (Get-StorageSubSystem -FriendlyName cluster*) -CanPool $true)
}
New-StoragePool @params
}
# Create Volume
Invoke-Command -ComputerName n01.vdi.local -ScriptBlock {
$params = @{
Size = 50GB;
StoragePoolFriendlyName = "s2d-pool";
FriendlyName = "mirror-disk";
FileSystem = "CSVFS_ReFS";
PhysicalDiskRedundancy = 2
}
New-Volume @params
# Create VMs on a host and add them to the cluster
1..4 | ForEach-Object {
# create differencing VHD for the VM
$vhdParams = @{
Differencing = $true;
ParentPath = "c:\clusterstorage\volume1\hyper-v\vhd\win2012r2.vhdx";
Path = "c:\clusterstorage\volume1\hyper-v\vhd\hcvm0$_.vhdx";
ComputerName = "n01"
}
New-VHD @vhdParams
# Configure virtual Switches on the Hyper-V Hosts
$vmSwitchParams = @{
CimSession = $cimSessions;
Name = "sw-ext";
NetAdapterName = "Ethernet";
AllowManagementOS = $true
}
New-VMSwitch @vmSwitchParams
# Configure paths for VMs and VHDs
$vmHostParams = @{
function Get-VmNestedHVStatus {
param($VM)
foreach ($item in $VM) {
try {
Get-VM $item -ErrorAction Stop | Select-Object -ExpandProperty extensiondata | Select-Object -ExpandProperty config | Select-Object Name,NestedHVEnabled
} catch {
Write-Warning -Message $_
}
}
}