Skip to content

Instantly share code, notes, and snippets.

@RemiEscourrou
Created August 21, 2019 11:47
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RemiEscourrou/047de6d08da795b0e2d0e3a7858d887c to your computer and use it in GitHub Desktop.
Save RemiEscourrou/047de6d08da795b0e2d0e3a7858d887c to your computer and use it in GitHub Desktop.
Change a password on a remote forest
function Set-PasswordRemotely {
## Change a password on a remote forest.
## Used Get-Credential to avoid password in PSReadLine
## The method above is actually based on NetUserChangePassword function.
## Inspired from @chryzsh
## https://gist.github.com/chryzsh/f814a3d6088c5bc8f1adfafce2eb3779
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)][string] $UserName,
[Parameter(Mandatory = $true)][alias('DC', 'Server', 'ComputerName')][string] $DomainController
)
$DllImport = @'
[DllImport("netapi32.dll", CharSet = CharSet.Unicode)]
public static extern bool NetUserChangePassword(string domain, string username, string oldpassword, string newpassword);
'@
$NetApi32 = Add-Type -MemberDefinition $DllImport -Name 'NetApi32' -Namespace 'Win32' -PassThru
$OldPasswordSecure = Get-Credential $UserName -Message "OldPassword"
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($OldPasswordSecure.Password)
$OldPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
$NewPasswordSecure = Get-Credential $UserName -Message "NewPassword"
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($NewPasswordSecure.Password)
$NewPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
if ($result = $NetApi32::NetUserChangePassword($DomainController, $UserName, $OldPassword, $NewPassword)) {
Write-Output -InputObject 'Password change failed. Please try again.'
} else {
Write-Output -InputObject 'Password change succeeded.'
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment