java - 以编程方式获取 DFS/UNC 信息 - Java

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

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

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

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

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

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

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

只有当我碰壁而占用大量编程时间时,我才会发布问题。

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

enter image description here

谢谢

最佳答案

我能够窃取 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。

关于java - 以编程方式获取 DFS/UNC 信息 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31861327/

相关文章:

java - 使用 PhantomJS 在 Selenium (Java) 下登录页面时出现 "undefined is not a function"

java - 使用java读取单列excel表

json - Powershell JSON管道将多个值扩展为一列csv

windows-7 - 使用PowerShell完全停止 "explorer"进程

powershell - 通过Powershell作为命令行参数传递时不考虑Msbuild属性

powershell - 您可以在 PowerShell v2 脚本中设置对象的 DefaultDisplayPropertySet 吗?

powershell - 在Powershell中提示输入

java - 如何避免在 java lambda 表达式中定义单个元素数组

java - 从 keystore 中的文件中读取公钥

powershell $lastexitcode 在运行批处理文件时不起作用