我能够在 PowerShell 2.0 上获得以下代码。请注意,我还使用了一个 PSCX ( http://pscx.codeplex.com ) 函数,它只是将 Windows 错误代码转换为消息。我还设置了选项以对用户名和密码进行交互式提示。包含注释掉的脚本,显示如何使用 PowerShell 的 Get-Credential cmdlet 执行相同操作。
顺便说一句,如果您通过变量向函数提供用户名/密码,请确保顺序正确,即密码后跟用户名。那咬了我 10 分钟,直到我终于发现我的订单错了。嗬!另一个花絮,对于像这样的互操作帮助,请务必查看http://www.pinvoke.net。无论如何,希望这会有所帮助。
$WNetAddConnection2WrapperSource = @'
using System;
using System.Runtime.InteropServices;
namespace Win32Api {
public enum ResourceScope {
RESOURCE_CONNECTED = 1,
RESOURCE_GLOBALNET,
RESOURCE_REMEMBERED,
RESOURCE_RECENT,
RESOURCE_CONTEXT
};
public enum ResourceType {
RESOURCETYPE_ANY,
RESOURCETYPE_DISK,
RESOURCETYPE_PRINT,
RESOURCETYPE_RESERVED = 8
};
[Flags]
public enum ResourceUsage {
RESOURCEUSAGE_CONNECTABLE = 0x00000001,
RESOURCEUSAGE_CONTAINER = 0x00000002,
RESOURCEUSAGE_NOLOCALDEVICE = 0x00000004,
RESOURCEUSAGE_SIBLING = 0x00000008,
RESOURCEUSAGE_ATTACHED = 0x00000010,
RESOURCEUSAGE_ALL = (RESOURCEUSAGE_CONNECTABLE |
RESOURCEUSAGE_CONTAINER | RESOURCEUSAGE_ATTACHED),
};
public enum ResourceDisplayType {
RESOURCEDISPLAYTYPE_GENERIC,
RESOURCEDISPLAYTYPE_DOMAIN,
RESOURCEDISPLAYTYPE_SERVER,
RESOURCEDISPLAYTYPE_SHARE,
RESOURCEDISPLAYTYPE_FILE,
RESOURCEDISPLAYTYPE_GROUP,
RESOURCEDISPLAYTYPE_NETWORK,
RESOURCEDISPLAYTYPE_ROOT,
RESOURCEDISPLAYTYPE_SHAREADMIN,
RESOURCEDISPLAYTYPE_DIRECTORY,
RESOURCEDISPLAYTYPE_TREE,
RESOURCEDISPLAYTYPE_NDSCONTAINER
};
[StructLayout(LayoutKind.Sequential)]
public class NetResource {
public ResourceScope Scope;
public ResourceType Type;
public ResourceDisplayType DisplayType;
public ResourceUsage Usage;
public string LocalName;
public string RemoteName;
public string Comment;
public string Provider;
};
[Flags]
public enum AddConnectionOptions {
CONNECT_UPDATE_PROFILE = 0x00000001,
CONNECT_UPDATE_RECENT = 0x00000002,
CONNECT_TEMPORARY = 0x00000004,
CONNECT_INTERACTIVE = 0x00000008,
CONNECT_PROMPT = 0x00000010,
CONNECT_NEED_DRIVE = 0x00000020,
CONNECT_REFCOUNT = 0x00000040,
CONNECT_REDIRECT = 0x00000080,
CONNECT_LOCALDRIVE = 0x00000100,
CONNECT_CURRENT_MEDIA = 0x00000200,
CONNECT_DEFERRED = 0x00000400,
CONNECT_RESERVED = unchecked((int)0xFF000000),
CONNECT_COMMANDLINE = 0x00000800,
CONNECT_CMD_SAVECRED = 0x00001000,
CONNECT_CRED_RESET = 0x00002000
}
public static class NativeMethods {
[DllImport("mpr.dll", EntryPoint="WNetAddConnection2")]
public static extern int WNetAddConnection2(
NetResource netResource, string password,
string username, AddConnectionOptions options);
}
}
'@
Add-Type -TypeDefinition $WNetAddConnection2WrapperSource
$netResource = new-object Win32Api.NetResource
$netResource.Type = [Win32Api.ResourceType]::RESOURCETYPE_DISK
$netResource.LocalName = 'P:'
$netResource.RemoteName = '\\AnotherPC\C'
# Get username and password
#$cred = Get-Credential
#$username = $cred.UserName
#$bstr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($cred.Password)
#$password = [Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr)
#[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr)
$opts = [Win32Api.AddConnectionOptions]::CONNECT_INTERACTIVE -bor
[Win32Api.AddConnectionOptions]::CONNECT_PROMPT -bor
[Win32Api.AddConnectionOptions]::CONNECT_UPDATE_PROFILE
$res = [Win32Api.NativeMethods]::WNetAddConnection2($netResource, 0, 0, $opts)
if ($res -ne 0) {
# This function comes with PSCX http://pscx.codeplex.com
Get-ExceptionForWin32 $res
throw "Failed to connect"
}
# Display results
net use