tri_94 Posted March 4, 2019 Posted March 4, 2019 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} }
HPlum78 Posted March 4, 2019 Posted March 4, 2019 (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 March 4, 2019 by HPlum78
dayzd Posted March 4, 2019 Posted March 4, 2019 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...
tri_94 Posted March 4, 2019 Author Posted March 4, 2019 (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 March 4, 2019 by tri_94
HPlum78 Posted March 4, 2019 Posted March 4, 2019 Just updated the links in my post (so they work!)! as they point out the limitations that @dayzd is referring to...
tri_94 Posted March 4, 2019 Author Posted March 4, 2019 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
dayzd Posted March 4, 2019 Posted March 4, 2019 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!
ADMaster Posted March 4, 2019 Posted March 4, 2019 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.
dayzd Posted March 6, 2019 Posted March 6, 2019 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 }
ADMaster Posted March 6, 2019 Posted March 6, 2019 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] 1
tri_94 Posted March 7, 2019 Author Posted March 7, 2019 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.
tri_94 Posted March 7, 2019 Author Posted March 7, 2019 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
ADMaster Posted March 7, 2019 Posted March 7, 2019 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] 1
tri_94 Posted March 7, 2019 Author Posted March 7, 2019 (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 March 7, 2019 by tri_94
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now