forked from gangstanthony/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-ComputerShares.ps1
74 lines (61 loc) · 2.22 KB
/
Get-ComputerShares.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
# http://stackoverflow.com/questions/15797198/powershell-get-a-list-of-network-machines
# switch -regex (NET.EXE VIEW) { "^\\\\(?<Name>\S+)\s+" {$matches.Name}}
# $shares = $(switch -regex (net view) { '^\\\\(?<Name>\S+)' {$matches.Name}}) | % {net view $_}
# do NOT need admin rights
# not reliable. tried once on ca-nor1-sb10, nothing, tried again, got results... :/
# check all computers that show up on the network
#''; net view | ? {$_ -match '^\\\\'} | % {$_.trim()} | % {if (ping1 $_.substring(2)) {net view $_}} | ? {$_ -and $_ -notmatch 'no entries|not found'} | % {if ($_ -match 'succ') {"`n"} else {$_}}
# need admin rights for this option
#gwmi win32_share -comp 'lt-40132' | select pscomputername, type, name, caption, description | ft -a
function Get-ComputerShares {
param (
[string]$comp = $env:COMPUTERNAME
)
if (!$comp) { throw 'No comps.' }
$ping = New-Object System.Net.NetworkInformation.Ping
try {
$result = $ping.Send($comp)
} catch {
$result = $null
}
$sharename = $type = $comment = $ip = '-'
if ($result.Status -eq 'Success') {
# get the ip address
$ip = $result.Address.ToString()
# THE MAIN COMMAND
$netview = iex "cmd /c net view $comp 2>&1" | ? {$_}
# if there are less than 5 lines, no shares found
if ($netview.count -lt 5) {
[pscustomobject]@{
Computer = $comp
IP = $ip
ShareName = $sharename
Type = $type
Comment = $comment
}
return
}
$netview = $netview | ? {$_ -match '\s{2}'} | select -Skip 1
foreach ($line in $netview) {
$line = $line -split '\s{2,}'
$sharename = $line[0]
$type = $line[1]
$comment = $line[2]
[pscustomobject]@{
Computer = $comp
IP = $ip
ShareName = $sharename
Type = $type
Comment = $comment
}
}
} else {
[pscustomobject]@{
Computer = $comp
IP = $ip
ShareName = $sharename
Type = $type
Comment = $comment
}
}
}