Public/Recreate-MobyLinuxVM.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<#
    .SYNOPSIS
        This function recreates the MobyLinuxVM used by Docker-For-Windows to manage Linux Containers.
 
    .DESCRIPTION
        See .SYNOPSIS
 
    .NOTES
 
    .PARAMETER MobyLinuxVMMemoryInGB
        This parameter is OPTIONAL, however, it has a default value of 2.
 
        This parameter takes an integer (even numbers only) that represents the amount of Memory
        in GB to allocate to the newly recreated MobyLinuxVM.
 
    .EXAMPLE
        # Open an elevated PowerShell Session, import the module, and -
 
        PS C:\Users\zeroadmin> Recreate-MobyLinuxVM
#>

function Recreate-MobyLinuxVM {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$False)]
        [ValidateScript({
            $(($_ % 2) -eq 0) -and $($_ -ge 2)
        })]
        [int]$MobyLinuxVMMemoryInGB = 2
    )

    if ([bool]$PSBoundParameters['MobyLinuxVMMemoryInGB']) {
        $MobyLinuxVMMemoryInMB = [Math]::Round($MobyLinuxVMMemoryInGB * 1KB)
    }

    try {
        $DockerDir = $($(Get-Command docker).Source -split "\\Docker\\Resources\\")[0] + "\Docker"
    }
    catch {
        Write-Error $_
        $global:FunctionResult = "1"
        return
    }

    $MobyLinuxISOPath = $(Get-ChildItem -Path "C:\Program Files\Docker" -Recurse -File -Filter "docker-for-win.iso").FullName
    if (!$MobyLinuxISOPath) {
        Write-Error "Unable to find docker-for-win.iso! Halting!"
        $global:FunctionResult = "1"
        return
    }

    if ([bool]$(Get-VM -Name MobyLinuxVM -ErrorAction SilentlyContinue)) {
        MobyLinuxBetter -Destroy
    }

    MobyLinuxBetter -VmName MobyLinuxVM -IsoFile $MobyLinuxISOPath -Create -Memory $MobyLinuxVMMemoryInMB
}