Jump to content

Recommended Posts

Posted

Hi there I've made script to show the mapped drives of a remote pc. However i'd like to be able to export that data ideally to csv with pc name, username and date and time stamp as well as the mapped drives.

Any help would be great. Thanks as always

 

 

$ComputerName = "name"

 

gwmi win32_mappedlogicaldisk -ComputerName $ComputerName | select SystemName,Name,ProviderName,SessionID | foreach {

$disk = $_

$user = gwmi Win32_LoggedOnUser -ComputerName $ComputerName | where { ($_.Dependent.split("=")[-1] -replace '"') -eq $disk.SessionID} | foreach {$_.Antecedent.split("=")[-1] -replace '"'}

$disk | select Name,ProviderName,@{n="MappedTo";e={$user} }

Posted (edited)

So I thought I would look into this a little more as I always think that there maybe a more PS centric way of doing this type of thing WMI/CIM is always a fall back position. So here is what I found:-

 

https://docs.microsoft.com/en-us/windows/desktop/cimwin32prov/win32-mappedlogicaldisk

 

You can read to the bottom of that page or just go to here:-

 

https://devblogs.microsoft.com/scripting/how-can-i-determine-which-drives-are-mapped-to-network-shares/

 

But I would look at the Get-PSDrive -PSProvider FileSystem way not totally sure about this mind would have to run some tests...

Edited by HPlum78
Posted

I don't think what you're looking to do is going to be possible.

 

Unless I've been doing it wrong for a long time, Powershell won't be able to get details like that from another user's session. Whenever you query another computer with Powershell (Invoke-Command, Get-WMIObject, Get-CimInstance etc.), you're doing it under your login, in your user context. You're not logged in to the client and have no drives mapped, so it returns nothing.

 

I've tried running your commands in a script I have that will check each online computer in my AD, and the only one that returns results is the computer I'm actively logged in to:

 

...snip...
ZZ-PC-xx2
ZZ-PC-xy2
ZZ-PC-xxx-001
SystemName    MappedTo Name ProviderName                      
----------    -------- ---- ------------                      
ZZ-PC-xxx-001 dayzd    A:   \\host-01\dayzd$
ZZ-PC-xxx-001 dayzd    B:   \\host-01\Depts
ZZ-PC-xxx-001 dayzd    X:   \\host\www
ZZ-PC-xxx-001 dayzd    Y:   \\hostTheThird\Backups\D\__prev
ZZ-PC-xxx-001 dayzd    Z:   \\host4\c$

ZZ-PC-xx-002
ZZ-PC-xyx-001
...snip...

Posted (edited)
I don't think what you're looking to do is going to be possible.

 

Unless I've been doing it wrong for a long time, Powershell won't be able to get details like that from another user's session. Whenever you query another computer with Powershell (Invoke-Command, Get-WMIObject, Get-CimInstance etc.), you're doing it under your login, in your user context. You're not logged in to the client and have no drives mapped, so it returns nothing.

 

I've tried running your commands in a script I have that will check each online computer in my AD, and the only one that returns results is the computer I'm actively logged in to:

 

...snip...
ZZ-PC-xx2
ZZ-PC-xy2
ZZ-PC-xxx-001
SystemName    MappedTo Name ProviderName                      
----------    -------- ---- ------------                      
ZZ-PC-xxx-001 dayzd    A:   \\host-01\dayzd$
ZZ-PC-xxx-001 dayzd    B:   \\host-01\Depts
ZZ-PC-xxx-001 dayzd    X:   \\host\www
ZZ-PC-xxx-001 dayzd    Y:   \\hostTheThird\Backups\D\__prev
ZZ-PC-xxx-001 dayzd    Z:   \\host4\c$

ZZ-PC-xx-002
ZZ-PC-xyx-001
...snip...

 

 

Hi there the script that I posted works fine if the user is logged into the remote pc. I'm basically using the script along with something like impero. So if a user tells me that don't have the correct mapped drives. I look and find the machine name from impero and then I use my script and it tells me which drives are mapped.

 

Thanks

Edited by tri_94
Posted
I believe that a user needs to be logged in. But that script for me gives me the drives for whichever user is logged in to the remote pc. I've tested it with a dozen different pc's and users
Posted
I believe that a user needs to be logged in. But that script for me gives me the drives for whichever user is logged in to the remote pc. I've tested it with a dozen different pc's and users

 

I'm glad it does - I wish it would for me, too!

Posted

The script you posted doesn't work for me. I need to figure out where the line breaks are etc to successfully run it.

Try posting it in code block.

 

To answer the question though, you should be able to pipe the results into export-csv.

Posted
I need to figure out where the line breaks are etc to successfully run it.

It's missing a trailing '}'

 

$ComputerName = "name"
Get-WmiObject Win32_MappedLogicalDisk -ComputerName $ComputerName | Select SystemName,Name,ProviderName,SessionID | ForEach { 
   $disk = $_
   $user = Get-WMIObject Win32_LoggedOnUser -ComputerName $ComputerName | Where {($_.Dependent.split("=")[-1] -replace '"') -eq $disk.SessionID} | ForEach {$_.Antecedent.split("=")[-1] -replace '"'}
   $disk | Select SystemName,@{n="MappedTo";e={$user}},Name,ProviderName
}

Posted

I thought I tried adding one, but I guess not.

Your script is very inconsistent. The only client I was able to get it working on was mine. Every other client returned no drives. However a few servers did return results remotely.

 

It will put the results in a csv file and open it after running now. The csv is placed in the directory the script is run from. This can be changed with the path variable.

 

 

 

[color=#001080][font=Consolas]$ComputerName[/font][/color][color=#000000][font=Consolas] = [/font][/color][color=#A31515][font=Consolas]"name"
[/font][/color][color=#000000][font=Consolas][color=#001080]$path[/color] = [color=#a31515]"[/color][color=#001080]$PSScriptRoot[/color][color=#a31515]\drives.csv"[/color]
[color=#001080]$drives[/color] = [color=#795e26]Get-WmiObject[/color] Win32_MappedLogicalDisk -ComputerName [color=#001080]$ComputerName[/color] | [color=#795e26]Select-Object[/color] SystemName,Name,ProviderName,SessionID | [color=#795e26]ForEach-object[/color] { 
   [color=#001080]$disk[/color] = $_
   [color=#001080]$user[/color] = [color=#795e26]Get-WMIObject[/color] Win32_LoggedOnUser -ComputerName [color=#001080]$ComputerName[/color] | [color=#795e26]Where-object[/color] {($_[color=#001080].Dependent.split[/color]([color=#a31515]"="[/color])[[color=#09885a]-1[/color]] -replace [color=#a31515]'"'[/color]) -eq [color=#001080]$disk.SessionID[/color]} | [color=#795e26]ForEach-object[/color] {$_[color=#001080].Antecedent.split[/color]([color=#a31515]"="[/color])[[color=#09885a]-1[/color]] -replace [color=#a31515]'"'[/color]}
   [color=#001080]$disk[/color] | [color=#795e26]Select-object[/color] SystemName,[color=#0000ff]@[/color]{[color=#001080]n[/color]=[color=#a31515]"MappedTo"[/color];[color=#001080]e[/color]={[color=#001080]$user[/color]}},Name,ProviderName
} 
[color=#001080]$drives[/color] | [color=#795e26]Export-Csv[/color] [color=#001080]$path[/color] -NoTypeInformation
[/font][/color]
[color=#795E26][font=Consolas]Start-Process [/font][/color][color=#001080][font=Consolas]$path[/font][/color]

  • Thanks 1
Posted
Thank you that's working for me now. Is it possible to add a date and time too and blank line after the entry. The idea is when I have people telling then the drives are not showing I can get the results and make any changes and then confirm its worked by running again. Thanks again for your help. I'm not sure why the script doesn't always work for other people as its worked here every time I've tested it.
Posted

I've managed to add date and line break but I'm sure someone can tell me a much neater way of doing it.

Thanks

 

 

 

$ComputerName = "pcname"

$path = "$PSScriptRoot\drives.csv"

$Date = Get-Date

$drives = Get-WmiObject Win32_MappedLogicalDisk -ComputerName $ComputerName | Select-Object SystemName,Name,ProviderName,SessionID | ForEach-object {

$disk = $_

$user = Get-WMIObject Win32_LoggedOnUser -ComputerName $ComputerName | Where-object {($_.Dependent.split("=")[-1] -replace '"') -eq $disk.SessionID} | ForEach-object {$_.Antecedent.split("=")[-1] -replace '"'}

$disk | Select-object SystemName,@{n="MappedTo";e={$user}},Name,ProviderName

}

Add-Content $path ""

Add-Content $path $DATE

$drives | Export-Csv $path -NoTypeInformation -Append

 

Start-Process $path

Posted

You could just add it as a column in the csv. Adding it at the top of the file messes with the filter sort functions if you had a larger data set. Doing it this way keeps columns and rows lined up.

 

If you keep the append on, you will have a record with time stamp of before and after in the same file.

 

Also the start-process isn't needed for function, its just a quick way to open the resulting file. If there were a silent / scheduled script I wouldn't include it. However it sounds like you are running this interactively.

You could also use variables within the path to be drives-pcname-date.csv or something of the like.

 

[color=#000000][font=Consolas][color=#001080]$ComputerName[/color] = [color=#a31515]"pcname"[/color]
[color=#001080]$path[/color] = [color=#a31515]"[/color][color=#001080]$PSScriptRoot[/color][color=#a31515]\drives.csv"[/color]
[color=#001080]$Date[/color] = [color=#795e26]Get-Date[/color]
[color=#001080]$drives[/color] = [color=#795e26]Get-WmiObject[/color] Win32_MappedLogicalDisk -ComputerName [color=#001080]$ComputerName[/color] | [color=#795e26]Select-Object[/color] SystemName,Name,ProviderName,SessionID | [color=#795e26]ForEach-object[/color] { 
   [color=#001080]$disk[/color] = $_
   [color=#001080]$user[/color] = [color=#795e26]Get-WMIObject[/color] Win32_LoggedOnUser -ComputerName [color=#001080]$ComputerName[/color] | [color=#795e26]Where-object[/color] {($_[color=#001080].Dependent.split[/color]([color=#a31515]"="[/color])[[color=#09885a]-1[/color]] -replace [color=#a31515]'"'[/color]) -eq [color=#001080]$disk.SessionID[/color]} | [color=#795e26]ForEach-object[/color] {$_[color=#001080].Antecedent.split[/color]([color=#a31515]"="[/color])[[color=#09885a]-1[/color]] -replace [color=#a31515]'"'[/color]}
   [color=#001080]$disk[/color] | [color=#795e26]Select-object[/color] SystemName,[color=#0000ff]@[/color]{[color=#001080]n[/color]=[color=#a31515]"MappedTo"[/color];[color=#001080]e[/color]={[color=#001080]$user[/color]}},Name,ProviderName,[color=#0000ff]@[/color]{[color=#001080]n[/color]=[color=#a31515]"Date Checked"[/color];[color=#001080]e[/color]={[color=#001080]$Date[/color]}}
} 
[color=#001080]$drives[/color] | [color=#795e26]Export-Csv[/color] [color=#001080]$path[/color] -NoTypeInformation -Append
[color=#795e26]Start-Process[/color] [color=#001080]$path[/color]
[/font][/color]

  • Thanks 1
Posted (edited)

[system.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null

$ComputerName = [Microsoft.VisualBasic.Interaction]::InputBox("Enter computer name", "ComputerName", "$env:ComputerName")

$path = "d:\mapped-drives.csv"

$Date = Get-Date

$drives = Get-WmiObject Win32_MappedLogicalDisk -ComputerName $ComputerName | Select-Object SystemName,Name,ProviderName,SessionID | ForEach-object {

$disk = $_

$user = Get-WMIObject Win32_LoggedOnUser -ComputerName $ComputerName | Where-object {($_.Dependent.split("=")[-1] -replace '"') -eq $disk.SessionID} | ForEach-object {$_.Antecedent.split("=")[-1] -replace '"'}

$disk | Select-object SystemName,@{n="MappedTo";e={$user}},Name,ProviderName,@{n="Date Checked";e={$Date}}

}

[system.Windows.Forms.MessageBox]::Show("Information saved to $path")

 

$drives | Export-Csv $path -NoTypeInformation -Append

 

Add-Type -AssemblyName PresentationCore,PresentationFramework

$ButtonType = [system.Windows.MessageBoxButton]::YesNo

$MessageIcon = [system.Windows.MessageBoxImage]::Question

$MessageBody = "Would you like to load the file?"

$MessageTitle = "Load File"

 

$Result = [system.Windows.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)

switch ($Result) {

 

'Yes' {

 

Start-Process $path

}

 

'No' {

$drives | Write-Host

#Exit

 

}

 

}

Edited by tri_94

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...