diff --git a/GMSACredential/Get-GMSACredential.ps1 b/GMSACredential/Commands/Get-GMSACredential.ps1 similarity index 100% rename from GMSACredential/Get-GMSACredential.ps1 rename to GMSACredential/Commands/Get-GMSACredential.ps1 diff --git a/GMSACredential/Commands/Invoke-GMSACommand.ps1 b/GMSACredential/Commands/Invoke-GMSACommand.ps1 new file mode 100644 index 0000000..ed71e62 --- /dev/null +++ b/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 +} \ No newline at end of file diff --git a/GMSACredential/GMSACredential.psd1 b/GMSACredential/GMSACredential.psd1 index 57d98de..f75c33f 100644 --- a/GMSACredential/GMSACredential.psd1 +++ b/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 diff --git a/GMSACredential/GMSACredential.psm1 b/GMSACredential/GMSACredential.psm1 index 9816b83..b03769f 100644 --- a/GMSACredential/GMSACredential.psm1 +++ b/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' \ No newline at end of file +. "$PSScriptRoot\Commands\Get-GMSACredential.ps1" +. "$PSScriptRoot\Commands\Invoke-GMSACommand.ps1" + + +Export-ModuleMember -Function @('Get-GMSACredential','Invoke-GMSACommand') \ No newline at end of file diff --git a/GMSACredential/refs/net46/SimpleImpersonation.dll b/GMSACredential/refs/net46/SimpleImpersonation.dll new file mode 100644 index 0000000..9b09c33 Binary files /dev/null and b/GMSACredential/refs/net46/SimpleImpersonation.dll differ diff --git a/GMSACredential/refs/netstandard2.0/SimpleImpersonation.dll b/GMSACredential/refs/netstandard2.0/SimpleImpersonation.dll new file mode 100644 index 0000000..ec34b04 Binary files /dev/null and b/GMSACredential/refs/netstandard2.0/SimpleImpersonation.dll differ diff --git a/GMSACredential/refs/netstandard2.0/System.Security.Principal.Windows.dll b/GMSACredential/refs/netstandard2.0/System.Security.Principal.Windows.dll new file mode 100644 index 0000000..afd187c Binary files /dev/null and b/GMSACredential/refs/netstandard2.0/System.Security.Principal.Windows.dll differ