0

I have a log off script that saves mapped network shares to a txt file:

#File path for save
$TxtPath = "C:\temp\" + "$env:UserName" + ".txt"

#Clear existing entries in file
Clear-Content -Path $TxtPath

#Return drive letters
$DriveList = Get-PSDrive | Select-Object -ExpandProperty 'Name' | Select-String -Pattern '^[a-b:d-g:i-z]$'

#Get path for each drive letter and save in specific format (letter;path)
Foreach($Item in $DriveList){
$DrivePath = (Get-PSDrive $Item).DisplayRoot
$Entry = -join($Item, ":", ";", $DrivePath)
Add-Content -Path $TxtPath -Value ($Entry)
}

I then have a logon script that resets and maps these drives:

#File path for user drive paths
$TxtPath = "C:\temp\" + "$env:UserName" + ".txt"

#Get current drives
$DriveList = Get-PSDrive | Select-Object -ExpandProperty 'Name' | Select-String -Pattern '^[a-b:d-g:i-z]$'

#Remove current drives
ForEach($Item in $DriveList){
    $Drive = -join($Item, ":")
    net use $Drive /delete
}

#Map network drives from file
ForEach($Line in Get-Content $TxtPath) {
    $DriveLetter,$DrivePath = $Line.split(';')
    net use $DriveLetter $DrivePath
}

My issue is that, because I am using net use to delete and map the drives (in the logon script), that the Get-PSDrive function in the logoff script does not return a drive path. The reason I am using net use over Remove-PSDrive is due to the drive not being fully removed (still shows on the users device).

Is anyone able to tell me how I can capture the Remote Name value for a network share when I look it up using net use (net use Z:)? If I can simply capture this path (and only this path) I will be able to write that to the text file along with the drive letter and thus solve my issue.

I know I can capture the results of net use by saving the data to a file:

net use x: > C:\Temp\output.txt

However I am unable to save this data / single line of required information to a variable. Any help would be much appreciated.

6
  • Why do you not use the persistent paramter? Commented May 30, 2018 at 6:35
  • Where? The purpose of these scripts is to enable a very basic form of profile management - so the drives will follow the user no matter what device the log into... Commented May 30, 2018 at 6:37
  • ok, when you change devices persistent will no help. Commented May 30, 2018 at 6:39
  • Yep... hence the script... do you have any thoughts about how I could capture the variable from net use? Commented May 30, 2018 at 6:40
  • I can not reproduce your problem. when I use net use and after that search for the drives with get-psdrive I get the root path. Commented May 30, 2018 at 6:50

1 Answer 1

1

One way to do this is look directly in the registry:

Get-ChildItem "HKCU:Network\" |
    ForEach-Object {
        [PsCustomObject]@{
            DriveLetter = $_.PSChildName
            RemotePath = (Get-ItemProperty $_.PSPath).RemotePath
        }
    }

This will give output like this:

DriveLetter RemotePath                
----------- ----------                
M           \\server1\share1 
N           \\server2\share2   
O           \\server3\share3

To save to file, I'd recommend CSV format by adding this after the last bracket:

| Export-Csv <path>\MappedDrive.csv

You can then easily import the data again with Import-Csv.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.