Advertisement
easternnl

Get servername & share from DFS path

Aug 10th, 2018
532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $signature = @'
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Management.Automation;
  5. using System.Runtime.InteropServices;
  6.  
  7. public class Win32Api
  8. {
  9.    [DllImport("netapi32.dll", SetLastError = true)]
  10.    private static extern int NetApiBufferFree(IntPtr buffer);
  11.  
  12.    [DllImport("Netapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  13.    private static extern int NetDfsGetClientInfo
  14.    (
  15.    [MarshalAs(UnmanagedType.LPWStr)] string EntryPath,
  16.    [MarshalAs(UnmanagedType.LPWStr)] string ServerName,
  17.    [MarshalAs(UnmanagedType.LPWStr)] string ShareName,
  18.    int Level,
  19.    ref IntPtr Buffer
  20.    );
  21.  
  22.    public struct DFS_INFO_3
  23.    {
  24.        [MarshalAs(UnmanagedType.LPWStr)]
  25.        public string EntryPath;
  26.        [MarshalAs(UnmanagedType.LPWStr)]
  27.        public string Comment;
  28.        public UInt32 State;
  29.        public UInt32 NumberOfStorages;
  30.        public IntPtr Storages;
  31.    }
  32.    public struct DFS_STORAGE_INFO
  33.    {
  34.        public Int32 State;
  35.        [MarshalAs(UnmanagedType.LPWStr)]
  36.        public string ServerName;
  37.        [MarshalAs(UnmanagedType.LPWStr)]
  38.        public string ShareName;
  39.    }
  40.  
  41.    public static List<PSObject> NetDfsGetClientInfo(string DfsPath)
  42.    {
  43.        IntPtr buffer = new IntPtr();
  44.        List<PSObject> returnList = new List<PSObject>();
  45.  
  46.        try
  47.        {
  48.            int result = NetDfsGetClientInfo(DfsPath, null, null, 3, ref buffer);
  49.  
  50.            if (result != 0)
  51.            {
  52.                throw (new SystemException("Error getting DFS information"));
  53.            }
  54.            else
  55.            {
  56.                DFS_INFO_3 dfsInfo = (DFS_INFO_3)Marshal.PtrToStructure(buffer, typeof(DFS_INFO_3));
  57.  
  58.                for (int i = 0; i < dfsInfo.NumberOfStorages; i++)
  59.                {
  60.                    IntPtr storage = new IntPtr(dfsInfo.Storages.ToInt64() + i * Marshal.SizeOf(typeof(DFS_STORAGE_INFO)));
  61.  
  62.                    DFS_STORAGE_INFO storageInfo = (DFS_STORAGE_INFO)Marshal.PtrToStructure(storage, typeof(DFS_STORAGE_INFO));
  63.  
  64.                    PSObject psObject = new PSObject();
  65.  
  66.                    psObject.Properties.Add(new PSNoteProperty("State", storageInfo.State));
  67.                    psObject.Properties.Add(new PSNoteProperty("ServerName", storageInfo.ServerName));
  68.                    psObject.Properties.Add(new PSNoteProperty("ShareName", storageInfo.ShareName));
  69.  
  70.                    returnList.Add(psObject);
  71.                }
  72.            }
  73.        }
  74.        catch (Exception e)
  75.        {
  76.            throw(e);
  77.        }
  78.        finally
  79.        {
  80.            NetApiBufferFree(buffer);
  81.        }
  82.        return returnList;
  83.    }
  84. }
  85. '@
  86.  
  87.         if (-not ('Win32Api' -as [Type])) {
  88.             Add-Type -TypeDefinition $signature
  89.         }
  90.    
  91. $fileserver = [Win32Api]::NetDfsGetClientInfo($env:HOMESHARE) | Where-Object { $_.State -eq 6 }
  92.  
  93. $share = ($env:HOMESHARE).Replace("\\corp.net\dfs", "")
  94.  
  95. Write-Output "explorer `"\\$($fileserver.ServerName)\$($fileserver.ShareName)$share`""  | Out-File -Encoding ascii -FilePath "C:\temp\homedrive.cmd"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement