Invoke-CommandAs icon indicating copy to clipboard operation
Invoke-CommandAs copied to clipboard

Support for PowerShell 7

Open janegilring opened this issue 5 years ago • 12 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

janegilring avatar Jan 31 '20 05:01 janegilring

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?

mkellerman avatar Feb 06 '20 00:02 mkellerman

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.

janegilring avatar Feb 09 '20 10:02 janegilring

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

germeshausen avatar May 11 '22 09:05 germeshausen

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.

mkellerman avatar May 14 '22 20:05 mkellerman

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

pluto00987 avatar Jan 22 '24 18:01 pluto00987

Has anyone worked on this? Is there a fix?

mkellerman avatar Jan 24 '24 17:01 mkellerman