Особо не тестировал,но у меня под Windows 7 отработало,как надо.
Код:
$code = @"
const int UNLEN = 256;
const int PWLEN = 256;
const int DNLEN = 15;
[DllImport("Rasapi32.dll ", CharSet=CharSet.Auto, SetLastError=true)]
public static extern uint RasSetCredentials(
[MarshalAs(UnmanagedType.LPWStr)]
string lpszPhonebook,
[MarshalAs(UnmanagedType.LPWStr)]
string lpszEntry,
LPRASCREDENTIALS lpCredentials,
bool fClearCredentials
);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct LPRASCREDENTIALS {
public uint dwSize;
public uint dwMask;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = UNLEN + 1)]
public string szUserName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = PWLEN + 1)]
public string szPassword;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = DNLEN + 1)]
public string szDomain;
}
"@
Add-Type -MemberDefinition $code -Namespace Ras -Name RasCred
#Путь к Phonebook
$pbk = "$env:APPDATA\Microsoft\Network\Connections\Pbk\rasphone.pbk"
#Название соединения
$con = "MyNet"
$StrCred = New-Object Ras.RasCred+LPRASCREDENTIALS
<#
http://msdn.microsoft.com/en-us/library/aa376730(v=vs.85).aspx
dwMask флаги
3 - 1 -bor 2 (UserName -bor Password)
RASCM_UserName = 0x00000001,
RASCM_Password = 0x00000002,
RASCM_Domain = 0x00000004,
RASCM_DefaultCreds = 0x00000008,
RASCM_PreSharedKey = 0x00000010,
RASCM_ServerPreSharedKey = 0x00000020,
RASCM_DDMPreSharedKey = 0x00000040
#>
$StrCred.dwMask = 3
$StrCred.szUserName = "Test"
$StrCred.szPassword = "Test"
[Ras.RasCred]::RasSetCredentials($pbk,$con,$StrCred,$false)