mkellerman / Invoke-CommandAs

Invoke Command As System/Interactive/GMSA/User on Local/Remote machine & returns PSObjects.
https://www.powershellgallery.com/packages/Invoke-CommandAs/
MIT License
455 stars 71 forks source link

Support for PowerShell 7 #44

Open janegilring opened 4 years ago

janegilring commented 4 years ago

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

mkellerman commented 4 years ago

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?

janegilring commented 4 years ago

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.

germeshausen commented 2 years ago

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

mkellerman commented 2 years ago

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.

pluto00987 commented 8 months ago

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

I think this was a false alarm. As of Powershell 7.4 that module is not available and shows a similar error as mentioned under 6.x. For additional explanation it suggests running Import-Module which says this: Module 'PSScheduledJob' is blocked from loading using Windows PowerShell compatibility feature by a 'WindowsPowerShellCompatibilityModuleDenyList' setting in PowerShell configuration file.

See also https://github.com/PowerShell/PowerShell/issues/12743#issuecomment-631939937

So the underlying issue for PS 6 and 7 support is that the PSScheduledJob module is no longer available.

Edit: Corrected module name PSScheduledJob singular, not PSScheduledJobs

mkellerman commented 8 months ago

Has anyone worked on this? Is there a fix?

danubie commented 6 months ago

Within the last days I'm looking for a solution on that issue.

Due to the fact, that scheduled jobs are not supported in PSCore, I'll trying the following workaround:

On my home Win10 it already looks promissing. But I have to look for the opportunity to test it with Windows server releases local and via remote sessions.

Additionally, I would like to upgrade tests to Pesterv5 syntax.

dcmacdonald commented 6 months ago

I could help with server tests if you want.

danubie commented 6 months ago

Thanks for your offer. I managed to get a local container running for first server tests. If it's ready, I'll come back to it.

danubie commented 5 months ago

@dcmacdonald : I've prepared a first version of my idea of supporting PS7 (https://github.com/danubie/Invoke-CommandAs/tree/PS7-support).

What it does: To replace the missing 'Register-ScheduledJob' my solution

Open issue: scriptblock haveing $using. I did not use this kind of scriptblock feature with Invoke-CommandAs, Maybe @mkellerman has an example for me.

I would greatly appreciate tests by you. Local tests using the powershell core container and a test system at our company did quite well.

@mkellerman: Is this idea for supporting PS7 ok for you?

Thanks in advance, Wolfgang

mkellerman commented 4 months ago

@danubie, nice work! I would recommend not using files writen to disk for the scriptblock and parameters. I'll need some time to evaluate (which i'm unfortunatly limited on).

mkellerman commented 1 week ago

Anyone working on this? Should i close this ticket?