forked from dsccommunity/HyperVDsc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSample_xVhdFileExamples.ps1
More file actions
171 lines (137 loc) · 4.99 KB
/
Sample_xVhdFileExamples.ps1
File metadata and controls
171 lines (137 loc) · 4.99 KB
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
# sample values used for testing.
$sampleVhdPath = "C:\test_vhds\RenameComputer.Vhd"
# sample text file that you want to copy in the VHD.
$sampletxt = "C:\sample.txt"
# This path is the relative path to mounted drive letter.
$sampleVhdDestinationPath = "xvhdFileExample\CopiedFile"
# A local folder that you want to copy in to the VHD.
$samplefolder = "c:\SampleFolder"
Configuration xVhdD_CopyFileOrFolder
{
Param(
[Parameter(Mandatory=$true, Position=0)]
[validatescript({Test-Path $_})]
$vhdPath,
[Parameter(Mandatory=$true)]
[validatescript({Test-Path $_})]
$itemToCopy,
[Parameter(Mandatory=$true)]
$relativeDestinationPath
)
Import-DscResource -moduleName xHyper-V
xVhdFile FileCopy
{
VhdPath = $vhdPath
FileDirectory = MSFT_xFileDirectory {
SourcePath = $itemToCopy
DestinationPath = $relativeDestinationPath
}
}
}
# Copy File/Folder example
xVhdD_CopyFileOrFolder -vhdPath $sampleVhdPath -itemToCopy $sampletxt -relativeDestinationPath $sampleVhdDestinationPath
Start-DscConfiguration -ComputerName localhost -Path $pwd\xVhdD_CopyFileOrFolder\ -Wait -Verbose
xVhdD_CopyFileOrFolder -vhdPath $sampleVhdPath -itemToCopy $samplefolder -relativeDestinationPath $sampleVhdDestinationPath
Start-DscConfiguration -ComputerName localhost -Path $pwd\xVhdD_CopyFileOrFolder\ -Wait -Verbose
Configuration RemoveFileOrFolderFromVHD
{
param(
[Parameter(Mandatory=$true, Position=0)]
[validatescript({Test-Path $_})]
$vhdPath,
[Parameter(Mandatory=$true)]
$relativeDestinationPath,
$Ensure = 'Absent'
)
Import-DscResource -moduleName xHyper-V
xVhdFile RemoveFile
{
VhdPath = $vhdPath
FileDirectory = MSFT_xFileDirectory {
DestinationPath = $relativeDestinationPath
Ensure = $Ensure
}
}
}
RemoveFileOrFolderFromVHD -vhdPath $sampleVhdPath -relativeDestinationPath $sampleVhdDestinationPath
Start-DscConfiguration -ComputerName localhost -Path $pwd\RemoveFileOrFolderFromVHD\ -Wait -Verbose
Configuration ChangeAttribute
{
param(
[Parameter(Mandatory=$true, Position=0)]
[validatescript({Test-Path $_})]
$vhdPath,
[Parameter(Mandatory=$true)]
$relativeDestinationPath,
[ValidateSet ("Archive", "Hidden", "ReadOnly", "System" )] $attribute
)
Import-DscResource -moduleName xHyper-V
xVhdFile Change-Attribute
{
VhdPath = $vhdPath
FileDirectory = MSFT_xFileDirectory {
DestinationPath = $relativeDestinationPath
Attributes = $attribute
}
}
}
ChangeAttribute -vhdPath $sampleVhdPath -relativeDestinationPath $sampleVhdDestinationPath -attribute 'ReadOnly'
Start-DscConfiguration -ComputerName localhost -Path $pwd\RemoveFileOrFolderFromVHD\ -Wait -Verbose
# End to end sample for x-Hyper-v
Configuration Sample_EndToEndXHyperV_RunningVM
{
param
(
[Parameter(Mandatory)]
$vhdPath,
[Parameter(Mandatory)]
$name,
[Parameter(Mandatory)]
[validatescript({Test-Path $_})]
$unattendedFilePathToCopy
)
Import-DscResource -module xHyper-V
# Create a switch to be used by the VM
xVMSwitch switch
{
Name = "Test-Switch"
Ensure = "Present"
Type = "Internal"
}
# Create new VHD file.
xVHD NewVHD1
{
Ensure = "Present"
Name = $name
Path = (Split-Path $vhdPath)
Generation = "vhd"
ParentPath = $vhdPath
}
# Customize VHD by copying a folders/files to the VHD before a VM can be created
# Example below shows copying unattended.xml before a VM can be created
xVhdFile CopyUnattendxml
{
VhdPath = $vhdPath
FileDirectory = MSFT_xFileDirectory {
SourcePath = $unattendedFilePathToCopy
DestinationPath = "unattended.xml"
}
}
# create the testVM out of the vhd.
xVMHyperV testvm
{
Name = "$($name)_vm"
SwitchName = "Test-Switch"
VhdPath = Join-path (Split-Path $vhdPath) "$name.vhd"
ProcessorCount = 2
MaximumMemory = 1GB
MinimumMemory = 512MB
RestartIfNeeded = "TRUE"
DependsOn = "[xVHD]NewVHD1","[xVMSwitch]switch","[xVhdFile]CopyUnattendxml"
State = "Running"
}
}
# Create a mof file.
Sample_EndToEndXHyperV_RunningVM -vhdPath $sampleVhdPath -name TestMachine -unattendedFilePathToCopy C:\temp\unattended.xml
# Run the configuration on localhost.
Start-DscConfiguration -Path $pwd\Sample_EndToEndXHyperV_RunningVM -ComputerName localhost -Verbose -Wait