【问题标题】:get DFS/UNC info progromatically - Java以编程方式获取 DFS/UNC 信息 - Java
【发布时间】:2015-10-29 22:19:36
【问题描述】:

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

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

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

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

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

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

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

如果有人知道,不胜感激。

谢谢

【问题讨论】:

  • 你能贴一张你想要的信息的屏幕截图吗?
  • @AaronJensen 看起来他想要资源管理器中 DFS 选项卡中的 DFS 推荐列表。
  • 您需要列表中的所有服务器,还是只需要当前的服务器?
  • 不,我们在列表中只有一台服务器,因此无需捕获多个。
  • 还是一无所获,我已经尝试了很多方法... Windows 资源管理器如何在引用框中显示服务器但没有在任何地方列出?即使是一个 powershell 脚本也很好,所以我可以在远程机器上运行它。

标签: java powershell powershell-2.0 unc microsoft-distributed-file-system


【解决方案1】:

我能够窃取 this answer here 中的 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。

【讨论】:

  • 让我把它添加进去,看看我是否可以将它用于我的情况。我相信我在另一个网站上看到过这个,但这看起来有更好的记录。当我有机会实施它时,我会在一天左右与您联系。谢谢!
  • 我无法让它做任何事情。我如何从中获得输出?我需要从上面的脚本中删除任何内容吗?
  • @reddragon72 最后一行是产生输出的内容;这就是你调用它的方式。当然你需要用你自己的替换共享路径。
  • 所以我这样做了。 [Dfs]::GetDfsInfo('\\LOCAL.AD.MYBUSNESS.COM\AmeBUS$\XBUS\NATXVDIHOME06\USERSID') 我当然更改了一些项目名称,但是当我把它放在那里时,我什么也得不到。跨度>
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 2021-12-12
    • 1970-01-01
    • 2017-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    相关资源
    最近更新 更多