Skip to content

Instantly share code, notes, and snippets.

@mczerniawski
Last active September 3, 2018 20:03
Show Gist options
  • Select an option

  • Save mczerniawski/2f8ed02152e3a9be178445e26f11cace to your computer and use it in GitHub Desktop.

Select an option

Save mczerniawski/2f8ed02152e3a9be178445e26f11cace to your computer and use it in GitHub Desktop.
function Get-LAPSCredential {
<#
.SYNOPSIS
Retrieves LAPS password from AD and creates Credential Object
.DESCRIPTION
Using Local Administrator Password Solution cmdlet Get-AdmPwdPassword will query AD for ms-Mcs-AdmPwd attribute. Will use retrieved password to create Credential Object
.PARAMETER ComputerName
ComputerName to search for LAPS password
.EXAMPLE
Get-LAPSCredential -ComputerName 'SomeComputer'
UserName Password
-------- --------
SomeComputer\Administrator System.Security.SecureString
#>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingPlainTextForPassword', '')]
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,
ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
[string]
$ComputerName
)
Process{
Write-Verbose -Message "Retrieving LAPS Password for Computer {$ComputerName}"
$LAPSPassword = (Get-AdmPwdPassword -ComputerName $ComputerName -ErrorAction SilentlyContinue).Password
If ($LAPSPassword) {
Write-Verbose -Message "Found LAPS Password for Computer {$ComputerName}"
$LocalAdminPassword = ConvertTo-SecureString -String $LAPSPassword -AsPlainText -Force
$LocalAdminCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "$ComputerName\Administrator", $LocalAdminPassword
Write-Verbose -Message "Returning created Credential Object {$($LocalAdminCredential.UserName)}"
$LocalAdminCredential
}
Else {
Write-Error -Message "No LAPS password found for computer {$ComputerName}"
$Null
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment