mkellerman / Invoke-CommandAs

Invoke Command As System/Interactive/GMSA/User on Local/Remote machine & returns PSObjects.

Home Page:https://www.powershellgallery.com/packages/Invoke-CommandAs/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for PowerShell 7

janegilring opened this issue · comments

In PowerShell Core 6.2, this error is returned:
Invoke-ScheduledTask : The term 'Register-ScheduledJob' is not recognized as the name of a cmdlet, function, script file, or operable program.

Since the module leverages PSScheduledJobs which is not available in Core, this is expected.

However, in PowerShell 7 it seems like the PSScheduledJobs module is back, but there is a different error:

Invoke-CommandAs -ScriptBlock {whoami} -System Write-Error: C:\Users\janring\Documents\PowerShell\Modules\Invoke-CommandAs\3.1.6\public\Invoke-CommandAs.ps1 Line | 399 | Invoke-ScheduledTask @Parameters | ^ Cannot bind parameter 'ScriptBlock'. Cannot convert the " Param($Parameters) | $JobParameters = @{} If ($Parameters.ScriptBlock) { $JobParameters['ScriptBlock'] = | [ScriptBlock]::Create($Parameters.ScriptBlock) } If ($Parameters.ArgumentList) { | $JobParameters['ArgumentList'] = $Parameters.ArgumentList } If ($Parameters.Using) { | $Parameters.Using | % { Set-Variable -Name $_.Name -Value | ([System.Management.Automation.PSSerializer]::Deserialize($_.Value)) } Start-Job | @JobParameters | Receive-Job -Wait -AutoRemoveJob } Else { Invoke-Command | @JobParameters }" value of type "System.String" to type | "System.Management.Automation.ScriptBlock".

$PSVersionTable
PSVersion 7.0.0-rc.1
PSEdition Core
GitCommitId 7.0.0-rc.1
OS Microsoft Windows 10.0.18363
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0

I've installed Powershell 7.0.0-rc.1 on both my client (source) and my server (destination).

Every time i execute Invoke-CommandAs, it returns PSVersion 5.1... can't get it to run on 7 in my remote session.. am i missing something?

Code i use to test:

Import-Module .\Invoke-CommandAs -Force

If (!$Credential) { $Credential = Get-Credential '.\Administrator' }
If (!$PSSession) { $PSSession = New-PSSession -ComputerName '192.168.86.80' -Credential $Credential }

$ScriptBlock = { 

    Write-Warning "Computer  : $($Env:ComputerName)"
    Write-Warning "PSVersion : $($PSVersionTable.PSVersion.ToString())"

    [System.Security.Principal.Windowsidentity]::GetCurrent()
    
}

Invoke-CommandAs -Session $PSSession -ScriptBlock $ScriptBlock -ASSystem

Result:

RunspaceId         : ee68d0d2-4a5d-4f4d-afe8-a39bea22cbba
AuthenticationType : NTLM
ImpersonationLevel : None
IsAuthenticated    : True
IsGuest            : False
IsSystem           : True
IsAnonymous        : False
Name               : NT AUTHORITY\SYSTEM
Owner              : S-1-5-18
User               : S-1-5-18
Groups             : {S-1-1-0, S-1-5-32-545, S-1-5-6, S-1-2-1…}
Token              : 2116
AccessToken        : Microsoft.Win32.SafeHandles.SafeAccessTokenHandle
UserClaims         : {http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name: NT AUTHORITY\SYSTEM, http://schemas.microsoft.com/ws/2008/06/identity/claims/primarysid: S-1-5-18,
                     http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid: S-1-1-0, http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid: S-1-5-32-545…}
DeviceClaims       : {}
Claims             : {http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name: NT AUTHORITY\SYSTEM, http://schemas.microsoft.com/ws/2008/06/identity/claims/primarysid: S-1-5-18,
                     http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid: S-1-1-0, http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid: S-1-5-32-545…}
Actor              :
BootstrapContext   :
Label              :
NameClaimType      : http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name
RoleClaimType      : http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid

WARNING: Computer  : MK-TESTSERVER
WARNING: PSVersion : 5.1.14393.3383

What am i doing wrong?

From within Windows PowerShell on your target machine (server), run Get-PSSessionConfiguration and you will see the available session configurations.
Currently, the microsoft.powershell endpoint is the default, even when remoting from PowerShell Core or PowerShell 7.
I believe Steve Lee on the PowerShell mentioned on Twitter recently that they plan to change this behaviour in PowerShell 7.1, so that the latest PowerShell 7.x session configuration will be the default.

Until then, you can simply specify -ConfigurationName when using Invoke-Command/New-PSSession:

Invoke-Command -ComputerName . {$PSVersionTable} -ConfigurationName PowerShell.7.0.0-rc.2

This will of course require that Enable-PSRemoting has been run in PowerShell 7.

commented

Had been a while ago and i'd like to ask if there is something in the pipe regarding powershell 7.2.

I have t had an opportunity or requirement to do any work on this for my work. If someone cares to give it a try and submit a PR, I’d gladly test and merge this in.