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
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
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