如何显示远程计算机映射的驱动器路径?

时间:2019-01-15 14:01:09

标签: powershell powershell-remoting

我让它在本地工作,但是在远程系统上,它不显示路径,而仅显示驱动器号。我的目标是使它显示远程主机的驱动器路径。

有时,它也不显示映射到远程计算机的所有驱动器,我也不知道为什么。

我尝试将Win32_LogicalDisk更改为MappedLogicalDisk,但这只会导致没有任何信息。

$DISK = Get-WmiObject -computer $compname Win32_LogicalDisk
foreach ($device in $DISK){
    Write-Host      "Drive: " $device.name
    Write-Host      "Path:  " $device.ProviderName
""
}
Pause
CheckHost

1 个答案:

答案 0 :(得分:0)

尝试以下示例之一:

这一个...

$ComputerName = "ServerName"

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} }
}

或者这个

function Get-MappedDrives($ComputerName)
{
$output = @()

if(Test-Connection -ComputerName $ComputerName -Count 1 -Quiet)
{
    $Hive = [long]$HIVE_HKU = 2147483651

    $sessions = Get-WmiObject -ComputerName $ComputerName -Class win32_process | 
    ?{$_.name -eq "explorer.exe"}

    if($sessions)
    {
    foreach($explorer in $sessions)
    {
        $sid = ($explorer.GetOwnerSid()).sid

        $owner  = $explorer.GetOwner()

        $RegProv = get-WmiObject -List -Namespace "root\default" -ComputerName $ComputerName | 
        Where-Object {$_.Name -eq "StdRegProv"}

        $DriveList = $RegProv.EnumKey($Hive, "$($sid)\Network")
        if($DriveList.sNames.count -gt 0)
        {
        foreach($drive in $DriveList.sNames)
        {
        $output += "$($drive)`t$(($RegProv.GetStringValue($Hive, "$($sid)\Network\$($drive)", 
        "RemotePath")).sValue)`t$($owner.Domain)`t$($owner.user)`t$($ComputerName)"
        }
        }
        else{write-debug "No mapped drives on $($ComputerName)"}
    }
    }
    else{write-debug "explorer.exe not running on $($ComputerName)"}
}
else{write-debug "Can't connect to $($ComputerName)"}
return $output
}

<#
#Enable if you want to see the write-debug messages
$DebugPreference = "Continue"

$list = "Server01", "Server02"
$report = $(foreach($ComputerName in $list){Get-MappedDrives $ComputerName}) | 
ConvertFrom-Csv -Delimiter `t -Header Drive, Path, Domain, User, Computer
#>