PowerShell / PowerShell

PowerShell for every system!
https://microsoft.com/PowerShell
MIT License
43.56k stars 7.06k forks source link

Invoke-Command does not support -Startuptype AutomaticDelayedStart #21557

Closed Ryssk closed 2 weeks ago

Ryssk commented 2 weeks ago

Prerequisites

Steps to reproduce

Hi, seems like an issue with using StartupType AutomaticDelayedStart on 7.4.1 while running Invoke-Command, checking the enum i only get the following options aswell. Is it due to running it inside a Scriptblock?

Boot System Automatic Manual Disabled

According to the FAQ it should be working: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/set-service?view=powershell-7.4#-startuptype

Expected behavior

$S = Get-Service -Name Telegraf
Invoke-Command -ComputerName remotepc -ScriptBlock { Set-Service -StartupType "AutomaticDelayedStart" -InputObject $S -Status Running }

Actual behavior

$S = Get-Service -Name Telegraf
Invoke-Command -ComputerName remotepc -ScriptBlock { Set-Service -StartupType "AutomaticDelayedStart" -InputObject $S -Status Running }

Set-Service: Cannot bind parameter 'StartupType'. Cannot convert value "AutomaticDelayedStart" to type "System.ServiceProcess.ServiceStartMode". Error: "Unable to match the identifier name AutomaticDelayedStart to a valid enumerator name. Specify one of the following enumerator names and try again:
Boot, System, Automatic, Manual, Disabled"

Error details

No response

Environment data

Name                           Value
----                           -----
PSVersion                      7.4.1
PSEdition                      Core
GitCommitId                    7.4.1
OS                             Microsoft Windows 10.0.17763
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

jborean93 commented 2 weeks ago

When you use Invoke-Command with -ComputerName it connects to a registered PSSessionConfiguration under a specific name. The default configuration is Microsoft.PowerShell which is the PSSession registered for Windows PowerShell 5.1. You can see this by running Invoke-Command -ComputerName foo { $PSVersionTable.PSVersion } and see it will be for 5.1 and not 7.4.1.

While Set-Service on Windows PowerShell 5.1 did have a -StartupType parameter the AutomaticDelayedStart was added in version in PowerShell 7.x. To use this startup type you'll either have to

The PowerShell.7 configuration will only exist on the remote host if you installed PowerShell 7 and registered the session configuration with Enable-PSRemoting on PowerShell 7. You can see what configurations are available by running as admin Get-PSSessionConfiguration in PowerShell 7 on the target host.

microsoft-github-policy-service[bot] commented 2 weeks ago

This issue has been marked as answered and has not had any activity for 1 day. It has been closed for housekeeping purposes.

microsoft-github-policy-service[bot] commented 2 weeks ago

📣 Hey @Ryssk, how did we do? We would love to hear your feedback with the link below! 🗣️

🔗 https://aka.ms/PSRepoFeedback

Microsoft Forms