-
Notifications
You must be signed in to change notification settings - Fork 0
/
eject_drive.ps1
54 lines (53 loc) · 1.64 KB
/
eject_drive.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
<#
.NOTES
Copyright 2012 Robert Nees
Licensed under the Apache License, Version 2.0 (the "License");
http://sushihangover.blogspot.com
.SYNOPSIS
Eject Removable Media
.DESCRIPTION
This scripts provides a way to eject a removable media drive via the cmd line
.EXAMPLE
Do-Eject.ps1 E:
.LINK
http://sushihangover.blogspot.com
#>
Param (
[parameter(
parametersetname="Main")]
[switch]$help,
[parameter(
parametersetname="Main",
mandatory=$false,
position=1)]
[Alias("drive")]
[string]$driveLetter = '',
[parameter(
parametersetname="Main",
mandatory=$false,
position=2)]
[Alias("remote")]
[string]$remoteServer = 'localhost'
)
if ($help.IsPresent) {
help ($MyInvocation.MyCommand.Name) -examples
exit
}
if ($driveLetter -eq '') {
# List removable media and exit
# Get-WMIObject Win32_LogicalDisk -filter "DriveType=2" -computer $
write-host "`nRemovable Media currently present:" -foregroundcolor green
Get-WMIObject Win32_LogicalDisk -filter "DriveType=2" -computer $remoteServer | format-tablE -Property DeviceID, VolumeName -AutoSize -HideTableHeaders
exit
} else {
$cd_drive = $driveLetter
if ($remoteServer = 'localhost') {
$sa = New-Object -comObject Shell.Application
$sa.Namespace(17).ParseName("$cd_drive").InvokeVerb("Eject")
} else {
Invoke-Command -ComputerName $remoteServer -ScriptBlock {
$sa = New-Object -comObject Shell.Application
$sa.Namespace(17).ParseName("$cd_drive").InvokeVerb("Eject")
}
}
}