-
Notifications
You must be signed in to change notification settings - Fork 64
/
BurnCDs.ps1
179 lines (146 loc) · 5.62 KB
/
BurnCDs.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
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
<#
.SYNOPSIS Burns multiple CD copies from a specified folder path
.DESCRIPTION
.EXAMPLE .\BurnCDs.ps1 -Path 'C:\Folder' -CdTitle 'Test Title' -Copies 67
.PARAMETER Path
The folder path containing the files you'd like to burn to the CD
.PARAMETER CdTitle The title of the CD that you'd like to show up in Windows Explorer
.PARAMETER Copies The number of copies of the CD you'd like
#>
[CmdletBinding()]
param (
[Parameter(Mandatory=$True,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True,
HelpMessage='What is the folder path you would like to burn?')]
[string]$Path,
[Parameter(Mandatory=$False,
ValueFromPipeline=$False,
ValueFromPipelineByPropertyName=$True,
HelpMessage='What is the name of the CD?')]
[string]$CdTitle,
[Parameter(Mandatory=$False,
ValueFromPipeline=$False,
ValueFromPipelineByPropertyName=$False,
HelpMessage='How many copies would you like?')]
[int]$Copies = 1
)
begin {
Function Eject-CDTray {
<# .SYNOPSIS Ejects the local machine's CD tray .DESCRIPTION This function looks for all available CD drives and ejects them .EXAMPLE Eject-CDTray #>
[CmdletBinding()]
param (
)
begin {
$sh = New-Object -ComObject "Shell.Application"
}
process {
$sh.Namespace(17).Items() | Where { $_.Type -eq "CD Drive" } | foreach { $_.InvokeVerb("Eject") }
}
end {
}
}
Function Close-CDTray {
<# .SYNOPSIS Closes the local machine's CD tray if it's ejected .DESCRIPTION .EXAMPLE Close-CDTray #>
[CmdletBinding()]
param (
)
begin {
$DiskMaster = New-Object -com IMAPI2.MsftDiscMaster2
$DiscRecorder = New-Object -com IMAPI2.MsftDiscRecorder2
$id = $DiskMaster.Item(0)
}
process {
$DiscRecorder.InitializeDiscRecorder($id)
$DiscRecorder.CloseTray()
}
end {
}
}
Function Out-CD {
<# .SYNOPSIS Burns the contents of a folder to a CD .DESCRIPTION This function retrieves the contents of a specified folder path and burns a CD with the specified title .EXAMPLE Out-CD -Path 'C:\Folder' -CdTitle 'This is my title' .PARAMETER Path The folder path containing the files you'd like to burn to the CD .PARAMETER CdTitle The title of the CD that you'd like to show up in Windows Explorer #>
[CmdletBinding()]
param (
[Parameter(Mandatory=$True,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True,
HelpMessage='What is the folder path you would like to burn?')]
[string]$Path,
[Parameter(Mandatory=$False,
ValueFromPipeline=$False,
ValueFromPipelineByPropertyName=$True,
HelpMessage='What is the name of the CD?')]
[string]$CdTitle
)
begin {
try
{
Write-Verbose 'Creating COM Objects.'
$DiskMaster = New-Object -com IMAPI2.MsftDiscMaster2
$DiscRecorder = New-Object -com IMAPI2.MsftDiscRecorder2
$FileSystemImage = New-Object -com IMAPI2FS.MsftFileSystemImage
$DiscFormatData = New-Object -com IMAPI2.MsftDiscFormat2Data
}
catch
{
$err = $Error[0]
Write-Error $err
return
}
}
process {
Write-Verbose 'Initializing Disc Recorder.'
$id = $DiskMaster.Item(0)
$DiscRecorder.InitializeDiscRecorder($id)
Write-Verbose 'Assigning recorder.'
$dir = $FileSystemImage.Root
$DiscFormatData.Recorder = $DiscRecorder
$DiscFormatData.ClientName = 'PowerShell Burner'
Write-Verbose 'Multisession?'
if (-not $DiscFormatData.MediaHeuristicallyBlank)
{
try
{
$FileSystemImage.MultisessionInterfaces = $DiscFormatData.MultisessionInterfaces
Write-Verbose 'Importing existing session.'
$FileSystemImage.ImportFileSystem() | Out-Null
}
catch
{
$err = $Error[0]
Write-Error $err
return
}
}
else
{
Write-Verbose 'Empty medium.'
$FileSystemImage.ChooseImageDefaults($DiscRecorder)
$FileSystemImage.VolumeName = $CdTitle
}
Write-Verbose "Adding directory tree ($Path)."
$dir.AddTree($Path, $false)
Write-Verbose 'Creating image.'
$result = $FileSystemImage.CreateResultImage()
$stream = $result.ImageStream
Write-Verbose 'Burning.'
$DiscFormatData.Write($stream)
Write-Verbose 'Done.'
}
end {
}}
Eject-CDTray
}
process {
for ($i = 0; $i -lt $Copies; $i++) {
Sleep -Seconds 7
Close-CDTray
Sleep -Seconds 8
Out-Cd -Path $Path -CdTitle $CdTitle -Verbose
Eject-CDTray
}
}
end {
Sleep -Seconds 7
Close-CDTray
}