Windows 为cmdlet提供的每个信息创建变量
所以我想这是一个新手问题,但是谷歌不能再帮我了,因为我缺少描述我想做什么的术语 我想做的只是询问.NET Framework可选Windows功能的状态,如果其中一个未安装,请这样做 我的方法是Windows 为cmdlet提供的每个信息创建变量,windows,powershell,automation,Windows,Powershell,Automation,所以我想这是一个新手问题,但是谷歌不能再帮我了,因为我缺少描述我想做什么的术语 我想做的只是询问.NET Framework可选Windows功能的状态,如果其中一个未安装,请这样做 我的方法是 Get-WindowsOptionalFeature -Online | Where-Object {$_.FeatureName -clike "Net*"} 预计产出为: FeatureName : NetFx3 State : DisabledWithPayloa
Get-WindowsOptionalFeature -Online | Where-Object {$_.FeatureName -clike "Net*"}
预计产出为:
FeatureName : NetFx3
State : DisabledWithPayloadRemoved
FeatureName : NetFx4-AdvSrvs
State : Disabled
FeatureName : NetFx4Extended-ASPNET45
State : Disabled
现在我想将其解析为一个Foreach方法,以便安装State=Disabled*的每个特性
我尝试为每个变量创建一个变量,其中变量的名称也是给定的值,例如:
$NetFx3 = Netfx3
正如我所说的,这是针对目前被禁用的每个功能,而不是针对任何其他功能
这样我就可以使用它们,但应该有一种更简单的方法,我看不到…如果将当前命令的输出导入每个对象的
,PowerShell将自动为您创建一个变量。
尝试一下,您会发现有一个名为$psItem
的对象,它包含列表中某个项目的所有属性,并且会自动移动到列表中的下一个项目
Get-WindowsOptionalFeature -Online | Where-Object {$_.FeatureName -clike "Net*"}|
ForEach-Object{
Write-host "The status of the feature called $($PSItem.FeatureName) is $($PSItem.State)"
}
边栏,有趣的变量
因为我觉得您可能有兴趣了解一些有趣的技巧,所以我鼓励您使用输出,看看属性是什么,这非常简单
就这样把-OutVariable myVar
打到末尾。下面是一些有趣的命令,让你开始
Get-WindowsOptionalFeature -Online | Where-Object {$_.FeatureName -clike "Net*"} -OutVariable myVar
#pick the first item in the list and see some properties
>$myVar[0]
FeatureName : NetFx3
State : Enabled
#see ALL of its properties
>$myVar[0] | Format-List * -Force
FeatureName : NetFx3
State : Enabled
Path :
Online : True
WinPath :
SysDrivePath :
RestartNeeded : False
LogPath : C:\WINDOWS\Logs\DISM\dism.log
ScratchDirectory :
LogLevel : WarningsInfo
#access one or two of them
>$myvar[0] | select FeatureName, Online,LogPath
FeatureName Online LogPath
----------- ------ -------
NetFx3 True C:\WINDOWS\Logs\DISM\dism.log
#get those same properties for every item in the list
> $myvar | select FeatureName, Online,LogPath
FeatureName Online LogPath
----------- ------ -------
NetFx3 True C:\WINDOWS\Logs\DISM\dism.log
NetFx4-AdvSrvs True C:\WINDOWS\Logs\DISM\dism.log
NetFx4Extended-ASPNET45 True C:\WINDOWS\Logs\DISM\dism.log
#Save a spreadsheet with this info
> $myvar | select FeatureName, Online,LogPath | Export-Csv C:\pathTo\This.csfv -NoTypeInfo
回到你的问题上来
但是,如果您想自动安装缺少的任何角色,您可以这样做
$missingDotNet = Get-WindowsOptionalFeature -Online | Where-Object {$_.FeatureName -clike "Net*" -and $_.State -ne 'Enabled'}
if ($missingDotNet.Count -eq 0){
Write-Host "All Dotnet features installed (or not running as Admin)"
}
else{
Enable-WindowsOptionalFeature $missingDotNet
}
最后,我遇到了一个错误,因为变量发出的似乎是“$\u0.FeatureName=NetFx3”
但是有了下面的代码,我就可以让它运行了
#####Check on all .Net Features
$missingDotNet = Get-WindowsOptionalFeature -Online | Where-Object {$_.FeatureName -clike "Net*" -and $_.State -ne 'Enabled'} | Select -ExpandProperty FeatureName
#####If no .Net Feature is missing Script just prints "All .NET Features are installed or not running as Admin (no guarantee)"
if ($missingDotNet.Count -eq 0){
Write-Host "All .NET Features installed or not running as Admin"
}
#####If there are .Net Features that are missing this line will install them
else
{
Enable-WindowsOptionalFeature -Online -FeatureName ($missingDotNet)
}
因此,现在-由于该脚本旨在完全安装SAP GUI install-我可以爬回我的洞穴并完成其余的工作。您可以将您的条件更改为Where对象{$\u.FeatureName-如“Net*”-和$\u.State-ne“Enabled”}
以仅获取未启用的功能。如果您通过管道将此信息传输到启用Windows OptionalFeature,那么您就完成了。嘿,谢谢您的帮助!还感谢您提供关于“变量乐趣”的更多信息:D