我有下面的工作代码,它用作SCCM中的检测子句,以检测服务器功能是否已安装为服务器功能脚本安装程序的一部分。
$role = Get-WmiObject -Class Win32_ServerFeature | Where-Object {$_.ID -eq 2}
if ($role) {
Write-Host "Installed"
}
else {
}
问题是,上面只有1个服务器特性,我如何使用一个数组来添加类中每个相关的服务器特性,以便检测子句是100%准确的?
本例中的相关角色包括:
Web-Server Web-ISAPI-Ext Web-Windows-Auth Web-Metabase Web-WMI RDC
在已经安装了它们的机器上,我可以提取以下信息以添加:
Get-WmiObject -Class win32_serverfeature | select Name, ID
Name ID
---- --
Web Server (IIS) 2
File Services 6
Windows Deployment Services 19
.NET Framework 3.0 Features 36
Windows Process Activation Service 41
Telnet Client 44
SNMP Services 59
Remote Server Administration Tools 67
Web Server 140
Common HTTP Features 141
Static Content 142
Default Document 143
Directory Browsing 144
HTTP Errors 145
Application Development 147
ISAPI Extensions 152
Health and Diagnostics 155
HTTP Logging 156
Request Monitor 158
Security 162
Windows Authentication 164
Request Filtering 169
Performance 171
Static Content Compression 172
Management Tools 174
IIS Management Console 175
IIS 6 Management Compatibility 178
IIS 6 Metabase Compatibility 179
Configuration APIs 217
.NET Environment 218
Process Model 219
.NET Framework 3.0 220
SNMP Service 224
SNMP WMI Provider 225
Deployment Server 251
Transport Server 252
File Server 255
Role Administration Tools 256
Windows Deployment Services Tools 264
Web Server (IIS) Tools 281
注意:这是为运行2008 SP2的遗留服务器设计的,因此,这意味着我有较旧的工具集可用(不能使用ServerFeaturecmd.exe- Install-WindowsFeature和Get-WindowsFeature)
谢谢
发布于 2018-11-13 00:00:58
如果你只是想减少代码,你可以这样写:
$featureIDs = @(2, 140, 141, 162, 164, 179)
$compliant = $true
foreach($ID in $featureIDs) {
$compliant = $compliant -and ((Get-WmiObject -Class Win32_ServerFeature | Where-Object {$_.ID -eq $ID}) -ne $null)
}
if ($compliant) {
Write-Host "Installed"
}
else {
}
然而,这确实做了很多WMI调用,而不仅仅是您需要的调用。我不确定是否可以用一些WQL语法做得更好(因为WQL非常有限),但如果我找到更好的方法,我会更新它。我现在主要发布这个相当粗糙的解决方案,因为我想我理解你想要实现的目标,希望能激发出一个更好的解决方案。
发布于 2018-11-12 23:14:22
这就是我所想的,但是,将对象放入一个数组而不是单个变量?这似乎不是最简单的方法。
$roleID2 = Get-WmiObject -Class Win32_ServerFeature | Where-Object {$_.ID -eq 2}
$roleID140 = Get-WmiObject -Class Win32_ServerFeature | Where-Object {$_.ID -eq 140}
$roleID141 = Get-WmiObject -Class Win32_ServerFeature | Where-Object {$_.ID -eq 141}
$roleID162 = Get-WmiObject -Class Win32_ServerFeature | Where-Object {$_.ID -eq 162}
$roleID164 = Get-WmiObject -Class Win32_ServerFeature | Where-Object {$_.ID -eq 164}
$roleID179 = Get-WmiObject -Class Win32_ServerFeature | Where-Object {$_.ID -eq 179}
If ($roleID2 -and $roleID140 -and $roleID141 -and $roleID162 -and $roleID164 $roleID179) {
Write-Host "Installed"
}
else {
}
https://stackoverflow.com/questions/53263639
复制相似问题