-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTest-NetworkDrive.ps1
57 lines (48 loc) · 3.11 KB
/
Test-NetworkDrive.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
#requires -Version 2
Function Test-NetworkDrive {
<#
.SYNOPSIS
Mounts network drive and attempts to write a test file.
.DESCRIPTION
Mounts a drive to the specified path(s) and writes a text file with the current date and time as the title
.PARAMETER Share
The path(s) to be mounted
.EXAMPLE
Test-NetworkDrive -Share '\\Server\Share\Monitor'
.EXAMPLE
'\\Server\Share\Monitor','\\Server\Share2\Monitor' | Test-NetworkDrive
.EXAMPLE
Import-CSV Shares.csv | Test-NetworkDrive
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true,
Position = 0,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[string[]]
$Share
)
begin {
$Date = (Get-Date -Format MM_dd_yyyy_HH_mm)
}
process
{
foreach ($Object in $Share) {
try {
$Path = New-PSDrive -Name 'Monitor' -PSProvider FileSystem -Root "$Object" -ErrorAction Stop
New-Item -ItemType file -Path $Path.Root -Name "$Date.txt" -ErrorAction Stop
}
catch {
Write-Error -Message $_.Exception.Message
}
finally {
if($Path.Name) {
Remove-PSDrive -Name $Path.Name -Force
}
}
}
}
end {}
}