Browse Source

added invoke command

main
Ryan Ephgrave 5 years ago
parent
commit
d1ff01f118
  1. 0
      GMSACredential/Commands/Get-GMSACredential.ps1
  2. 42
      GMSACredential/Commands/Invoke-GMSACommand.ps1
  3. 8
      GMSACredential/GMSACredential.psd1
  4. 15
      GMSACredential/GMSACredential.psm1
  5. BIN
      GMSACredential/refs/net46/SimpleImpersonation.dll
  6. BIN
      GMSACredential/refs/netstandard2.0/SimpleImpersonation.dll
  7. BIN
      GMSACredential/refs/netstandard2.0/System.Security.Principal.Windows.dll

0
GMSACredential/Get-GMSACredential.ps1 → GMSACredential/Commands/Get-GMSACredential.ps1

42
GMSACredential/Commands/Invoke-GMSACommand.ps1

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

8
GMSACredential/GMSACredential.psd1

@ -12,7 +12,7 @@
RootModule = '.\GMSACredential.psm1'
# Version number of this module.
ModuleVersion = '0.5'
ModuleVersion = '0.6'
# Supported PSEditions
# CompatiblePSEditions = @()
@ -54,7 +54,7 @@ Description = 'Provides functions to get a usable PSCredential for a GMSA accoun
# RequiredModules = @()
# Assemblies that must be loaded prior to importing this module
# RequiredAssemblies = @()
#RequiredAssemblies = @('.\refs\System.Security.Principal.Windows.dll','.\refs\SimpleImpersonation.dll')
# Script files (.ps1) that are run in the caller's environment prior to importing this module.
# ScriptsToProcess = @()
@ -69,7 +69,7 @@ Description = 'Provides functions to get a usable PSCredential for a GMSA accoun
# NestedModules = @()
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = @('Get-GMSACredential')
FunctionsToExport = @('Get-GMSACredential','Invoke-GMSACommand')
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @()
@ -107,7 +107,7 @@ PrivateData = @{
# IconUri = ''
# ReleaseNotes of this module
# ReleaseNotes = ''
ReleaseNotes = 'Added Invoke-GMSACommand'
} # End of PSData hashtable

15
GMSACredential/GMSACredential.psm1

@ -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')

BIN
GMSACredential/refs/net46/SimpleImpersonation.dll

Binary file not shown.

BIN
GMSACredential/refs/netstandard2.0/SimpleImpersonation.dll

Binary file not shown.

BIN
GMSACredential/refs/netstandard2.0/System.Security.Principal.Windows.dll

Binary file not shown.
Loading…
Cancel
Save