Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $signature = @'
- using System;
- using System.Collections.Generic;
- using System.Management.Automation;
- using System.Runtime.InteropServices;
- public class Win32Api
- {
- [DllImport("netapi32.dll", SetLastError = true)]
- private static extern int NetApiBufferFree(IntPtr buffer);
- [DllImport("Netapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
- private static extern int NetDfsGetClientInfo
- (
- [MarshalAs(UnmanagedType.LPWStr)] string EntryPath,
- [MarshalAs(UnmanagedType.LPWStr)] string ServerName,
- [MarshalAs(UnmanagedType.LPWStr)] string ShareName,
- int Level,
- ref IntPtr Buffer
- );
- public struct DFS_INFO_3
- {
- [MarshalAs(UnmanagedType.LPWStr)]
- public string EntryPath;
- [MarshalAs(UnmanagedType.LPWStr)]
- public string Comment;
- public UInt32 State;
- public UInt32 NumberOfStorages;
- public IntPtr Storages;
- }
- public struct DFS_STORAGE_INFO
- {
- public Int32 State;
- [MarshalAs(UnmanagedType.LPWStr)]
- public string ServerName;
- [MarshalAs(UnmanagedType.LPWStr)]
- public string ShareName;
- }
- public static List<PSObject> NetDfsGetClientInfo(string DfsPath)
- {
- IntPtr buffer = new IntPtr();
- List<PSObject> returnList = new List<PSObject>();
- try
- {
- int result = NetDfsGetClientInfo(DfsPath, null, null, 3, ref buffer);
- if (result != 0)
- {
- throw (new SystemException("Error getting DFS information"));
- }
- else
- {
- DFS_INFO_3 dfsInfo = (DFS_INFO_3)Marshal.PtrToStructure(buffer, typeof(DFS_INFO_3));
- for (int i = 0; i < dfsInfo.NumberOfStorages; i++)
- {
- IntPtr storage = new IntPtr(dfsInfo.Storages.ToInt64() + i * Marshal.SizeOf(typeof(DFS_STORAGE_INFO)));
- DFS_STORAGE_INFO storageInfo = (DFS_STORAGE_INFO)Marshal.PtrToStructure(storage, typeof(DFS_STORAGE_INFO));
- PSObject psObject = new PSObject();
- psObject.Properties.Add(new PSNoteProperty("State", storageInfo.State));
- psObject.Properties.Add(new PSNoteProperty("ServerName", storageInfo.ServerName));
- psObject.Properties.Add(new PSNoteProperty("ShareName", storageInfo.ShareName));
- returnList.Add(psObject);
- }
- }
- }
- catch (Exception e)
- {
- throw(e);
- }
- finally
- {
- NetApiBufferFree(buffer);
- }
- return returnList;
- }
- }
- '@
- if (-not ('Win32Api' -as [Type])) {
- Add-Type -TypeDefinition $signature
- }
- $fileserver = [Win32Api]::NetDfsGetClientInfo($env:HOMESHARE) | Where-Object { $_.State -eq 6 }
- $share = ($env:HOMESHARE).Replace("\\corp.net\dfs", "")
- 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