Skip to content

Instantly share code, notes, and snippets.

@crshnbrn66
Forked from adamdriscoll/CreateProfile.ps1
Last active March 24, 2017 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crshnbrn66/699ea9ab3af437b443351e937bdd812a to your computer and use it in GitHub Desktop.
Save crshnbrn66/699ea9ab3af437b443351e937bdd812a to your computer and use it in GitHub Desktop.
Create profile
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
#$pszProfilePath = $Null
[PInvoke]::CreateProfile($UserSid, $UserName, $pszProfilePath, $ProfilePath)
}
$stringbuff = new-object system.text.stringbuilder(260)
[system.uint32]$a =$stringbuff.capacity
$sid = ((get-aduser -id 'testuser').sid.value)
CreateProfile -usersid $sid -username 'testuser' -ProfilePath $a
@crshnbrn66
Copy link
Author

Any pointers for this:

Cannot convert argument "pszProfilePath", with value: "System.Management.Automation.PSReference1[System.Text.StringBuilder]", for "CreateProfile" to type "System.Text.StringBuilder": "Cannot convert the "System.Management.Automation.PSReference1[System.Text.StringBuilder]" value of type "System.Management.Automation.PSReference`1[[System.Text.StringBuilder, mscorlib, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089]]" to type "System.Text.StringBuilder"."

@Jaykul
Copy link

Jaykul commented Mar 24, 2017

Need to remove the [ref] on $pszProfilePath on line 14

@crshnbrn66
Copy link
Author

Removed the ref.. now the darn thing blows up Shell..... HMM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment