psievm.psm1

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
<#
Copyright 2015 Ryan Conrad

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-----
this is loosely based on https://github.com/xdissent/ievms/blob/master/ievms.sh

#>



$script:PSIEVM = "psievm";

#region Exported Functions

function Get-IEVM {
    <#

    .SYNOPSIS

    Gets an IE VM from modern.ie and imports it to the supported VM Host.


    .DESCRIPTION



    .PARAMETER OS

    The OS version. Supported values:

    - XP, WinXP, Windows XP
    - Vista, WinVista, Windows Vista
    - 7, Win7, Windows 7
    - 8, Win8, Windows 8
    - 8.1, Win8.1, Windows 8.1
    - 10, Win10, Windows 10

    .PARAMETER IEVersion

    Based on the OS the following version are supported:

    - XP: 6, 7
    - Vista: 8
    - Win7: 8, 9, 10, 11
    - Win8: 10
    - Win8.1: 11
    - Win10: Edge

    .PARAMETER Shares

    A list of shares to mount in the VM (when supported by the host)

    .PARAMETER AlternateVMLocation

    Use an alternate location to find the VM image zips. The zips MUST follow the following name format:

    IE: IE<Version>.<OS>.For.Windows.<VMHost>.zip
    Edge: Microsoft%20Edge.<OS>.For.Windows.<VMHost>.zip

    .PARAMETER VMHost

    Defines the VM host to use. Supported VM hosts: VirtualBox

    .PARAMETER IgnoreInvalidMD5

    If exists, it will ignore the zip file MD5 hash check.

    .PARAMETER VMRootPath

    The path to store the VM files.

    .EXAMPLE

    Get-IEVM -OS XP -IEVersion 6 -VMHost VirtualBox

    Get the IE6 on Windows XP VM for VirtualBox

    .EXAMPLE

    Get-IEVM -OS Win7 -IEVersion 9 -VMHost VirtualBox -AlternateVMLocation "\\vmhost-machine\VirtualBox\"

    Get the IE9 on Windows 7 VM for VirtualBox from your company network share

    .NOTES

    Fork on Github: https://github.com/camalot/psievm

    #>

    [CmdletBinding()]
    Param (
        [ValidateSet("XP", "Vista", "7", "8", "8.1", "10", "WinXP","WinVista", "Win7", "Win8", "Win8.1",
            "Win10", "WindowsXP","WindowsVista", "Windows7", "Windows8", "Windows8.1", "Windows10",
            "Windows XP","Windows Vista", "Windows 7", "Windows 8", "Windows 8.1", "Windows 10")]
        [Parameter(Mandatory=$true, Position=0)]
        [string] $OS,
        [Parameter(Mandatory=$false, Position=2)]
        [string[]] $Shares,
        [Parameter(Mandatory=$false,Position=3)]
        [string] $AlternateVMLocation = "",
        [Parameter(Mandatory=$false, Position=4)]
        [ValidateSet("VirtualBox" <#, "VMWare", "VPC", "HyperV", "Vagrant"#>)]
        [string] $VMHost = "VirtualBox",
        [Parameter(Mandatory=$false, Position=5)]
        [switch] $IgnoreInvalidMD5,
        [Parameter(Mandatory=$false, Position=6)]
        [string] $VMRootPath = $pwd
    );

    DynamicParam {
        # Set the dynamic parameters' name
        $ievParam = "IEVersion";
        $ievAlias = "IE";

        # Create the dictionary
        $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary;
        # Create the collection of attributes
        $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute];
        # Create and set the parameters' attributes
        $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute;
        $ParameterAttribute.Mandatory = $true;
        $ParameterAttribute.Position = 1;

        # Add the attributes to the attributes collection
        $AttributeCollection.Add($ParameterAttribute);
        $arrSet = @();
        # We need to set the version that each OS supports, plus, we need to make sure the OS is in the correct format.
        switch -Regex ($OS) {
            "^(win(dows)?)?\s?xp$" {
                $OS = "XP";
                $arrSet = @("6", "8");
            }
            "^(win(dows)?)?\s?vista$" {
                $OS = "Vista";
                $arrSet = @("7");
            }
            "^(win(dows)?)?\s?7$" {
                $OS = "7";
                $arrSet = @("8","9","10","11");
            }
            "^(win(dows)?)?\s?8$" {
                $OS = "8";
                $arrSet = @("10");
            }
            "^(win(dows)?)?\s?8.1$" {
                $OS = "8.1";
                $arrSet = @("11");
            }
            "^(win(dows)?)?\s?10$" {
                $OS = "10";
                $arrSet = @("Edge");
            }
        }
        $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet);
        # Add the ValidateSet to the attributes collection
        $AttributeCollection.Add($ValidateSetAttribute);
        # Create the alias attribute
        $AliasAttribute = New-Object System.Management.Automation.AliasAttribute($ievAlias);
        $AttributeCollection.Add($AliasAttribute);
        # Create and return the dynamic parameter
        $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ievParam, [string], $AttributeCollection);
        $RuntimeParameterDictionary.Add($ievParam, $RuntimeParameter);
        return $RuntimeParameterDictionary;
    }

    begin {
        $VMUser = "IEUser";
        $VMPassword = "Passw0rd!";
        $IEVersion = $PsBoundParameters[$ievParam];
        Write-Host "Initializing for VMHost '$VMHost'" -BackgroundColor Gray -ForegroundColor Black;
        $vmIE = @{$true="11";$false="$IEVersion"}[$IEVersion -ieq "edge"];
        $vmName = ("IE{0} - Win{1}" -f $vmIE, $OS);
        $vmPath = (Join-Path -Path $VMRootPath -ChildPath $vmName);

        switch ($VMHost) {
            "VirtualBox" {
                $vmext = "ova";
            }
            # There is no default, because if it isn't valid, it wont get this far.
        }

        switch($OS) {
            "10" {
                $baseFile = "Microsoft%20{0}.{2}{1}.For.Windows.{3}.zip";
            }
            default {
                $baseFile = "IE{0}.{2}{1}.For.Windows.{3}.zip";
            }
        }
        $baseFile = ($baseFile -f $IEVersion, $OS,  @{$true="";$false="Win"}[$OS -imatch "^(xp|vista)$"], $VMHost);
        # No alternate VM Location is specified, download from ms
        if($AlternateVMLocation -eq "" -or $AlternateVMLocation -eq $null) {
            $buildNumber = "20141027";
            $baseURL = "https://az412801.vo.msecnd.net/vhd/VMBuild_{0}/{2}/IE{1}/Windows/{3}";
            switch -Regex ($IEVersion) {
                "^edge$" {
                    $buildNumber = "20150801";
                    $baseURL = "https://az792536.vo.msecnd.net/vms/VMBuild_{0}/{2}/MS{1}/Windows/{3}";
                }
            }
            $url = $baseURL -f $buildNumber, $IEVersion, $VMHost, $baseFile;
        } else {
            # use alternate path for the images (like a share)
            # combine the paths, validate and add missing slashes if needed
            $baseUrl = "{0}$AlternateVMLocation{2}{1}" -f (@{$true="";$false="\\"}[$AlternateVMLocation -imatch "^(\\\\|https?:\/\/|[a-z]:\\)"]), $baseFile,
                @{$true="";$false="/"}[$AlternateVMLocation -match "(\\|\/)$"];
            $url = $baseURL;
        }

        $baseVMName = ("IE$vmIE - Win$OS");
        $vmImportFile = (Join-Path -Path $vmPath -ChildPath "${baseVMName}.${vmext}" );
        $zip = (Join-Path -Path $vmPath -ChildPath "${vmName}.zip");
    }

    process {
        #Write-Host "$PSIEVM v$PSIEVMVersion" -ForegroundColor Yellow;

        # if the VM does not exist
        if( !(Test-VMHost -VMHost $VMHost -VMName $vmName) ) {

            # if the vmPath doesnt exist, create it.
            if(!(Test-Path -Path $vmPath)) {
                Write-Host ("Creating path `"$vmPath`"") -BackgroundColor Gray -ForegroundColor Black;
                New-Item -Path $vmPath -ItemType Directory | Out-Null;
            }

            if(!(Test-Path -Path $zip) -and !(Test-Path -Path $vmImportFile)) {
                Write-Host ("Transfer from: `"$url`" -> `"$zip`"") -BackgroundColor Gray -ForegroundColor Black;
                Start-BitsTransfer -Source $url -Destination $zip;
            }

            if((Test-Path -Path $zip) -and !(Test-Path -Path $vmImportFile)) {
                Write-Host ("Validating MD5 File Hash `"$zip`"") -BackgroundColor Gray -ForegroundColor Black;
                $shouldIgnoreMD5Validation = $PSBoundParameters.ContainsKey('IgnoreInvalidMD5');
                if(!($shouldIgnoreMD5Validation)) {
                    if(!(Test-MD5Hash -Path $zip -VMName $vmName -VMHost $VMHost)) {
                        throw "MD5 hash validation of zip '$zip' failed.";
                    }
                }
                Write-Host ("Extracting `"$zip`" -> `"$vmPath`"") -BackgroundColor Gray -ForegroundColor Black;
                Expand-7ZipArchive -Path $zip -DestinationPath (Split-Path $zip);
                Write-Host ("Deleting `"$zip`"") -BackgroundColor Gray -ForegroundColor Black;
                Remove-Item -Path $zip -Force | Out-Null;
            }

            if( !(Test-Path -Path $vmImportFile) ) {
                Write-Host "VM import file '$vmImportFile' not found." -BackgroundColor Red -ForegroundColor White;
                throw "VM import file '$vmImportFile' not found.";
            }
            # Add the VMRootPath to the shares.
            #$Shares.Add($VMRootPath) | Out-Null;
            $importSuccess = Import-VMImage -VMHost $VMHost -VMName $vmName -ImportFile $vmImportFile -IEVersion $IEVersion -OS $OS -VMRootPath $VMRootPath -Shares $Shares;
            if(!$importSuccess) {
                Write-Host "VM import failed." -BackgroundColor Red -ForegroundColor White;
                throw "VM import failed.";
            }
        }

        $startResult = Start-VMHost -VMHost $VMHost -VMName $vmName -VMRootPath $VMRootPath;

        if(-not $startResult) {
            throw "Error starting VM '$vmName' on host '$VMHost' at '$VMRootPath'";
        }
    }
}

Set-Alias -Name psievm -Value Get-IEVM;

function Update-PSIEVM {
    <#
    .SYNOPSIS
    Updates the installed version to the latest version that is available on github.
    .EXAMPLE
    Update-PSIEVM
    #>

    iex ((new-object net.webclient).DownloadString("https://raw.githubusercontent.com/camalot/psievm/master/psievm/psievm.package/tools/chocolateyInstall.ps1"));
}
#endregion


function Import-VMImage {
    Param (
        [Parameter(Mandatory=$true)]
        [string] $IEVersion,
        [Parameter(Mandatory=$true)]
        [string] $OS,
        [Parameter(Mandatory=$true)]
        [string] $VMHost,
        [Parameter(Mandatory=$true)]
        [string] $VMName,
        [Parameter(Mandatory=$true)]
        [string] $ImportFile,
        [Parameter(Mandatory=$true)]
        [string] $VMRootPath,
        [Parameter(Mandatory=$false)]
        [string[]] $Shares
    );

    switch($VMHost) {
        "VirtualBox" {
            return Import-VBoxImage -IEVersion $IEVersion -OS $OS -VMName $VMName -ImportFile $ImportFile -VMRootPath (Join-Path -Path $VMRootPath -ChildPath $VMName) -Shares $Shares;
        }
        default {
            return $false;
        }
    }
}

function Test-VMHost {
    Param (
        [Parameter(Mandatory=$true, Position=0)]
        [string] $VMHost,
        [Parameter(Mandatory=$true, Position=1)]
        [string] $VMName
    );
    switch($VMHost) {
        "VirtualBox" {
            return Test-VBoxVM -VMName $VMName;
        }
        default {
            return $false;
        }
    }
}

function Start-VMHost {
    Param (
        [Parameter(Mandatory=$true)]
        [string] $VMHost,
        [Parameter(Mandatory=$true)]
        [string] $VMName,
        [Parameter(Mandatory=$false)]
        [string] $VMRootPath
    );
    switch($VMHost) {
        "VirtualBox" {
            return Start-VBoxVM -VMName $VMName -VMRootPath $VMRootPath;
        }
        default {
            return $false;
        }
    };
}

#region VirtualBox
function Import-VBoxImage {
    Param (
        [Parameter(Mandatory=$true)]
        [string] $IEVersion,
        [Parameter(Mandatory=$true)]
        [string] $OS,
        [Parameter(Mandatory=$true)]
        [string] $VMName,
        [Parameter(Mandatory=$true)]
        [string] $ImportFile,
        # This should be the directory that contains the ova, the disk, everything.
        [Parameter(Mandatory=$true)]
        [string] $VMRootPath,
        [Parameter(Mandatory=$false)]
        [string[]] $Shares = @()
    );
    try {
        $vbunit = "11";
        switch -Regex ($IEVersion) {
            "^edge$" {
                $vbunit = "8";
            }
            "^(6|7|8)$" {
                $vbunit = "10";
            }
        };

        $vbm = Get-VBoxManageExe;
        $vbox = (Join-Path -Path $VMRootPath -ChildPath "${$VMName}.vbox");
        $disk = (Join-Path -Path $VMRootPath -ChildPath ("$VMName-disk1.vmdk"));
        Write-Host ("Importing $ImportFile to VM `"$VMName`"") -BackgroundColor Gray -ForegroundColor Black;

        #(& $vbm import `"$ImportFile`" --vsys 0 --vmname `"$VMName`" --unit $vbunit --disk `"$disk`" 2>&1 | Out-String) | Out-Null;
        Invoke-ShellCommand -Command $vbm -CommandArgs @("import",  "`"$ImportFile`"", "--vsys", "0", "--vmname", "`"$VMName`"", "--unit",  "$vbunit", "--disk", "`"$disk`"") | Out-Null;
        $Shares | where { $_ -ne "" -and $_ -ne $null; } | foreach {
            $shareName = (Split-Path -Path $_ -Leaf);
            Write-Host ("Adding share `"$shareName`" on VM `"$VMName`"") -BackgroundColor Gray -ForegroundColor Black;
            Invoke-ShellCommand -Command $vbm -CommandArgs @( "sharefolder", "add", "`"$VMName`"", "--name", "`"$shareName`"", "--automount", "--hostpath", "`"$_`"") | Out-Null;
            #(& $vbm sharedfolder add `"$VMName`" --name `"$shareName`" --automount --hostpath `"$_`" 2>&1 | Out-String) | Out-Null;
        };

        #$dt = (Get-Date -Format 'MM-dd-yyyy hh:mm');
        #(& $vbm setextradata `"$VMName`" `"psievm`" `"{\`"created\`" : \`"$dt\`", \`"version\`" : \`"$PSIEVMVERSION`"}\`" 2>&1 | Out-String) | Out-Null;
        #Invoke-ShellCommand -Command $vbm -CommandArgs @("setextradata", "`"$VMName`"", "`"psievm`"", "`"{\`"created\`" : \`"$dt\`", \`"version\`" : \`"$PSIEVMVERSION`"}\`"") | Out-String | Out-Null;
        
        Write-Host ("Taking initial snapshot of `"$VMName`"") -BackgroundColor Gray -ForegroundColor Black;

        #(& $vbm snapshot `"$VMName`" take clean --description `"The initial VM state.`" 2>&1 | Out-String) | Out-Null;
        Invoke-ShellCommand -Command $vbm -CommandArgs @("snapshot", "`"$VMName`"", "take", "clean", "--description", "`"The initial VM state.`"") | Out-Null;
        return $true;
    } catch [System.Exception] {
        return $false;
    }
}

function Start-VBoxVM {
    Param (
        [Parameter(Mandatory=$true)]
        [string] $VMName,
        [Parameter(Mandatory=$false)]
        [string] $VMRootPath
    );
    try {
        $vbm = Get-VBoxManageExe;
        Write-Host "Starting VM `"$VMName`"" -BackgroundColor Gray -ForegroundColor Black;
        #(& $vbm startvm `"$VMName`" *>&1) | Write-Host;
        Invoke-ShellCommand -Command "$vbm" -CommandArgs "startvm", "`"$VMName`"" | Out-Null;
        return $true;
    } catch [Exception] {
        return $false;
    }
}

function Test-VBoxVM {
    Param (
        [Parameter(Mandatory=$true, Position=0)]
        [string] $VMName
    );
    try {
        $vbm = Get-VBoxManageExe;
        Write-Host "Testing if VM `"$VMName`" already exists in VirtualBox" -BackgroundColor Gray -ForegroundColor Black;
        #$r = & "$vbm" showvminfo `"$VMName`" 2>&1 | Out-String;
        $r = Invoke-ShellCommand -Command $vbm -CommandArgs "showvminfo", "`"$VMName`"";
        $vmnEscaped = [Regex]::Escape($VMName);
        if($r -imatch "Could\snot\sfind\sa\sregistered\smachine\snamed\s'$vmnEscaped'") {
            Write-Host ("VM Image not found in VirtualBox.") -BackgroundColor Gray -ForegroundColor Black;
            # vm does not exist.
            return $false;
        }
        Write-Host ("VM Image found in VirtualBox.") -BackgroundColor Gray -ForegroundColor Black;
        return $true;
    } catch {
        return $false;
    }
}

#function Invoke-RemoteVBoxCommand {
# Param (
# [Parameter(Mandatory=$true, Position=0)]
# [string] $VMName,
# [Parameter(Mandatory=$true, Position=1)]
# [string] $Command,
# [Parameter(Mandatory=$false, Position=2)]
# [string] $Arguments
# );
# $vbm = Get-VBoxManageExe;
# Write-Host "Executing `"$Command $Arguments`" on `"$VMName`"" -BackgroundColor Gray -ForegroundColor Black;
# #(& $vbm guestcontrol `"$VMName`" run --username `"$VMUser`" --password `"$VMPassword`" --exe `"$Command`" -- `"$Arguments`" *>&1) | Out-String | Write-Host;
# if(Wait-VBoxGuestControl) {
# Invoke-ShellCommand -Command $vbm -CommandArgs @("guestcontrol", "`"$VMName`"", "run", "--username", "`"$VMUser`"", "--password", "`"$VMPassword`"", "--exe", "`"$Command`"", "--", "`"$Arguments`"");
# } else {
# "Unable to execute remote command.`nUnable to get guestcontrol" | Write-Host;
# }
#}


function Get-VBoxManageExe {
    $vbm = @("${env:ProgramFiles(x86)}\Oracle\VirtualBox\VBoxManage.exe","$($env:ProgramFiles)\Oracle\VirtualBox\VBoxManage.exe") | where { Test-Path -Path $_ } | select -First 1;
    if($vbm -eq $null) {
        Write-Host "Unable to locate VirtualBox tools. Installing via Chocolatey.";
        Install-ChocolateyApp -Names virtualbox, vboxguestadditions.install;

        return "${env:ProgramFiles(x86)}\Oracle\VirtualBox\VBoxManage.exe";
    }
    return $vbm;
}

#function Wait-VBoxGuestControl {
# try {
# $vbm = Get-VBoxManageExe;
# Write-Host "Waiting for Guest Additions Control Process (Max 3 minutes wait)." -BackgroundColor Gray -ForegroundColor Black -NoNewline;
# $timeout = new-timespan -Minutes 3
# $sw = [diagnostics.stopwatch]::StartNew()
# while ($sw.elapsed -lt $timeout){
# $r = Invoke-ShellCommand -Command $vbm -CommandArgs @("showvminfo", "`"$VMName`"");
# if($r -match "Additions\srun\slevel\:\s+3") {
# # vm does not exist.
# Write-Host "Guest Additions Ready." -BackgroundColor Gray -ForegroundColor Black;
# return $true;
# }
# Write-Host "." -NoNewline -BackgroundColor Gray -ForegroundColor Black;
# Start-Sleep -Seconds 1;
# }
# Write-Host "Guest Additions wait timed out." -BackgroundColor Gray -ForegroundColor Black;
# return $false;
# } catch {
# return $false;
# }
#}
#endregion

#region Chocolatey
function Get-ChocolateyExe {
    $cho = "$env:ProgramData\chocolatey\choco.exe";
    if(!(Test-Path -Path $cho) ) {
        Write-Host -BackgroundColor Red -ForegroundColor White "Unable to locate chocolatey. Installing chocolatey...";
        Invoke-InstallChocolatey;
        if(!(Test-Path -Path $cho) ) {
            throw [System.IO.FileNotFoundException] "Still unable to locate chocolatey, even after install attempt."
        }
    }
    return $cho;
}

function Invoke-InstallChocolatey {
    Invoke-Expression -Command ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1')) | Write-Host;
}

function Install-ChocolateyApp {
    Param (
        [Parameter(Mandatory=$true, Position=0)]
        [string[]] $Names
    );

    $choc = Get-ChocolateyExe;
    $list = ($Names -join " ");
    $args = "install -y $list" -split " ";
    Invoke-ShellCommand -Command $choc -CommandArgs $args;
}

#endregion

function Get-FileMD5Hash {
    Param (
        [Parameter(Mandatory=$true, Position=0)]
        [string] $Path
    )
    $md5 = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider;
    $stream = [System.IO.File]::Open("$Path",[System.IO.Filemode]::Open, [System.IO.FileAccess]::Read);
    $hash = [System.BitConverter]::ToString($md5.ComputeHash($stream)) -replace "-", "";
    $stream.Close();
    return $hash;
}

function Test-MD5Hash {
    Param (
        [Parameter(Mandatory=$true, Position=0)]
        [string] $VMName,
        [Parameter(Mandatory=$true, Position=1)]
        [string] $VMHost,
        [Parameter(Mandatory=$true, Position=2)]
        [string] $Path
    )
    begin {
        $hashes = @{
            "VirtualBox" = @{
                "IE6 - WinXP" = "1FE27A06C0A8E0CB3EE6D27DFE3C634A";
                "IE7 - WinXP" = "1FE27A06C0A8E0CB3EE6D27DFE3C634A";
                "IE8 - WinXP" = "EFBF507C4A3CE533C7AE539E59FB8A17";
                "IE7 - WinVista" = "C144A18EA40848F2611036448D598002";
                "IE8 - WinVista" = "C144A18EA40848F2611036448D598002";
                "IE9 - WinVista" = "C144A18EA40848F2611036448D598002";
                "IE8 - Win7" = "86D481F517CA18D50F298FC9FB1C5A18";
                "IE9 - Win7" = "61A2B69A5712ABD6566FCBD1F44F7A2B";
                "IE10 - Win7" = "755F05AF1507CD8940354BF564A08D50";
                "IE11 - Win7" = "7AA66EC15A51EE8B0A4AB39353472F07";
                "IE10 - Win8" = "CAF9FCEF0A4EE13A236BDC7BDB9FF1D3";
                "IE11 - Win8.1" = "080C652C69359B6742DE547BA594AB2A";
                "IE11 - Win10" = "8A441819E97F8766E25FAA810BD1FF4F";
            }
        };
    }
    process {
        if((Get-Command -Name "Get-FileHash") -eq $null) {
            $hash = (Get-FileMD5Hash -Path $Path);
        } else {
            $hash = (Get-FileHash -Path $Path -Algorithm MD5 -Verbose).Hash;
        }
        if(!($hashes.ContainsKey($VMHost)) -or !($hashes[$VMHost].ContainsKey($VMName))) {
            Write-Warning "No Hash Available for $($VMHost):$($VMName)";
            return $true;
        }
        $chash = $hashes[$VMHost][$VMName];
        Write-Host "MD5 Compare: '$hash' -> '$chash'" -BackgroundColor Gray -ForegroundColor Black;
        return $hash -ieq $chash;
    }
}

function Invoke-DownloadFile {
    Param (
        [string] $Url,
        [string] $File
    );
    $downloader = (new-object System.Net.WebClient);
    $downloader.Proxy.Credentials=[System.Net.CredentialCache]::DefaultNetworkCredentials;
    $downloader.DownloadFile($Url, $File);
}

function Expand-7ZipArchive {
    Param (
        [Parameter(Mandatory=$true, Position=0)]
        [string] $Path,
        [Parameter(Mandatory=$true, Position=1)]
        [string] $DestinationPath
    );
    begin {
        $7zaUrl = "https://raw.githubusercontent.com/camalot/psievm/master/psievm/.tools/7za.exe";
        $scriptRootPath = Get-ScriptRoot;
        $toolsDir = (Join-Path -Path $scriptRootPath -ChildPath "tools");
        if(!(Test-Path -Path $toolsDir)) {
            New-Item -Path $toolsDir -ItemType Directory | Out-Null;
        }
        $7zaExe = (Join-Path -Path $toolsDir -ChildPath "7za.exe");
    }
    process {
        if(!(Test-Path -Path $7zaExe)) {
            # download 7zip
            Write-Host "Download 7Zip commandline tool";
            Invoke-DownloadFile -Url $7zaUrl -File "$7zaExe";
        }
        Start-Process "$7zaExe" -ArgumentList "x -o`"$DestinationPath`" -y `"$Path`"" -Wait -NoNewWindow | Write-Host;
    }
}

function Invoke-ShellCommand {
    param (
        [Parameter(Mandatory=$true)]
        [string] $Command,
        [Parameter(Mandatory=$false)]
        [string[]] $CommandArgs
    );
    $args = $CommandArgs -join " ";
    Write-Host "$Command $args";
    return (& "$Command" $CommandArgs *>&1);
}

function Get-ScriptRoot {
    if(-not $PSScriptRoot) {
        if(-not $PSCommandPath) {
            return (Split-Path -Path $MyInvocation.MyCommand.Path -Parent);
        } else {
            return (Split-Path -Path $PSCommandPath -Parent);
        }
    } else {
        return (Resolve-Path -Path $PSScriptRoot);
    }
}