1

好的,我会尽量保持简短。

首先让我解释一下我想要得到什么。如果您打开 Windows 资源管理器并转到网络驱动器,那里有一个 DFS 选项卡(必须通过网络上的服务器启用 DFS,因此它可能不存在)。

在该选项卡中有一个名为“推荐列表”的列表......我想要那个框中的内容。我相信这是 DFS 或 UNC,您可以纠正我,它会对我有所帮助。

我所拥有的是 \domainname.com\something$\BUS\blah\myDriveHome 但这与该框中的其他内容相关联,该框中包含该共享设置的实际服务器,并且该共享是我运行合规性所需的查看。

我不能使用未与 Windows 7 打包的 exe,也不能使用任何其他 exe,因为我们无法分发 exe。

那么我做了什么……从命令行、powershell 和注册表对 DFS/UNC 路径等内容进行了非常彻底的搜索,但没有成功。命令行“net use”只返回链接路径而不是服务器,所以没用。

我只会在遇到占用大量编程时间的墙时发布问题。

如果有人有信息,将不胜感激。

在此处输入图像描述

谢谢

4

2 回答 2

0

我使用 PSEXEC 和 DFSUtil 去另一个方向通过远程 PC 找到 DFS 信息。返回很多信息,但我在阅读文件并匹配 UNC 后在 PowerShell 中对其进行了过滤。我会发布如何做,但我必须在最后做一些重大调整,其中包含 DFSUtil 的其他一些网站上的信息以及要查找的内容和 PSExec。我会在 PSEXEC 中注意这一点:

cmd.exe /s /c C:\Temp\psexec.exe 2> $null

如果返回在错误通道中,“2> $null”将为您省去一些麻烦,并且您的脚本会崩溃。您将需要在 PS 控制台中运行它,尽管没有它来捕获错误,但是当您有像我这样的脚本执行 50 多个系统检查时,您不希望整个事情只因一个错误而停止。

于 2016-03-22T15:28:37.767 回答
0

我能够在此处窃取此答案中的 C# 代码并进行一些修改,使其适用于 .Net 2.0,并在 PowerShell 中使用它:

$dfsCode = @'
using System;
using System.Runtime.InteropServices;

public static class Dfs
{
    private enum NetDfsInfoLevel
    {
        DfsInfo1 = 1,
        DfsInfo2 = 2,
        DfsInfo3 = 3,
        DfsInfo4 = 4,
        DfsInfo5 = 5,
        DfsInfo6 = 6,
        DfsInfo7 = 7,
        DfsInfo8 = 8,
        DfsInfo9 = 9,
        DfsInfo50 = 50,
        DfsInfo100 = 100,
        DfsInfo150 = 150,
    }

    [DllImport("netapi32.dll", SetLastError = true)]
    private static extern int NetApiBufferFree(IntPtr buffer);

    [DllImport("Netapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern int NetDfsGetInfo(
        [MarshalAs(UnmanagedType.LPWStr)] string DfsEntryPath, // DFS entry path for the volume
        [MarshalAs(UnmanagedType.LPWStr)] string ServerName,   // This parameter is currently ignored and should be NULL
        [MarshalAs(UnmanagedType.LPWStr)] string ShareName,    // This parameter is currently ignored and should be NULL.
        NetDfsInfoLevel Level,                                 // Level of information requested
        out IntPtr Buffer                                      // API allocates and returns buffer with requested info
        );

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    private struct DFS_INFO_3
    {
        [MarshalAs(UnmanagedType.LPWStr)]
        public string EntryPath;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string Comment;
        public int State;
        public int NumberOfStorages;
        public IntPtr Storage;
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    private struct DFS_STORAGE_INFO
    {
        public int State;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string ServerName;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string ShareName;
    }

    private static T GetStruct<T>(IntPtr buffer, int offset)where T:struct
    {
        T r = new T();
        r = (T) Marshal.PtrToStructure((IntPtr)((long)buffer + offset * Marshal.SizeOf(r)), typeof(T));
        return r;
    }

    public static string GetDfsInfo(string server)
    {
        string rval = null;
        IntPtr b;
        int r = NetDfsGetInfo(server, null, null, NetDfsInfoLevel.DfsInfo3, out b);
        if(r != 0)
        {
            NetApiBufferFree(b);

            // return passed string if not DFS
            return rval;
        }

        DFS_INFO_3 sRes = GetStruct<DFS_INFO_3>(b,0);
        if(sRes.NumberOfStorages > 0)
        {
            DFS_STORAGE_INFO sResInfo = GetStruct<DFS_STORAGE_INFO>(sRes.Storage,0);
            rval = string.Concat(@"\\", sResInfo.ServerName, @"\", sResInfo.ShareName, @"\");
        }

        NetApiBufferFree(b);

        return rval;
    }
}
'@

Add-Type -TypeDefinition $dfsCode

[Dfs]::GetDfsInfo('\\ad.domain.com\Share')

此代码适用于 Windows 7 随附的 PowerShell 2.0。

于 2015-08-06T19:58:10.207 回答