-
Notifications
You must be signed in to change notification settings - Fork 2
/
defaults.ps1
executable file
·77 lines (61 loc) · 2.32 KB
/
defaults.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Default location for virtual machines
# Change the values below for your environment. You have to create the directories.
# Name and location of readonly folder to mount in VM
$SHARED_RO_NAME = "ro"
$SHARED_RO_PATH = "D:\Shared\ro"
# Name and location of writable folder to mount in VM
$SHARED_RW_NAME = "rw"
$SHARED_RW_PATH = "D:\Shared\rw"
# Folder to store VMs in after they are created
$VM_DIR = "D:\Virtual Machines"
# --- No need to edit below --- #
# Defaults for shared cache for Packer.
$env:PACKER_CACHE_DIR = "../packer_cache"
# Functions
function Create-VirtualMachine($BASE, $CONF_NAME, $VM_DIR_NAME){
if (Test-Path $VM_DIR/$VM_DIR_NAME){
Write-Host "Directory for VM exists. Remove it and rerun the script. Exiting." -ForegroundColor Red
Exit
}
Set-Location $CONF_NAME
packer build -force -var-file ../variables-$BASE.pkr.hcl ./$CONF_NAME.pkr.hcl
Start-Sleep -s 2
if (-not (Test-Path $VM_DIR/$VM_DIR_NAME)){
Move-Item ./$VM_DIR_NAME $VM_DIR
Start-Sleep -s 2
} else {
Write-Host "Directory for VM has been created already during packer run. Exiting." -ForegroundColor Red
Exit
}
if (Test-Path ./shared.ps1) {
./shared.ps1
}
Set-Location ..
vmware.exe -q -t $VM_DIR/$VM_DIR_NAME/$VM_DIR_NAME.vmx
Start-Sleep -s 2
}
function Enable-SharedFolders($VM_DIR_NAME){
vmrun.exe enableSharedFolders $VM_DIR/$VM_DIR_NAME/$VM_DIR_NAME.vmx
Start-Sleep -s 2
}
function Add-SharedFolder($VM_DIR_NAME, $SHARE_NAME, $SHARE_PATH, $SHARED_STATE){
vmrun.exe addSharedFolder $VM_DIR/$VM_DIR_NAME/$VM_DIR_NAME.vmx $SHARE_NAME $SHARE_PATH
Start-Sleep -s 2
vmrun.exe setSharedFolderState $VM_DIR/$VM_DIR_NAME/$VM_DIR_NAME.vmx $SHARE_NAME $SHARE_PATH $SHARED_STATE
Start-Sleep -s 2
}
function Start-VM($VM_DIR_NAME){
vmrun.exe start $VM_DIR/$VM_DIR_NAME/$VM_DIR_NAME.vmx nogui
Start-Sleep -s 2
# Wait until running
vmrun.exe getGuestIPAddress $VM_DIR/$VM_DIR_NAME/$VM_DIR_NAME.vmx
Start-Sleep -s 2
}
function Stop-VM($VM_DIR_NAME){
vmrun.exe stop $VM_DIR/$VM_DIR_NAME/$VM_DIR_NAME.vmx
Start-Sleep -s 2
vmrun.exe deleteSnapshot $VM_DIR/$VM_DIR_NAME/$VM_DIR_NAME.vmx Installed
Start-Sleep -s 2
vmrun.exe snapshot $VM_DIR/$VM_DIR_NAME/$VM_DIR_NAME.vmx Secure
Start-Sleep -s 2
}