Skip to content

Instantly share code, notes, and snippets.

@pinecones-sx
Created September 5, 2019 00:12
Show Gist options
  • Save pinecones-sx/657cf2d6f4407a18bb17d568c85b8016 to your computer and use it in GitHub Desktop.
Save pinecones-sx/657cf2d6f4407a18bb17d568c85b8016 to your computer and use it in GitHub Desktop.
Finds mapped drives on local machine or via AD computers
<# Find-MappedDrives
Finds and returns all logically mapped drives from a computer
#>
function Find-MappedDrives {
param(
$Target = 'localhost',
[switch]$All
)
$results = @()
If (-not $All){
$drives = Get-WmiObject Win32_MappedLogicalDisk -computer $Target
ForEach ($drive in $drives){
If ($Target -eq 'localhost'){$Target = $env:COMPUTERNAME}
$results += [pscustomobject]@{
Computer = $Target
DriveLetter = $drive.Name
DrivePath = $drive.ProviderName
Status = 'Online'
}
}
}
Else{
$ADLoaded = Get-Module 'ActiveDirectory'
If (-not $ADLoaded){Import-Module 'ActiveDirectory'}
$domainComputers = Get-ADComputer -Filter * | Select -ExpandProperty Name
$results = (
Test-Connection -ComputerName $domainComputers -Count 1 -AsJob | Wait-Job | Receive-Job |
ForEach{
$thisComputer,$thisOnline,$thisDrives = $null
$thisComputer = $_.Address
$thisOnline = $_.StatusCode -eq 0
If ($thisOnline){
#STUB insert code to verify Enable-PSRemoting has been run, and to enable if not
$thisDrives = Invoke-Command -ComputerName $thisComputer -ScriptBlock {Get-WmiObject Win32_MappedLogicalDisk}
}
If ($thisDrives){
ForEach ($drive in $thisDrives){
[pscustomobject]@{
Computer = $thisComputer
DriveLetter = $drive.Name
DrivePath = $drive.ProviderName
Online = $thisOnline
}
}
}
Else{
[pscustomobject]@{
Computer = $thisComputer
DriveLetter = ''
DrivePath = ''
Online = $thisOnline
}
}
}
)
}
return $results
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment