共用方式為


建構 InParameters 物件

當使用ExecMethod類型的呼叫時,InParameters物件包含呼叫提供者方法的參數清單。 SWbemObject.ExecMethod_SWbemObject.ExecMethodAsync_SWbemServices.ExecMethodSWbemServices.ExecMethodAsync方法都需要InParameters物件。

下列程式描述如何建構 InParameters 物件。

建構 objwbemInParams 參數

  1. 連線到 WMI。

  2. 取得定義您要執行之方法的 WMI 類別定義。

  3. 取得您想要執行的 WMI 類別方法特定的 InParameters 物件。

    Set objInParam = objShare.Methods_("Create"). _
        inParameters.SpawnInstance_()
    
  4. 將 實例的屬性設定為任何適當的值。 請務必將值提供給 WMI 類別中索引鍵屬性,其中包含您要執行的方法。

    例如,如果您想要將名為 myinputparam 的輸入參數設定為名為 「INST」 之 InParameters 實例中的值 「abc」,程式碼看起來會像這樣。

    INST.Properties_.Add ("myinputparam").Value = "abc".
    
  5. 執行 方法,並取得您正在執行之方法的傳回狀態。

下列程式碼範例說明如何設定 InParameters 物件,以建立代表共用的新 WMI 物件。 如需 OutParameters 物件的詳細資訊,請參閱 剖析 OutParameters 物件。 如果 「C:/Share」 位置有名為 「Share」 的資料夾,則此範例會傳回成功傳回值 (0) 。 此範例可讓此資料夾與其他使用者共用。

' Connect to WMI.
Set objServices = GetObject("winmgmts:root\cimv2")

' Obtain the definition of the WMI class that defines
' the method you want to execute.
Set objShare = objServices.Get("Win32_Share")

' Obtain an InParameters object specific
' to the WMI class method you want to execute.
Set objInParam = objShare.Methods_("Create"). _
    inParameters.SpawnInstance_()

' Set the properties of the instance to whatever
' values are appropriate.
objInParam.Properties_.Item("Access") = objSecDescriptor
objInParam.Properties_.Item("Description") = _
    "New share created by WMI script"
objInParam.Properties_.Item("Name") = "share"
objInParam.Properties_.Item("Path") = "C:\share"
objInParam.Properties_.Item("Type") = 0
'optional - default is 'max allowed'
objInParam.Properties_.Item("MaximumAllowed") = 100
'optional - default is no password
objInParam.Properties_.Item("Password") = "Password"

' Execute the method and obtain the return status. 
' The OutParameters object in objOutParams
' is created by the provider. 
Set objOutParams = objShare.ExecMethod_("Create", objInParam)    
wscript.echo objOutParams.ReturnValue