-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserMappedDrivesAndPrinters.ps1
More file actions
231 lines (208 loc) · 9.28 KB
/
UserMappedDrivesAndPrinters.ps1
File metadata and controls
231 lines (208 loc) · 9.28 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# Get the current user's desktop path
$desktopPath = [System.IO.Path]::Combine([System.Environment]::GetFolderPath("Desktop"), "UserMappedDrivesAndPrinters.json")
# Initialize an empty array to hold the results
$results = @()
$mappedPrinterNames = @()
# Define an array of additional port prefixes
# $additionalPorts = @()
# Combine the additional ports with the existing ones
# $allPorts = "^(IP|TCP|WSD-|IPP|HTTP|HTTPS|FTP|SSH|SMTP|DNS|LPR|RAW|SMB)|^\d{1,3}(\.\d{1,3}){3}$" + ($additionalPorts -join "|")
function Get-NetworkInfo {
param (
[string]$ipAddress
)
$url = "https://whois.arin.net/rest/ip/$ipAddress"
try {
$response = Invoke-RestMethod -Uri $url -Method Get -Headers @{Accept = "application/json"}
return $response
} catch {
Write-Output "Error querying ARIN for IP: $ipAddress"
return $null
}
}
function Test-NetworkPort {
param (
[string]$portName
)
if ($portName -match "^(IP|TCP|WSD|IPP|HTTP|HTTPS|FTP|NET|SSH|SMTP|DNS|LPR|RAW|SMB)") {
return $true
} elseif ($portName -match "^\d{1,3}(\.\d{1,3}){3}$") {
$networkInfo = Get-NetworkInfo -ipAddress $portName
if ($networkInfo) {
return $true
}
}
return $false
}
# Get the list of user profiles
$userProfiles = Get-WmiObject -Class Win32_UserProfile | Where-Object { $_.Special -eq $false }
foreach ($profile in $userProfiles) {
try {
$profilePath = $profile.LocalPath
$sid = $profile.SID
# Get the user name, checking for Azure AD users as well
$userName = (Get-WmiObject -Class Win32_UserAccount | Where-Object { $_.SID -eq $sid }).Name
if (-not $userName) {
if (Get-WmiObject -Namespace "Root\Microsoft\Identity\Providers" -Class __Namespace -ErrorAction SilentlyContinue) {
$userName = (Get-WmiObject -Class Win32_UserAccount -Namespace "Root\Microsoft\Identity\Providers" | Where-Object { $_.SID -eq $sid }).Name
} else {
$userName = $profilePath.Split('\')[-1]
}
}
$lastUseTime = $profile.LastUseTime
Write-Host "`nUser: $userName"
# Initialize flags to check if mapped drives and printers are found
$hasMappedDrives = $false
$hasMappedPrinters = $false
$hasLocalPrinters = $false
# Check for mapped drives
$mappedDrivesKey = "Registry::HKEY_USERS\$($profile.SID)\Network"
if (Test-Path $mappedDrivesKey) {
$mappedDrives = Get-ChildItem -Path $mappedDrivesKey
if ($mappedDrives) {
Write-Host " Mapped Drives:"
$hasMappedDrives = $true
foreach ($drive in $mappedDrives) {
$driveLetter = $drive.PSChildName
$remotePath = (Get-ItemProperty -Path "$mappedDrivesKey\$driveLetter").RemotePath
Write-Host " ${driveLetter}"
$results += [pscustomobject]@{
UserName = $userName
ProfilePath = $profilePath
LastUseTime = $lastUseTime
Type = "Mapped Drive"
Identifier = $driveLetter
Details = $remotePath
}
}
}
}
if (-not $hasMappedDrives) {
Write-Host " No Mapped Drives"
$results += [pscustomobject]@{
UserName = $userName
ProfilePath = $profilePath
LastUseTime = $lastUseTime
Type = "Mapped Drive"
Identifier = "None"
Details = "No Mapped Drives"
}
}
# Check for mapped printers using WScript.Network
$network = New-Object -ComObject WScript.Network
$printerConnections = $network.EnumPrinterConnections()
$printerCount = $printerConnections.Count()
if ($printerCount -gt 0) {
Write-Host " Mapped Printers:"
$hasMappedPrinters = $true
for ($i = 0; $i -lt $printerCount; $i += 2) {
$portName = $printerConnections.Item($i)
$printerName = $printerConnections.Item($i + 1)
$printer = (Get-Printer)[$i]
if ($printer) {
$printerType = if (Test-NetworkPort -portName $portName) { "Network Printer" } else { "Local Printer" }
Write-Host " $printerName (Port: $portName)"
$results += [pscustomobject]@{
UserName = $userName
ProfilePath = $profilePath
LastUseTime = $lastUseTime
Type = $printerType
Identifier = $printerName
Details = "Port: $portName"
}
$mappedPrinterNames += $printerName
} else {
Write-Host " $printerName (Port: $portName)"
}
}
}
# Check for mapped printers in the registry
$printersKey = "Registry::HKEY_USERS\$($profile.SID)\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts"
if (Test-Path $printersKey) {
$mappedPrinters = Get-ItemProperty -Path $printersKey
if ($mappedPrinters) {
Write-Host " Mapped Printers (Registry):"
foreach ($printer in $mappedPrinters.PSObject.Properties) {
if ($printer.Name -notin @('PSPath', 'PSParentPath', 'PSChildName', 'PSProvider') -and $mappedPrinterNames -notcontains $printer.Name) {
$printerObject = Get-Printer | Where-Object { $_.Name -eq $printer.Name }
if ($printerObject) {
$portName = $printerObject.PortName
$printerType = if (Test-NetworkPort -portName $portName) { "Network Printer" } else { "Local Printer" }
Write-Host " $($printer.Name) (Port: $portName)"
$results += [pscustomobject]@{
UserName = $userName
ProfilePath = $profilePath
LastUseTime = $lastUseTime
Type = $printerType
Identifier = $printer.Name
Details = "Port: $portName"
}
$mappedPrinterNames += $printer.Name
} else {
Write-Host "Printer $($printer.Name) not found using Get-Printer."
}
}
}
}
}
if (-not $hasMappedPrinters) {
Write-Host " No Mapped Printers"
$results += [pscustomobject]@{
UserName = $userName
ProfilePath = $profilePath
LastUseTime = $lastUseTime
Type = "Mapped Printer"
Identifier = "None"
Details = "No Mapped Printers"
}
}
# Check for local printers and their ports
$printers = Get-WmiObject -Query "SELECT * FROM Win32_Printer"
if ($printers) {
Write-Host " Local Printers:"
$hasLocalPrinters = $true
foreach ($printer in $printers) {
if ($mappedPrinterNames -notcontains $printer.Name) {
$portName = $printer.PortName # Retrieve the port name from the printer object
$printerType = if (Test-NetworkPort -portName $portName) { "Network Printer" } else { "Local Printer" }
Write-Host " $($printer.Name) (Port: $portName)"
$results += [pscustomobject]@{
UserName = $userName
ProfilePath = $profilePath
LastUseTime = $lastUseTime
Type = $printerType
Identifier = $printer.Name
Details = "Port: $portName"
}
}
}
}
if (-not $hasLocalPrinters) {
Write-Host " No Local Printers"
$results += [pscustomobject]@{
UserName = $userName
ProfilePath = $profilePath
LastUseTime = $lastUseTime
Type = "Printer"
Identifier = "None"
Details = "No Local Printers"
}
}
} catch {
Write-Host "Error processing user: $userName"
Write-Host $_.Exception.Message
$results += [pscustomobject]@{
UserName = $userName
ProfilePath = $profilePath
LastUseTime = $lastUseTime
Type = "Error"
Identifier = "N/A"
Details = $_.Exception.Message
}
}
}
# Export results to CSV
$backslash = [char]0x005C
$results | ConvertTo-Json | ForEach-Object { $_ -replace '\\\\', $backslash } | Out-File -FilePath $desktopPath -Encoding utf8 -Force
# $results | Export-CSV -Path $desktopPath -NoTypeInformation -Force
Write-Host "Results have been exported to $desktopPath"