使用 PowerShell 指令碼搭配 WMI 橋接器提供者
本文涵蓋如何使用PowerShell Cmdlet腳本來設定每位使用者和每部裝置的原則設定,以及如何透過 WMI網橋提供者叫用方法。
設定每部裝置的原則設定
本節提供PowerShell Cmdlet 範例腳本,可透過 WMI網橋提供者設定每個裝置的設定。 如果類別支援裝置設定,則必須針對 InPartition (“local-system”) 定義類別層級限定符。
針對所有裝置設定,WMI 網橋客戶端必須在本機系統用戶下執行。 若要這樣做,請從 https://technet.microsoft.com/sysinternals/bb897553.aspx 下載 psexec 工具,並 psexec.exe -i -s cmd.exe
從提升許可權的系統管理員命令提示字元執行。
本節中的腳本範例會使用 類別MDM_Policy_Config01_WiFi02:
[dynamic, provider("DMWmiBridgeProv"), InPartition("local-system")]
class MDM_Policy_Config01_WiFi02
{
string InstanceID;
string ParentID;
sint32 AllowInternetSharing;
sint32 AllowAutoConnectToWiFiSenseHotspots;
sint32 WLANScanMode;
};
下列腳本描述如何建立、列舉、查詢、修改和刪除實例。
$namespaceName = "root\cimv2\mdm\dmmap"
$className = "MDM_Policy_Config01_WiFi02"
# Create a new instance for MDM_Policy_Config01_WiFi02
New-CimInstance -Namespace $namespaceName -ClassName $className -Property @{ParentID="./Vendor/MSFT/Policy/Config";InstanceID="WiFi";AllowInternetSharing=1;AllowAutoConnectToWiFiSenseHotspots=0;WLANScanMode=100}
# Enumerate all instances available for MDM_Policy_Config01_WiFi02
Get-CimInstance -Namespace $namespaceName -ClassName $className
# Query instances with matching properties
Get-CimInstance -Namespace $namespaceName -ClassName $className -Filter "ParentID='./Vendor/MSFT/Policy/Config' and InstanceID='WiFi'"
# Modify existing instance
$obj = Get-CimInstance -Namespace $namespaceName -ClassName $className -Filter "ParentID='./Vendor/MSFT/Policy/Config' and InstanceID='WiFi'"
$obj.WLANScanMode=500
Set-CimInstance -CimInstance $obj
# Delete existing instance
try
{
$obj = Get-CimInstance -Namespace $namespaceName -ClassName $className -Filter "ParentID='./Vendor/MSFT/Policy/Config' and InstanceID='WiFi'"
Remove-CimInstance -CimInstance $obj
}
catch [Exception]
{
write-host $_ | out-string
}
設定每位用戶設定
本節提供PowerShell Cmdlet範例腳本,可透過WMI網橋設定每位用戶設定。 如果類別支援用戶設定,則必須針對 InPartition (“local-user”) 定義類別層級限定符。
本節中的腳本範例會使用 類別MDM_Policy_User_Config01_Authentication02:
[dynamic, provider("DMWmiBridgeProv"), InPartition("local-user")]
class MDM_Policy_User_Config01_Authentication02
{
string InstanceID;
string ParentID;
sint32 AllowEAPCertSSO;
};
注意
如果目前登入的用戶嘗試自行存取或修改用戶設定,則使用上一節中的個別裝置設定腳本會更容易。 所有 PowerShell Cmdlet 都必須在提升許可權的系統管理員命令提示字元下執行。
如果存取或修改不同用戶的設定,則PowerShell腳本會更複雜,因為WMI網橋預期會在MI自定義內容中設定使用者SID,這在原生PowerShell Cmdlet 中不受支援。
注意
所有命令都必須在本機系統下執行。
Windows 命令 wmic useraccount get name, sid
可用來取得使用者 SID。 下列文稿範例假設使用者 SID 是 S-1-5-21-4017247134-4237859428-3008104844-1001
。
$namespaceName = "root\cimv2\mdm\dmmap"
$className = "MDM_Policy_User_Config01_Authentication02"
# Configure CIM operation options with target user info
$options = New-Object Microsoft.Management.Infrastructure.Options.CimOperationOptions
$options.SetCustomOption("PolicyPlatformContext_PrincipalContext_Type", "PolicyPlatform_UserContext", $false)
$options.SetCustomOption("PolicyPlatformContext_PrincipalContext_Id", "S-1-5-21-4017247134-4237859428-3008104844-1001", $false)
# Construct session used for all operations
$session = New-CimSession
##########################################################################
# Create a new instance for MDM_Policy_User_Config01_Authentication02
##########################################################################
$newInstance = New-Object Microsoft.Management.Infrastructure.CimInstance $className, $namespaceName
$property = [Microsoft.Management.Infrastructure.CimProperty]::Create("ParentID", './Vendor/MSFT/Policy/Config', "string", "Key")
$newInstance.CimInstanceProperties.Add($property)
$property = [Microsoft.Management.Infrastructure.CimProperty]::Create("InstanceID", 'Authentication', "String", "Key")
$newInstance.CimInstanceProperties.Add($property)
$property = [Microsoft.Management.Infrastructure.CimProperty]::Create("AllowEAPCertSSO", 1, "Sint32", "Property")
$newInstance.CimInstanceProperties.Add($property)
try
{
$session.CreateInstance($namespaceName, $newInstance, $options)
}
catch [Exception]
{
write-host $_ | out-string
}
##########################################################################
# Enumerate all instances for MDM_Policy_User_Config01_Authentication02
##########################################################################
$session.EnumerateInstances($namespaceName, $className, $options)
##########################################################################
# Query instance for MDM_Policy_User_Config01_Authentication02
# with matching properties
##########################################################################
$getInstance = New-Object Microsoft.Management.Infrastructure.CimInstance $className, $namespaceName
$property = [Microsoft.Management.Infrastructure.CimProperty]::Create("ParentID", './Vendor/MSFT/Policy/Config', "string", "Key")
$getInstance.CimInstanceProperties.Add($property)
$property = [Microsoft.Management.Infrastructure.CimProperty]::Create("InstanceID", 'Authentication', "String", "Key")
$getInstance.CimInstanceProperties.Add($property)
try
{
$session.GetInstance($namespaceName, $getInstance, $options)
}
catch [Exception]
{
write-host $_ | out-string
}
##########################################################################
# Modify existing instance for MDM_Policy_User_Config01_Authentication02
##########################################################################
$getInstance = New-Object Microsoft.Management.Infrastructure.CimInstance $className, $namespaceName
$property = [Microsoft.Management.Infrastructure.CimProperty]::Create("ParentID", './Vendor/MSFT/Policy/Config', "string", "Key")
$getInstance.CimInstanceProperties.Add($property)
$property = [Microsoft.Management.Infrastructure.CimProperty]::Create("InstanceID", 'Authentication', "String", "Key")
$getInstance.CimInstanceProperties.Add($property)
try
{
$updateInstance = $session.GetInstance($namespaceName, $getInstance, $options)[0]
$updateInstance.AllowEAPCertSSO = 0
$session.ModifyInstance($namespaceName, $updateInstance, $options)
}
catch [Exception]
{
write-host $_ | out-string
}
##########################################################################
# Delete existing instance for MDM_Policy_User_Config01_Authentication02
##########################################################################
$getInstance = New-Object Microsoft.Management.Infrastructure.CimInstance $className, $namespaceName
$property = [Microsoft.Management.Infrastructure.CimProperty]::Create("ParentID", './Vendor/MSFT/Policy/Config', "string", "Key")
$getInstance.CimInstanceProperties.Add($property)
$property = [Microsoft.Management.Infrastructure.CimProperty]::Create("InstanceID", 'Authentication', "String", "Key")
$getInstance.CimInstanceProperties.Add($property)
try
{
$deleteInstance = $session.GetInstance($namespaceName, $getInstance, $options)[0]
$session.DeleteInstance($namespaceName, $deleteInstance, $options)
}
catch [Exception]
{
write-host $_ | out-string
}
叫用方法
本節提供用來叫用 WMI Bridge 物件方法的 PowerShell Cmdlet 範例腳本。 下列腳本必須在本機系統用戶下執行。 若要這樣做,請從 https://technet.microsoft.com/sysinternals/bb897553.aspx 下載 psexec 工具,並 psexec.exe -i -s cmd.exe
從提升許可權的系統管理員命令提示字元執行。
本節中的腳本範例使用 MDM_WindowsLicensing 類別的 UpgradeEditionWithProductKeyMethod 方法。
$namespaceName = "root\cimv2\mdm\dmmap"
$className = "MDM_WindowsLicensing"
$methodName = "UpgradeEditionWithProductKeyMethod"
$fakeProductKey = "7f1a3659-3fa7-4c70-93ce-0d354e8e158e"
$session = New-CimSession
$params = New-Object Microsoft.Management.Infrastructure.CimMethodParametersCollection
$param = [Microsoft.Management.Infrastructure.CimMethodParameter]::Create("param", $fakeProductKey, "String", "In")
$params.Add($param)
try
{
$instance = Get-CimInstance -Namespace $namespaceName -ClassName $className -Filter "ParentID='./Vendor/MSFT' and InstanceID='WindowsLicensing'"
$session.InvokeMethod($namespaceName, $instance, $methodName, $params)
}
catch [Exception]
{
write-host $_ | out-string
}