Advertisement
f3lckern

Scriptd

Feb 21st, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #region functions
  2. function Invoke-VMCommand {
  3.     param (
  4.  
  5.         [Parameter(Mandatory = $true)][string]$computername,
  6.         [Parameter(Mandatory = $true)][guid]$VMId,
  7.         [pscredential]$ComputerCred,
  8.         [Parameter(Mandatory = $true)][pscredential]$VMCred,
  9.         [Parameter(Mandatory = $true)][scriptblock]$ScriptBlock
  10.  
  11.     )
  12.     if ($ComputerCred) {
  13.         $PSDefaultParameterValues = @{
  14.             "Invoke-Command:Credential" = $ComputerCred
  15.         }
  16.     }
  17.     Invoke-Command -ComputerName $computername -ScriptBlock {
  18.         $exeSB = [scriptblock]::Create("$Using:ScriptBlock")
  19.         Invoke-Command -VMId $Using:VMId -Credential $Using:VMCred -ScriptBlock $exeSB
  20.     }
  21. }
  22. #endregion  
  23.  
  24. $vmmserver = "CENSORED"
  25. $ErrorActionPreference = "Stop"
  26. $initm = 1
  27.  
  28. if ($initm -eq 1) {
  29.     #VM Variabels
  30.     $housevm = Get-SCVirtualMachine | Where-Object -Property VMId -EQ "B9AC40E5-B6DD-49DF-9CFC-2C4A5EFA5DEC"
  31.     $VMId = $housevm.VMId
  32.     $VMStatus = $housevm.Status
  33.     $VMHost = $housevm.HostName
  34.     $VMLocation = $housevm.Location
  35.     $vmdir = $VMLocation -replace "C:", "\\$VMHost\c$"
  36.     $VHDPRESENT = $housevm | Get-SCVirtualDiskDrive
  37.  
  38.  
  39.     #VHDX Variabels
  40.     $updvhdxchild = "Win2016-Gen2.vhdx"
  41.     $updvhdxmother = "mothervhd\DK01UPDTEMP01_Win2016-Gen2.vhdx.mother"
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.     #Trys to stop the VM
  50.     try {
  51.         if ($VMStatus -eq 'Running') {
  52.             Write-Host -ForegroundColor Yellow "[WARNING]: The VM is on... Trying to stop it..."
  53.             Stop-SCVirtualMachine -VM $housevm
  54.         }
  55.     }
  56.     catch {
  57.         Write-Host -ForegroundColor Red "[ERROR]: Cloud not stop VM.  Check VMM for solution"
  58.     }
  59.     #END OF TRY
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.     #IF VHD IS PRESENT IN VM, IT WILL BE REMOVED
  68.     if ($null -ne $VHDPRESENT) {
  69.         try {
  70.             Write-Host -ForegroundColor Yellow "[WARNING]: Existing HDD DISK Drive, detected - Trying auto-remove before proceeding...`n"
  71.             $VHDPRESENT | Remove-SCVirtualDiskDrive
  72.         }
  73.         catch {
  74.             Throw "Could not remove.  Check VMM for solution"
  75.         }
  76.         Write-Host -ForegroundColor Green "[STATUS]: Successfully removed VirtualDiskDrive...`n"
  77.     }
  78.     #END OF TRY
  79.  
  80.  
  81.  
  82.  
  83.    
  84.  
  85.     #Copies the VHD, from "MOTHER"
  86.     try {
  87.         Write-Host -ForegroundColor Green "[STATUS]: Copying MOTHERVHDX into CHILDVHDX...`n"
  88.         Copy-Item -Path "$vmdir\$updvhdxmother" -Destination "$vmdir\$updvhdxchild"
  89.     }
  90.     catch {
  91.         Throw "Could not copy MOTHER into CHILD. Is the path '$vmdir' correct?"
  92.     }
  93.     #END OF TRY
  94.  
  95.  
  96.  
  97.  
  98.     #Attaches child disk to VM
  99.     try {
  100.         Write-Host -ForegroundColor Green "[STATUS]: Trying to attach VHDX($upvhdxchild)...`n"
  101.         New-SCVirtualDiskDrive -VMMServer $vmmserver -VM $housevm -SCSI -Bus 0 -LUN 0 -UseLocalVirtualHardDisk -FileName "$updvhdxchild" -Path "$VMLocation" -VolumeType BootAndSystem
  102.     }
  103.     catch {
  104.         Throw "Could not connect disk to VM. Check VMM for a solution OR is the path $vmdir\$upvhdxchild correct?"
  105.     }
  106.     #END OF TRY
  107.  
  108.  
  109.  
  110.  
  111.     #START VM
  112.     try {
  113.         Write-Host -ForegroundColor Grenn "[STATUS]: Starting the Virtual Machine..."
  114.         Start-SCVirtualMachine -VM $housevm
  115.     }
  116.     catch {
  117.         $poke = Get-SCVirtualMachine | Where-Object -Property VMId -EQ "B9AC40E5-B6DD-49DF-9CFC-2C4A5EFA5DEC"
  118.         $throwstatus = $poke.status
  119.         Throw "Couldnt start the Virtual Machine - status is $throwstatus"
  120.     }
  121.  
  122.  
  123.  
  124.     #Credentials for VM
  125.     $Username = 'Administrator'
  126.     $Password = 'DDDDDDDD'
  127.     $pass = ConvertTo-SecureString -AsPlainText $Password -Force
  128.     $Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $pass
  129.  
  130.     do {
  131.         do {
  132.             try {
  133.                 Write-Host -ForegroundColor Green "[STATUS]: Checking if any updates are availabe..."
  134.                 $Updates = Invoke-VMCommand -computername $VMHost -VMId $VMId -VMCred $Cred -ScriptBlock {
  135.                     Get-WUList
  136.                 }
  137.                 $failedconn = $null
  138.             }
  139.             catch {
  140.                 Write-Host -ForegroundColor Red "[ERROR]: Couldnt connect to the VM... Trying again in a bit"
  141.                 $failedconn = 1
  142.                 Start-Sleep -Seconds 5 #Less than 10 seconds is ambigous
  143.             }
  144.         }until($null -eq $failedconn)
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.         if ($null -ne $Updates) {
  152.             Invoke-VMCommand -computername $VMHost -VMId $VMId -VMCred $Cred -ScriptBlock {
  153.                 Write-Host -ForegroundColor Yellow "[WARNING]: UPDATES AVAILABLE GOING TO INSTALL..."
  154.                 Start-Sleep -Seconds 2
  155.                 Write-Host -ForegroundColor Green "[STATUS]: Downloads, and installing updates. LOL"
  156.                 Install-WindowsUpdate -Download -Install -AcceptAll -AutoReboot
  157.             }
  158.         }
  159.     }until($null -eq $Updates)
  160.  
  161.     Write-Host -BackgroundColor DarkGreen -ForegroundColor Yellow "LOOKS LIKE UPDATE IS COMPLETE"
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement