NTS-ConfigMgrTools.psm1

function New-VMVolume {
    param (
        [Parameter(Mandatory = $false, HelpMessage = "enter char for volume, e.g. V")]
        [char]
        $VMDriveLetter = 'V',

        [Parameter(Mandatory = $false, HelpMessage = "enter name for storagepool , e.g. Pool01")]
        [string]
        $StoragePoolName = "Pool01",

        [Parameter(Mandatory = $false, HelpMessage = "enter name for virtual disk, e.g. VDisk01")]
        [string]
        $VirtualDiskName = "VDisk01",

        [Parameter(Mandatory = $false, HelpMessage = "enter label for the volume, e.g. VMs")]
        [string]
        $VMVolumeName = "VMs"
    )

    try {
        if($null -eq (Get-StoragePool -FriendlyName $StoragePoolName -ErrorAction SilentlyContinue)){
            $PhysicalDisks = Get-PhysicalDisk -CanPool $true | Where-Object -FilterScript { $PSitem.Bustype -ne "USB" }
            $NVMe_Devices = $PhysicalDisks | Where-Object -FilterScript { $PSItem.Bustype -eq "NVMe" -and $PSitem.Size -gt 256GB }
            $Non_NVMe_Devices = $PhysicalDisks | Where-Object -FilterScript { $PSItem.Bustype -ne "NVMe" }
            if ($null -ne $NVMe_Devices) {
                $SelectedDisks = $NVMe_Devices
            }
            else {
                $SelectedDisks = $Non_NVMe_Devices
            }
            $StorageSubSystemFriendlyName = (Get-StorageSubSystem -FriendlyName "*Windows*").FriendlyName
            Write-Output "Create storage pool '$($StoragePoolName)'"
            New-StoragePool -StorageSubSystemFriendlyName $StorageSubSystemFriendlyName -FriendlyName $StoragePoolName -PhysicalDisks $SelectedDisks | Out-Null
            if($null -eq (Get-VirtualDisk -FriendlyName $VirtualDiskName -ErrorAction SilentlyContinue)){
                Write-Output "Create vdisk '$($VirtualDiskName)'"
                New-VirtualDisk -StoragePoolFriendlyName $StoragePoolName -FriendlyName $VirtualDiskName -UseMaximumSize -ProvisioningType Fixed -ResiliencySettingName Simple | Out-Null
                Initialize-Disk -FriendlyName $VirtualDiskName -PartitionStyle GPT | Out-Null
                $VDiskNumber = (Get-Disk -FriendlyName $VirtualDiskName).Number
                Write-Output "Create volume '$($VMVolumeName)'"
                New-Volume -DiskNumber $VDiskNumber -FriendlyName $VMVolumeName -FileSystem ReFS -DriveLetter $VMDriveLetter | Out-Null
            }
            else{
                Write-Output "Virtual disk '$($VirtualDiskName)' already exists - skipping"
            }
        }
        else{
            Write-Output "Pool '$($StoragePoolName)' already exists - skipping"
        }
    }
    catch {
        Write-Output "error during creation of vm volume: $($PSItem.Exception.Message)"
    }
}
Export-ModuleMember -Function New-VMVolume


function New-VMVSwitch {
    param (
        # Course Shortcut
        [Parameter(Mandatory = $false, HelpMessage = "enter name for the virtual switch, e.g. LAN")]
        [string]
        $Course_Shortcut = "LAN"
    )
    
    try {
        if($null -eq (Get-VMSwitch -Name $Course_Shortcut -ErrorAction SilentlyContinue)){
            $pNICs = Get-NetAdapter -Physical | Where-Object -Property Status -eq "UP"
            $10G_NICs = $pNICs | Where-Object -Property LinkSpeed -EQ "10 Gbps"
            $1G_NICs = $pNICs | Where-Object -Property LinkSpeed -EQ "1 Gbps"
    
            if($10G_NICs){
                $Selected_NIC = $10G_NICs[0]
            }
            elseif($1G_NICs){
                $Selected_NIC = $1G_NICs[0]
            }
            else {
                (Get-NetAdapter -Physical | Where-Object -Property Status -eq "UP")[0]
            }
    
            Write-Output "create vswitch '$($Course_Shortcut)'"
            New-VMSwitch -Name $Course_Shortcut -NetAdapterName $Selected_NIC.Name -AllowManagementOS $false | Out-Null
            Add-VMNetworkAdapter -ManagementOS -SwitchName $Course_Shortcut -Name "vNIC-$($Course_Shortcut)"
            Rename-NetAdapter -Name $Selected_NIC.Name -NewName "pNIC-$($Course_Shortcut)"
        }
        else{
            Write-Output "virtual vswitch '$($Course_Shortcut)' already exists - skipping"
        }
    }
    catch {
        Write-Output "error during creation of virtual switch: $($PSItem.Exception.Message)"
    }
}
Export-ModuleMember -Function New-VMVSwitch


function Register-VM_in_CM {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $false, HelpMessage = "pass the vm_config object, should be like `
        `$VM_Config = @{}
        `$VM_01 = @{
            Name = '`$Course_Shortcut)-VWIN11-`$Participant_Number)1'
            RAM = `$RAM
            CPU = `$CPUCount
            CM_Collection_Name = `$CM_Collection_W11_Autopilot
            Credentials = `$VM_Credentials
            DiskSize = `$DynamicDiskSize
            MAC = ""
        }"
)]
        [hashtable]
        $VM_Config_Obj,

        [Parameter(Mandatory = $true, HelpMessage = "specify the fqdn of the sms provider")]
        [string]
        $CM_Siteserver_FQDN,

        [Parameter(Mandatory = $true, HelpMessage = "specify credentials to access the sms provider")]
        [PSCredential]
        $CM_Credentials
    )

    if ($null -eq $VM_Config_Obj) {
        throw "no VM_Config_Obj was supplied, please specify"
    }
    if ($null -eq $CM_Siteserver_FQDN) {
        throw "no CM_Siteserver_FQDN was supplied, please specify"
    }
    if ($null -eq $CM_Credentials) {
        throw "no CM_Credentials was supplied, please specify"
    }

    Write-Output "`nstarting configmgr preparation"
    Write-Output "------"

    try {
        Invoke-Command -ComputerName $CM_Siteserver_FQDN -Credential $CM_Credentials -ScriptBlock {
            $PSDriveName = "CM-PB2"
            $VM_Config_Obj = $using:VM_Config_Obj
            $CM_Collection_All_Systems_Name = "All Systems"

            Import-Module -Name ConfigurationManager
            New-PSDrive -Name $PSDriveName -PSProvider "CMSite" -Root $using:CM_Siteserver_FQDN -Description "Primary site" | Out-Null
            Set-Location -Path "$($PSDriveName):\"

            foreach ($VM in $VM_Config_Obj.Keys | Sort-Object){
                # check destination collection existance
                if ($null -eq (Get-CMCollection -Name $VM_Config_Obj.$VM.CM_Collection_Name)) {
                    Get-CMDevice -Name $VM | Remove-CMDevice -Force -Confirm:$false
                    throw "collection '$($VM_Config_Obj.$VM.CM_Collection_Name)' does not existing, device infos for '$($VM)' was removed"
                }
            }
            
            # create vm info
            foreach ($VM in $VM_Config_Obj.Keys | Sort-Object){
                Write-Output "'$($VM)': creating vm computer info in configmgr - macaddress '$($VM_Config_Obj.$VM.MAC)'"
                Get-CMDevice -Name $VM | Remove-CMDevice -Force -Confirm:$false
            }
            Start-Sleep -Seconds 5
            foreach ($VM in $VM_Config_Obj.Keys | Sort-Object){
                Import-CMComputerInformation -CollectionName $CM_Collection_All_Systems_Name -ComputerName $VM -MacAddress $VM_Config_Obj.$VM.MAC
            }

            # check vm collection membership
            foreach ($VM in $VM_Config_Obj.Keys | Sort-Object){
                #region all systems collection
                $Device_Exists_In_AllDevices = $false
                $All_Devices_Counter = 0
                Write-Output "'$($VM)': checking collection memberships"
                while ($Device_Exists_In_AllDevices -eq $false -and $All_Devices_Counter -lt 30) {
                    $CMDevice = Get-CMDevice -Name $VM
                    if ($CMDevice.Name -eq $VM) {
                        $Device_Exists_In_AllDevices = $true
                    }
                    else{
                        Start-Sleep -Seconds 5
                        $All_Devices_Counter++
                    }
                    Write-Output "'$($VM)': device not found in collection '$($CM_Collection_All_Systems_Name)'"
                    if ($All_Devices_Counter -eq 12) {
                        Write-Output "'$($VM)': triggering membership update in Collection '$($CM_Collection_All_Systems_Name)'"
                        Start-Sleep -Seconds (10 + (Get-Random -Maximum 50 -Minimum 10))
                        Get-CMCollection -Name $CM_Collection_All_Systems_Name | Invoke-CMCollectionUpdate
                    }
                }
                if ($All_Devices_Counter -ge 30) {
                    throw "'$($VM)': could not find in the collection '$($CM_Collection_All_Systems_Name)'"
                }
                else {
                    Write-Output "'$($VM)': '$($CM_Collection_All_Systems_Name)' - found, continuing"
                }
                #endregion
            
                # create vm membership rule
                Add-CMDeviceCollectionDirectMembershipRule -CollectionName $VM_Config_Obj.$VM.CM_Collection_Name -ResourceID $CMDevice.ResourceID

                #region destination collection
                $Device_Exists_In_Specified_Collection = $false
                $Specified_Collection_Counter = 0
                while ($Device_Exists_In_Specified_Collection -eq $false -and $Specified_Collection_Counter -lt 30) {
                    $Collection_Direct_Members = Get-CMDeviceCollectionDirectMembershipRule -CollectionName $VM_Config_Obj.$VM.CM_Collection_Name | Where-Object RuleName -eq $VM
                    if ($null -ne $Collection_Direct_Members) {
                        $Device_Exists_In_Specified_Collection = $true
                    }
                    else{
                        Start-Sleep -Seconds 5
                        $Specified_Collection_Counter++
                    }
                    Write-Output "'$($VM)': device not found in collection '$($VM_Config_Obj.$VM.CM_Collection_Name)'"
                    if ($Specified_Collection_Counter -eq 20) {
                        Write-Output "'$($VM)': triggering membership update in Collection '$($VM_Config_Obj.$VM.CM_Collection_Name)'"
                        Start-Sleep -Seconds (10 + (Get-Random -Maximum 50 -Minimum 10))
                        Get-CMCollection -Name $VM_Config_Obj.$VM.CM_Collection_Name | Invoke-CMCollectionUpdate
                    }
                }
                if ($Specified_Collection_Counter -ge 30) {
                    throw "'$($VM)': could not find in the collection '$($VM_Config_Obj.$VM.CM_Collection_Name)'"
                }
                else {
                    Write-Output "'$($VM)': '$($VM_Config_Obj.$VM.CM_Collection_Name)' - found, continuing"
                }
                #endregion
            }
            Start-Sleep -Seconds 60
            Set-Location -Path $env:SystemDrive
            Remove-PSDrive -Name $PSDriveName
        }
    }
    catch {
        throw "error during registration of device infos in configmgr: $($PSItem.Exception.Message)"
    }
    Write-Output "`nfinished configmgr preparation, now waiting 90 seconds for the configmgr database updates and stabilization"
    Start-Sleep -Seconds 180
}
Export-ModuleMember -Function Register-VM_in_CM


function Start-VM_Deployment {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $false, HelpMessage = "pass the vm_config object, should be like `
        `$VM_Config = @{}
        `$VM_01 = @{
            Name = '`$Course_Shortcut)-VWIN11-`$Participant_Number)1'
            RAM = `$RAM
            CPU = `$CPUCount
            CM_Collection_Name = `$CM_Collection_W11_Autopilot
            Credentials = `$VM_Credentials
            DiskSize = `$DynamicDiskSize
            MAC = ""
        }"
)]
        [hashtable]
        $VM_Config_Obj
    )

    if ($null -eq $VM_Config_Obj) {
        throw "no VM_Config_Obj was supplied, please specify"
    }

    Write-Output "`nstarting vm deployments"
    Write-Output "------"

    try {
        foreach ($VM in $VM_Config_Obj.Keys | Sort-Object) {
            Write-Output "'$($VM)': starting deployment"
            Start-Sleep -Seconds 2
            Start-VM -VMName $VM
        }
    }
    catch {
        throw "error while starting vms: $($PSItem.Exception.Message)"
    }
}
Export-ModuleMember -Function Start-VM_Deployment


function Confirm-VM_Deployment {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $false, HelpMessage = "pass the vm_config object, should be like `
        `$VM_Config = @{}
        `$VM_01 = @{
            Name = '`$Course_Shortcut)-VWIN11-`$Participant_Number)1'
            RAM = `$RAM
            CPU = `$CPUCount
            CM_Collection_Name = `$CM_Collection_W11_Autopilot
            Credentials = `$VM_Credentials
            DiskSize = `$DynamicDiskSize
            MAC = ""
        }"
)]
        [hashtable]
        $VM_Config_Obj,

        [Parameter(Mandatory = $true, HelpMessage = "specify the fqdn of the sms provider")]
        [string]
        $CM_Siteserver_FQDN,

        [Parameter(Mandatory = $true, HelpMessage = "specify credentials to access the sms provider")]
        [PSCredential]
        $CM_Credentials
    )

    if ($null -eq $VM_Config_Obj) {
        throw "no VM_Config_Obj was supplied, please specify"
    }
    if ($null -eq $CM_Siteserver_FQDN) {
        throw "no CM_Siteserver_FQDN was supplied, please specify"
    }
    if ($null -eq $CM_Credentials) {
        throw "no CM_Credentials was supplied, please specify"
    }

    Write-Output "`nchecking vm os deployment status"
    Write-Output "------"

    try {
        Invoke-Command -ComputerName $CM_Siteserver_FQDN -Credential $CM_Credentials -ScriptBlock {
            $PSDriveName = "CM-PB2"

            Import-Module -Name ConfigurationManager
            New-PSDrive -Name $PSDriveName -PSProvider "CMSite" -Root $using:CM_Siteserver_FQDN -Description "Primary site" | Out-Null
            Set-Location -Path "$($PSDriveName):\"
    
            $Deployment_Finished = $false
            $Deployment_Check_Count = 0
            $CM_Deployment_Running = @{}
            foreach ($VM in $using:VM_Config_Obj.keys | Sort-Object) {
                $CM_Deployment_Running.Add($VM,$true)
            }
    
            do {
                $Deployment_Check_Count++
                $CM_All_Deployments = Get-CMDeploymentStatus | Get-CMDeploymentStatusDetails | `
                                        Where-Object -FilterScript { $using:VM_Config_Obj.keys -contains $PSItem.Devicename } | `
                                        Sort-Object -Property DeviceName
                Write-Output "timestamp : $(Get-Date -format "yyyy-MM-dd_HH.mm.ss")"
                if ($null -ne $CM_All_Deployments) {
                    foreach($VM_Deployment in $CM_All_Deployments){
                        if($VM_Deployment.StatusDescription -notlike "*The task sequence manager successfully completed execution of the task sequence*"){
                            Write-Output "'$($VM_Deployment.DeviceName)': still running - $($VM_Deployment.StatusDescription)"
                            $CM_Deployment_Running.$($VM_Deployment.Devicename) = $true
                        }
                        else {
                            Write-Output "'$($VM_Deployment.Devicename)': finished - $($VM_Deployment.StatusDescription)"
                            $CM_Deployment_Running.$($VM_Deployment.Devicename) = $false
                        }
                    }
                }
                else {
                    Write-Output "Waiting on Deployments"
                }                
                Write-Output "------"
    
                if($CM_Deployment_Running.Values -notcontains $true){
                    $Deployment_Finished = $true
                }
                else {
                    Start-Sleep -Seconds 30
                }
    
            } while (($Deployment_Finished -eq $false) -and $Deployment_Check_Count -lt 180 )
    
            if ($Deployment_Check_Count -ge 200) {
                throw "deployment not finished after 100 mins"
            }

            Set-Location -Path $env:SystemDrive
            Remove-PSDrive -Name $PSDriveName
        }
    }
    catch {
        throw "error while checking deployment status: $($PSItem.Exception.Message)"
    }
    Write-Output "`nfinished vm deployments"
    Write-Output "------"
}
Export-ModuleMember -Function Confirm-VM_Deployment


function New-VMs_Objectbased {
    param (
        [Parameter(Mandatory = $false, HelpMessage = "pass the vm_config object, should be like `
        `$VM_Config = @{}
        `$VM_01 = @{
            Name = '`$Course_Shortcut)-VWIN11-`$Participant_Number)1'
            RAM = `$RAM
            CPU = `$CPUCount
            CM_Collection_Name = `$CM_Collection_W11_Autopilot
            Credentials = `$VM_Credentials
            DiskSize = `$DynamicDiskSize
            MAC = ""
        }"
)]
        [hashtable]
        $VM_Config_Obj,

        [Parameter(Mandatory = $false, HelpMessage = "enter name for the virtual switch, e.g. LAN")]
        [string]
        $Course_Shortcut = 'LAN',

        [Parameter(Mandatory = $false, HelpMessage = "enter char for volume, e.g. V")]
        [char]
        $VMDriveLetter = 'V'
    )

    if ($null -eq $VM_Config_Obj) {
        throw "no VM_Config_Obj was supplied, please specify"
    }
    
    try {
        $VM_Base_Path = $VMDriveLetter + ":\VMs"
        Write-Output "`nstarting vm creation"
        Write-Output "------"
        foreach($VM in $VM_Config.Keys | Sort-Object){
            $VMVHDXPath = ($VM_Base_Path + "\" + $VM_Config.$VM.Name + "\" + $VM_Config.$VM.Name + ".vhdx")
            Write-Output "'$($VM_Config.$VM.Name)': creating vm"
            try {
                New-VHD -Path $VMVHDXPath -SizeBytes $VM_Config.$VM.DiskSize -Dynamic | Out-Null
                New-VM -Name $VM_Config.$VM.Name -MemoryStartupBytes $VM_Config.$VM.RAM -Path $VM_Base_Path -Generation 2 -VHDPath $VMVHDXPath -BootDevice NetworkAdapter -SwitchName $Course_Shortcut | Out-Null
            }
            catch {
                throw "error during creation of vhdx or vm '$($VM_Config.$VM.Name)'"
            }
            try {
                Set-VMProcessor -VMName $VM_Config.$VM.Name -Count $VM_Config.$VM.CPU
                Set-VMKeyProtector -VMName $VM_Config.$VM.Name -NewLocalKeyProtector
                Enable-VMTPM -VMName $VM_Config.$VM.Name
                Set-VM -AutomaticStartAction Nothing -VMName $VM_Config.$VM.Name
                Set-VM -AutomaticStopAction ShutDown -VMName $VM_Config.$VM.Name
                Get-VMIntegrationService -VMName $VM_Config.$VM.Name | Where-Object -Property Enabled -EQ $false | Enable-VMIntegrationService
            }
            catch {
                throw "error while setting properties of vm '$($VM_Config.$VM.Name)'"
            }
            Start-VM -Name $VM_Config.$VM.Name
            Start-Sleep -Seconds 2
            Stop-VM -Name $VM_Config.$VM.Name -Force -TurnOff
            Start-Sleep -Seconds 1
        }
        foreach($VM in $VM_Config.Keys | Sort-Object){
            $VM_Config.$VM.MAC = (Get-VM -Name $VM_Config.$VM.Name | Get-VMNetworkAdapter).MacAddress
            Set-VMNetworkAdapter -VMName $VM_Config.$VM.Name -StaticMacAddress $VM_Config.$VM.MAC
        }
    }
    catch {
        throw "error during creation of vms: $($PSItem.Exception.Message)"
    }
}
Export-ModuleMember -Function New-VMs_Objectbased


function Test-VMConnection {
    [cmdletbinding()]
    param (
        # VM object id
        [Parameter(Mandatory = $false, HelpMessage = "specify the vmid")]
        [Guid]
        $VMId,

        # local admin credentials
        [Parameter(HelpMessage = "specify credentials for the vm")]
        [ValidateNotNullOrEmpty()]
        [PSCredential]
        $LocalAdminCreds 
    )

    if ($null -eq $VMId) {
        throw "no VMId was supplied, please specify"
    }

    $VM = Get-VM -Id $VMId
    try {
        Write-Output "------"
        if ($VM.State -eq "Off") {
            Write-Output "------"
            Write-Output "'$($VM.Name)': not running - starting"
            $VM | Start-VM -WarningAction SilentlyContinue
        }

        # Wait for the VM's heartbeat integration component to come up if it is enabled
        $HearbeatIC  = (Get-VMIntegrationService -VM $VM | Where-Object Id -match "84EAAE65-2F2E-45F5-9BB5-0E857DC8EB47")
        if ($HearbeatIC -and ($HearbeatIC.Enabled -eq $true)) {
            $StartTime = Get-Date
            do {
                $WaitForMinitues = 5
                $TimeElapsed = $(Get-Date) - $StartTime
                if ($($TimeElapsed).TotalMinutes -ge 5) {
                    throw "'$($VM.Name)': integration components did not come up after $($WaitForMinitues) minutes"
                } 
                Start-Sleep -sec 1
            } 
            until ($HearbeatIC.PrimaryStatusDescription -eq "OK")
            Write-Output "'$($VM.Name)': heartbeat IC connected"
        }
        do {
            $WaitForMinitues = 5
            $TimeElapsed = $(Get-Date) - $StartTime
            Write-Output "'$($VM.Name)': testing connection"
            if ($($TimeElapsed).TotalMinutes -ge 5) {
                Write-Output "'$($VM.Name)': could not connect to ps direct after $($WaitForMinitues) minutes"
                throw
            } 
            Start-Sleep -sec 3
            $PSReady = Invoke-Command -VMId $VMId -Credential $LocalAdminCreds -ErrorAction SilentlyContinue -ScriptBlock { $True } 
        } 
        until ($PSReady)
    }
    catch {
        Write-Output "'$($VM.Name)': $($PSItem.Exception.Message)"
        break
    }
    return $PSReady
}
Export-ModuleMember -Function Test-VMConnection