Get-WindowsOptionalFeature 输出中的 RestartRequired 和 RestartNeeded 有什么区别?

What is the difference between RestartRequired and RestartNeeded in Get-WindowsOptionalFeature output?

我运行以下:

Get-WindowsOptionalFeature -Online -FeatureName TelnetClient | ConvertTo-Json -Depth 100

它产生以下输出:

{
    "DisplayName":  "Telnet Client",
    "Description":  "Allows you to connect to other computers remotely.",
    "RestartRequired":  1,
    "CustomProperties":  [
                             {
                                 "Name":  "Description",
                                 "Value":  "Telnet Client uses the Telnet protocol to connect to a remote Telnet server and run applications on that server.",
                                 "Path":  "ServerComponent"
                             },
                             {
                                 "Name":  "DisplayName",
                                 "Value":  "Telnet Client",
                                 "Path":  "ServerComponent"
                             },
                             {
                                 "Name":  "Id",
                                 "Value":  "44",
                                 "Path":  "ServerComponent"
                             },
                             {
                                 "Name":  "Type",
                                 "Value":  "Feature",
                                 "Path":  "ServerComponent"
                             },
                             {
                                 "Name":  "UniqueName",
                                 "Value":  "Telnet-Client",
                                 "Path":  "ServerComponent"
                             },
                             {
                                 "Name":  "Major",
                                 "Value":  "10",
                                 "Path":  "ServerComponent\Version"
                             },
                             {
                                 "Name":  "Minor",
                                 "Value":  "0",
                                 "Path":  "ServerComponent\Version"
                             },
                             {
                                 "Name":  "Name",
                                 "Value":  "TelnetClient",
                                 "Path":  "ServerComponent\Deploys\Update"
                             }
                         ],
    "FeatureName":  "TelnetClient",
    "State":  0,
    "Path":  null,
    "Online":  true,
    "WinPath":  null,
    "SysDrivePath":  null,
    "RestartNeeded":  false,
    "LogPath":  "C:\Windows\Logs\DISM\dism.log",
    "ScratchDirectory":  null,
    "LogLevel":  2
}

我的问题特别是关于“RestartNeeded”和“RestartRequired”之间的区别是什么?他们似乎都在谈论同一个话题。它们到底有什么区别?

"RestartRequired" 实际上是一个枚举 (Microsoft.Dism.Commands.RestartType),其值为:

Name Value
No 0
Possible 1
Required 2

(遗憾的是,我仍在使用 PowerShell 5.1,它缺少 ConvertTo-Json-EnumsAsStrings 选项,后者是在 PowerShell 6 和 7 中添加的。)

简答:TelnetClient 这样的功能 return a [Microsoft.Dism.Commands.AdvancedFeatureObject]:

  • RestartRequired表示安装或启用该功能后是否需要重启
  • RestartNeeded 用于显示该功能当前是否正在等待重新启动。

长版本: AdvancedFeatureObject 类型实际上从未设置过此值。相反,属性 仅由 Microsoft.Dism.Commands.ImageObject 使用,它是 return 从 Enable/Disable-WindowsOptionalFeature 编辑而来的。例如:

# I already have it installed:
Get-WindowsOptionalFeature -Online -FeatureName 'TelnetClient' | fl FeatureName,State,Restart*

FeatureName      : TelnetClient
State            : Enabled
RestartNeeded    : False
RestartRequired  : Possible

# I remove it, and save the output:
$result = Get-WindowsOptionalFeature -Online -FeatureName 'TelnetClient'| 
    Disable-WindowsOptionalFeature

# Returns an ImageObject
($result|gm).TypeName
Microsoft.Dism.Commands.ImageObject

# RestartNeeded is set here
$result

Path          :
Online        : True
RestartNeeded : True

# It does not get set on the main object though (only State=Disabled):

FeatureName     : TelnetClient
State           : Disabled
RestartRequired : Possible
RestartNeeded   : False

我认为 RestartNeeded 属性 由于某种类型继承而仅出现在 AdvancedFeatureObject 上,实际上并不是有用的 属性。