2017-03-24 2 views
0

Lorsque vous appelez un morceau non géré de code avec pinvoke-createprofile. Le processus Powershell.exe se bloque après l'appel à la méthode dans le code non managé. Le profil est créé avec succès.Powershell - appelant tombe en panne CreateProfile non géré Fonction Powershell.exe

Pourquoi se passerait-il? Mon code est ci-dessous:

function CreateProfile 
    { 
     param([String]$UserSid, [String]$UserName, [system.uint32]$ProfilePath) 
     Add-Type -TypeDefinition ' 
      using System; 
      using System.Runtime.InteropServices; 
      public static class PInvoke { 
       [DllImport("userenv.dll", SetLastError = true, CharSet = CharSet.Auto)] 
       public static extern int CreateProfile([MarshalAs(UnmanagedType.LPWStr)] String pszUserSid, [MarshalAs(UnmanagedType.LPWStr)] String pszUserName, [Out][MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszProfilePath, uint cchProfilePath); 
      } 
     ' 
     $pszProfilePath = new-object -typename System.Text.StringBuilder 
    [int]$results = [PInvoke]::CreateProfile($UserSid, $UserName, $pszProfilePath, $ProfilePath) 
    } 
$stringbuff = new-object system.text.stringbuilder(260) 
[system.uint32]$a =$stringbuff.capacity 
$sid = ((get-aduser -id 'brtestlocaluser').sid.value) 
CreateProfile -usersid $sid -username 'brtestlocaluser' -ProfilePath $a 
+1

Vous pouvez omettre le code avec try catch bloque et affiche les journaux ou essaie de déboguer le code dans ISE pour voir où il échoue. faire une enquête par vous-même d'abord. – pandemic

+0

Vous venez de créer passe 'StringBuilder' (' Capacity' = 0) dans le troisième paramètre de 'CreateProfile' et 260 dans le quatrième paramètre de' CreateProfile'. Depuis actuall capacité (0) est inférieure à la capacité déclarée (260), 'CreateProfile' corrompt la mémoire et causant arrêt de processus. – PetSerAl

Répondre

0

Enfin a été en mesure de comprendre cela. L'appeler dans un autre moyen semblait résoudre ce problème.

function Register-NativeMethod 
{ 
    [CmdletBinding()] 
    [Alias()] 
    [OutputType([int])] 
    Param 
    (
     # Param1 help description 
     [Parameter(Mandatory=$true, 
        ValueFromPipelineByPropertyName=$true, 
        Position=0)] 
     [string]$dll, 

     # Param2 help description 
     [Parameter(Mandatory=$true, 
        ValueFromPipelineByPropertyName=$true, 
        Position=1)] 
     [string] 
     $methodSignature 
    ) 

    $script:nativeMethods += [PSCustomObject]@{ Dll = $dll; Signature = $methodSignature; } 
} 
function Add-NativeMethods 
{ 
    [CmdletBinding()] 
    [Alias()] 
    [OutputType([int])] 
    Param($typeName = 'NativeMethods') 

    $nativeMethodsCode = $script:nativeMethods | ForEach-Object { " 
     [DllImport(`"$($_.Dll)`")] 
     public static extern $($_.Signature); 
    " } 

    Add-Type @" 
     using System; 
     using System.Text; 
     using System.Runtime.InteropServices; 
     public static class $typeName { 
      $nativeMethodsCode 
     } 
"@ 
} 
function New-ProfileFromSID { 

[CmdletBinding()] 
[Alias()] 
[OutputType([int])] 
Param 
(
    # Param1 help description 
    [Parameter(Mandatory=$true, 
       ValueFromPipelineByPropertyName=$true, 
       Position=0)] 
    [string]$UserName, 
    [string]$domain = '' 
) 
$methodname = 'UserEnvCP2' 
$script:nativeMethods = @(); 

if (-not ([System.Management.Automation.PSTypeName]$methodname).Type) 
{ 
    Register-NativeMethod "userenv.dll" "int CreateProfile([MarshalAs(UnmanagedType.LPWStr)] string pszUserSid,` 
    [MarshalAs(UnmanagedType.LPWStr)] string pszUserName,` 
    [Out][MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszProfilePath, uint cchProfilePath)"; 

    Add-NativeMethods -typeName $methodname; 
} 

$sb = new-object System.Text.StringBuilder(260); 
$pathLen = $sb.Capacity; 

Write-Verbose "Creating user profile for $Username"; 
#$SID= ((get-aduser -id $UserName -ErrorAction Stop).sid.value) 
if($domain) 
{ 
    $objUser = New-Object System.Security.Principal.NTAccount($domain, $UserName) 
    $strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier]) 
    $SID = $strSID.Value 
} 
else 
{ 
    $objUser = New-Object System.Security.Principal.NTAccount($UserName) 
    $strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier]) 
    $SID = $strSID.Value 
} 
Write-Verbose "$UserName SID: $SID" 
try 
{ 
    $result = [UserEnvCP2]::CreateProfile($SID, $Username, $sb, $pathLen) 
    if($result -eq '-2147024713') 
    { 
     $status = "$userName already exists" 
     write-verbose "$username Creation Result: $result" 
    } 
    elseif($result -eq '-2147024809') 
    { 
     $staus = "$username Not Found" 
     write-verbose "$username creation result: $result" 
    } 
    elseif($result -eq 0) 
    { 
     $status = "$username Profile has been created" 
     write-verbose "$username Creation Result: $result" 
    } 
    else 
    { 
     $status = "$UserName unknown return result: $result" 
    } 
} 
catch 
{ 
    Write-Error $_.Exception.Message; 
    break; 
} 
$status 
}