从PowerShell调用WNetAddConnection2

时间:2009-09-25 13:36:48

标签: c# winapi powershell wnet

我试图从PowerShell脚本直接调用一些Win32 API。我设法使WNetAddConnection使用以下代码:

$cp = New-Object Microsoft.CSharp.CSharpCodeProvider
$cpar = New-Object System.CodeDom.Compiler.CompilerParameters

$Code = @"
using System;
using System.Runtime.InteropServices;
namespace Win32Api
{
    public class Net
    {
        [DllImport("mpr.dll", EntryPoint = "WNetAddConnection")]
        public static extern uint Map(string lpRemoteName, string lpPassword, string lpLocalName);
        [DllImport("mpr.dll", EntryPoint = "WNetCancelConnection")]
      public static extern uint Delete(string lpName, byte fForce);     
    }
}
"@

$cp.CompileAssemblyFromSource($cpar, $code)
[Win32Api.Net]::Map("\\REMKOLAPTOP\C$", $null, "W:")

现在我想做同样的WNetAddConnection2,虽然我找到了正确的declarations for C#我不确定如何在PowerShell中声明这个用法。所以我的问题是:什么是正确的“翻译”,调用API的例子会很棒。

请注意,我不想使用下面的非api方法:

$net = $(New-Object -Com WScript.Network)
$net.MapNetworkDrive("u:", "\\computer\share")

1 个答案:

答案 0 :(得分:4)

我能够在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