# ============================================================================================== # # Microsoft PowerShell Source File -- Created with SAPIEN Technologies PrimalScript 2009 # # NAME: Get-Netconnections.ps1 # # AUTHOR: Jeffery Hicks, Windows PowerShell MVP , SAPIEN Technologies, Inc. # DATE : 3/19/2009 # # COMMENT: Use the Win32_NetworkConnection class to retrieve information about mapped network drives # Sample WMI Output # Status : OK # ConnectionState : Connected # Persistent : True # LocalName : W: # RemoteName : \\jdhit-dc01\public # __GENUS : 2 # __CLASS : Win32_NetworkConnection # __SUPERCLASS : CIM_LogicalElement # __DYNASTY : CIM_ManagedSystemElement # __RELPATH : Win32_NetworkConnection.Name="\\\\jdhit-dc01\\public (W:)" # __PROPERTY_COUNT : 17 # __DERIVATION : {CIM_LogicalElement, CIM_ManagedSystemElement} # __SERVER : XP01 # __NAMESPACE : root\cimv2 # __PATH : \\XP01\root\cimv2:Win32_NetworkConnection.Name="\\\\jdhit-dc01\\public (W:)" # AccessMask : # Caption : RESOURCE REMEMBERED # Comment : # ConnectionType : Persistent Connection # Description : RESOURCE REMEMBERED - Microsoft Windows Network # DisplayType : Share # InstallDate : # Name : \\jdhit-dc01\public (W:) # ProviderName : Microsoft Windows Network # RemotePath : \\jdhit-dc01\public # ResourceType : Disk # UserName : MYCOMPANY\administrator # Not all properties may be populated for all shares on all platforms. #EXAMPLES #PS C:\> Get-NetConnections #PS C:\> Get-NetConnections -computer "File01" -progress #if you have the Quest cmdlets installed #PS C:\> $data=get-qadcomputer | foreach {get-netconnections -computer $_.name -progress} #PS C:\> $data | sort RemotePath | format-table -groupby RemotePath Computer,LocalName,Status # DISCLAIMER AND WARNING: # THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY # KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. # TEST THOROUGHLY IN A NON-PRODUCTION ENVIRONMENT. IF YOU DON'T KNOW WHAT THIS # SCRIPT WILL DO...DO NOT RUN IT! # # ============================================================================================== Function Get-NetConnections { Param([string]$computername=$env:computername, [switch]$progress) #turn off the error pipeline $ErrorActionPreference="SilentlyContinue" #trap errors, display a message and continue Trap { Write-Warning "Error connecting to $computername" Continue } #make $computername upper case for looks $computername=$computername.ToUpper() if ($progress) { #if -progress is specified use Write-Progress to display what #is happening Write-Progress -Activity "Getting Network Connections" -status $computername ` -currentoperation "Executing Get-WMIObject Win32_NetworkConnection -computername $computername" } #get WMI information. Turn off error notification $connections=Get-WmiObject win32_networkconnection -computername $computername -ea "Stop" #validate connections were returned if (!$connections) { Write-Host "No network connections found on $computername" -foregroundcolor RED -backgroundcolor Yellow Return $null } if ($progress) { Write-Progress -Activity "Getting Network Connections" -status $computername ` -currentoperation "Parsing results" } $connections | select @{Name="Computer";Expression={$_.__SERVER}},` @{Name="LocalName";Expression = {$_.LocalName.toUpper()}},` @{Name="RemoteComputer";Expression={ #use regular expression to match the first part of the UNC $_.RemotePath -match "[^\\]+" | Out-Null ($matches.Item(0)).ToUpper() }},RemotePath,Status,ConnectionState }