Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region functions
- function Invoke-VMCommand {
- param (
- [Parameter(Mandatory = $true)][string]$computername,
- [Parameter(Mandatory = $true)][guid]$VMId,
- [pscredential]$ComputerCred,
- [Parameter(Mandatory = $true)][pscredential]$VMCred,
- [Parameter(Mandatory = $true)][scriptblock]$ScriptBlock
- )
- if ($ComputerCred) {
- $PSDefaultParameterValues = @{
- "Invoke-Command:Credential" = $ComputerCred
- }
- }
- Invoke-Command -ComputerName $computername -ScriptBlock {
- $exeSB = [scriptblock]::Create("$Using:ScriptBlock")
- Invoke-Command -VMId $Using:VMId -Credential $Using:VMCred -ScriptBlock $exeSB
- }
- }
- #endregion
- $vmmserver = "CENSORED"
- $ErrorActionPreference = "Stop"
- $initm = 1
- if ($initm -eq 1) {
- #VM Variabels
- $housevm = Get-SCVirtualMachine | Where-Object -Property VMId -EQ "B9AC40E5-B6DD-49DF-9CFC-2C4A5EFA5DEC"
- $VMId = $housevm.VMId
- $VMStatus = $housevm.Status
- $VMHost = $housevm.HostName
- $VMLocation = $housevm.Location
- $vmdir = $VMLocation -replace "C:", "\\$VMHost\c$"
- $VHDPRESENT = $housevm | Get-SCVirtualDiskDrive
- #VHDX Variabels
- $updvhdxchild = "Win2016-Gen2.vhdx"
- $updvhdxmother = "mothervhd\DK01UPDTEMP01_Win2016-Gen2.vhdx.mother"
- #Trys to stop the VM
- try {
- if ($VMStatus -eq 'Running') {
- Write-Host -ForegroundColor Yellow "[WARNING]: The VM is on... Trying to stop it..."
- Stop-SCVirtualMachine -VM $housevm
- }
- }
- catch {
- Write-Host -ForegroundColor Red "[ERROR]: Cloud not stop VM. Check VMM for solution"
- }
- #END OF TRY
- #IF VHD IS PRESENT IN VM, IT WILL BE REMOVED
- if ($null -ne $VHDPRESENT) {
- try {
- Write-Host -ForegroundColor Yellow "[WARNING]: Existing HDD DISK Drive, detected - Trying auto-remove before proceeding...`n"
- $VHDPRESENT | Remove-SCVirtualDiskDrive
- }
- catch {
- Throw "Could not remove. Check VMM for solution"
- }
- Write-Host -ForegroundColor Green "[STATUS]: Successfully removed VirtualDiskDrive...`n"
- }
- #END OF TRY
- #Copies the VHD, from "MOTHER"
- try {
- Write-Host -ForegroundColor Green "[STATUS]: Copying MOTHERVHDX into CHILDVHDX...`n"
- Copy-Item -Path "$vmdir\$updvhdxmother" -Destination "$vmdir\$updvhdxchild"
- }
- catch {
- Throw "Could not copy MOTHER into CHILD. Is the path '$vmdir' correct?"
- }
- #END OF TRY
- #Attaches child disk to VM
- try {
- Write-Host -ForegroundColor Green "[STATUS]: Trying to attach VHDX($upvhdxchild)...`n"
- New-SCVirtualDiskDrive -VMMServer $vmmserver -VM $housevm -SCSI -Bus 0 -LUN 0 -UseLocalVirtualHardDisk -FileName "$updvhdxchild" -Path "$VMLocation" -VolumeType BootAndSystem
- }
- catch {
- Throw "Could not connect disk to VM. Check VMM for a solution OR is the path $vmdir\$upvhdxchild correct?"
- }
- #END OF TRY
- #START VM
- try {
- Write-Host -ForegroundColor Grenn "[STATUS]: Starting the Virtual Machine..."
- Start-SCVirtualMachine -VM $housevm
- }
- catch {
- $poke = Get-SCVirtualMachine | Where-Object -Property VMId -EQ "B9AC40E5-B6DD-49DF-9CFC-2C4A5EFA5DEC"
- $throwstatus = $poke.status
- Throw "Couldnt start the Virtual Machine - status is $throwstatus"
- }
- #Credentials for VM
- $Username = 'Administrator'
- $Password = 'DDDDDDDD'
- $pass = ConvertTo-SecureString -AsPlainText $Password -Force
- $Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $pass
- do {
- do {
- try {
- Write-Host -ForegroundColor Green "[STATUS]: Checking if any updates are availabe..."
- $Updates = Invoke-VMCommand -computername $VMHost -VMId $VMId -VMCred $Cred -ScriptBlock {
- Get-WUList
- }
- $failedconn = $null
- }
- catch {
- Write-Host -ForegroundColor Red "[ERROR]: Couldnt connect to the VM... Trying again in a bit"
- $failedconn = 1
- Start-Sleep -Seconds 5 #Less than 10 seconds is ambigous
- }
- }until($null -eq $failedconn)
- if ($null -ne $Updates) {
- Invoke-VMCommand -computername $VMHost -VMId $VMId -VMCred $Cred -ScriptBlock {
- Write-Host -ForegroundColor Yellow "[WARNING]: UPDATES AVAILABLE GOING TO INSTALL..."
- Start-Sleep -Seconds 2
- Write-Host -ForegroundColor Green "[STATUS]: Downloads, and installing updates. LOL"
- Install-WindowsUpdate -Download -Install -AcceptAll -AutoReboot
- }
- }
- }until($null -eq $Updates)
- Write-Host -BackgroundColor DarkGreen -ForegroundColor Yellow "LOOKS LIKE UPDATE IS COMPLETE"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement