7 changed files with 59 additions and 6 deletions
@ -0,0 +1,42 @@ |
|||
Function Invoke-GMSACommand{ |
|||
<# |
|||
.SYNOPSIS |
|||
Helper command to invoke a scriptblock with credentials (especially helpful with GMSA creds) |
|||
|
|||
.DESCRIPTION |
|||
Will use the open source library SimpleImpersonation to invoke a ScriptBlock with the provided credentials |
|||
|
|||
.PARAMETER ScriptBlock |
|||
Script block to invoke |
|||
|
|||
.PARAMETER ArgumentList |
|||
Argument list for the scriptblock |
|||
|
|||
.PARAMETER Credential |
|||
Credential object (intended to be GMSA credentials, but can be any) |
|||
|
|||
.PARAMETER LogonType |
|||
LogonType Enum - New Credentials are good for most cases. Enum help is here: https://github.com/mj1856/SimpleImpersonation/blob/master/src/LogonType.cs |
|||
|
|||
.EXAMPLE |
|||
Invoke-GMSACommand -ScriptBlock {Write-Host 'test'} -Credential ( Get-GMSACredential -GMSAName 'MyGMSA' -Domain 'test.Domain' ) |
|||
|
|||
.NOTES |
|||
.Author: Ryan Ephgrave |
|||
#> |
|||
Param( |
|||
[ScriptBlock]$ScriptBlock, |
|||
[Object[]]$ArgumentList, |
|||
[PSCredential]$Credential, |
|||
[SimpleImpersonation.LogonType]$LogonType = [SimpleImpersonation.LogonType]::NewCredentials |
|||
) |
|||
$script:CommandOutput = $null |
|||
$SCred = [SimpleImpersonation.UserCredentials]::new($Credential.GetNetworkCredential().Domain,$Credential.GetNetworkCredential().UserName, $Credential.GetNetworkCredential().Password) |
|||
[SimpleImpersonation.Impersonation]::RunAsUser( |
|||
$SCred, |
|||
$LogonType, |
|||
[System.Action]{ $Script:CommandOutput = Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList $ArgumentList } |
|||
) |
|||
$script:CommandOutput |
|||
$script:CommandOutput = $null |
|||
} |
|||
@ -1,3 +1,14 @@ |
|||
. "$PSScriptRoot\Get-GMSACredential.ps1" |
|||
if($PSVersionTable.PSVersion.Major -lt 6){ |
|||
[System.Reflection.Assembly]::LoadWithPartialName("System.Security.Principal.Windows") |
|||
$null = Add-Type -Path "$PSScriptRoot\refs\net46\SimpleImpersonation.dll" |
|||
} |
|||
else{ |
|||
$null = Add-Type -Path "$PSScriptRoot\refs\netstandard2.0\SimpleImpersonation.dll" |
|||
$null = Add-Type -Path "$PSScriptRoot\refs\netstandard2.0\System.Security.Principal.Windows.dll" |
|||
} |
|||
|
|||
Export-ModuleMember -Function 'Get-GMSACredential' |
|||
. "$PSScriptRoot\Commands\Get-GMSACredential.ps1" |
|||
. "$PSScriptRoot\Commands\Invoke-GMSACommand.ps1" |
|||
|
|||
|
|||
Export-ModuleMember -Function @('Get-GMSACredential','Invoke-GMSACommand') |
|||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue