2

I'm making a script to collect the names and paths of all mapped drives on the domain. Unfortunately, due to WMI permissions, I can't simply pull them over the network as I get a WMI access denied error when I run the script. So I thought instead I would deploy the script to run locally on each computer and have them write the results to a share location that all users have access to.

The script works fine when I manually type the commands into PowerShell, but when I try deploying it via PDQ Deploy the results file gets generated but is 0 KB. What could be causing it to fail when running via PDQ? I have also noticed that it only works locally when I run it as myself, if I run it as the local admin or domain admin it fails in the same way. Perhaps I need to have it run as a local user? Is there any way to do so?

$computername = hostname
Get-WmiObject Win32_MappedLogicalDisk -ComputerName $computername | Out-File -Append \\servername\results\results.txt
1
  • Mapped drives are per-user, not per-computer. Commented Nov 21, 2017 at 20:52

1 Answer 1

2

As Bill Stewart mentioned in the comments, mapped drives are per user. The values are stored in each user's registry. Here is a function that pulls the information from each user's HKCU:\Network and HKCU:\Volatile Environment (Drives mapped from AD Home Share) via PSRemoting.

function Get-MappedDrive {
    [CmdletBinding(SupportsShouldProcess = $True, ConfirmImpact = 'Low')]
    param (
        [Parameter(Mandatory = $True,
            ValueFromPipelineByPropertyName = $True,
            Position = 0)]
        [string[]]$ComputerName
    )

    begin {}

    process {
        if ($pscmdlet.ShouldProcess($ComputerName)) {
            Invoke-Command -ComputerName $ComputerName {
                New-PSDrive -Name HKU -PSProvider Registry -Root HKEY_USERS | Out-Null
                Get-ChildItem HKU:\ |
                    ForEach-Object {Get-ChildItem "$($_.pspath)\Network" -ErrorAction SilentlyContinue} |
                    ForEach-Object {
                        [PSCustomObject]@{
                            User = (New-Object System.Security.Principal.SecurityIdentifier ($_.name -replace 'HKEY_USERS\\(.*?)\\.*','$1')).Translate( [System.Security.Principal.NTAccount]).Value
                            Drive = "$((Split-Path $_.name -Leaf).ToUpper()):"
                            Path = Get-ItemProperty -Path $_.PSPath -Name RemotePath | Select-Object -ExpandProperty RemotePath
                        }
                    }
                Get-ChildItem HKU:\ |
                    ForEach-Object {
                        if (Get-ItemProperty -Path "$($_.PSPath)\Volatile Environment" -name HOMEDRIVE -ErrorAction SilentlyContinue) {
                            [PSCustomObject]@{
                                User = (New-Object System.Security.Principal.SecurityIdentifier ($_.name -replace 'HKEY_USERS.(.*?)(\\.*|$)','$1')).Translate( [System.Security.Principal.NTAccount]).Value
                                Drive = Get-ItemProperty -Path "$($_.PSPath)\Volatile Environment" -name HOMEDRIVE | Select-Object -ExpandProperty HOMEDRIVE
                                Path = Get-ItemProperty -Path "$($_.PSPath)\Volatile Environment" -name HOMESHARE | Select-Object -ExpandProperty HOMESHARE
                            }
                        }
                    }
                Remove-PSDrive HKU | Out-Null

            }
        }
    }
    end {}
}

As for why the PDQ Deploy Script is failing is that the PowerShell script is being run as an account without permission to the file share. You could use net use \\server\folder username password or (New-Object -ComObject WScript.Network).MapNetworkDrive('Z:','\\server\folder',$false, 'username', 'password') to map the share with different credentials.

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.