ManagementObject.Put() not persisting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Clegg

    #1

    ManagementObject.Put() not persisting

    I initially posted this to .Framework.WMI a week ago but am yet to have
    a response...

    I am having trouble changing properties of a Windows Service
    application using the ManagementObjec t class (in particular the
    StartMode property). I can change the property and calling
    ManagementObjec t.Put doesn't throw any exceptions, but the setting
    isn't changed. If I query the property after setting and putting, it
    appears to be updated, but if you create a new ManagementObjec t
    instance to retrieve the details again, the old property is returned.
    What am I missing here? I have pasted some code below to demonstrate.

    //***BEGIN CODE***
    using System;
    using System.Manageme nt;

    namespace MOTest {
    class MOTest {
    static void Main(string[] args) {
    string objectPath = "Win32_Service. Name='ebetCJMSv c'";
    ManagementObjec t mo = new ManagementObjec t(objectPath);
    if (mo != null) {
    ShowStartMode(m o, "Before Put:");
    if (mo["StartMode"].ToString() == "Manual")
    mo["StartMode"] = "Auto";
    else
    mo["StartMode"] = "Manual";
    ManagementPath mp = mo.Put();
    ShowStartMode(m o, "After Put:");
    ShowStartMode(o bjectPath, "After Reload:");

    Console.WriteLi ne("Press Enter to continue...");
    Console.ReadLin e();
    }
    }

    static private void ShowStartMode(M anagementObject mo, string prefix)
    {
    if (mo != null) {
    Console.WriteLi ne("{0} {1}", prefix, mo["StartMode"].ToString());
    }
    }

    static private void ShowStartMode(s tring objectPath, string prefix) {
    ManagementObjec t mo = new ManagementObjec t(objectPath);
    ShowStartMode(m o, prefix);
    }
    }
    }
    //***END CODE***
  • Sam

    #2
    ManagementObjec t.Put() not persisting

    I'm not 100% sure but you might have to do a Get() call
    at the start to bind to the management object.

    HTH

    Sam
    [color=blue]
    >-----Original Message-----
    >I initially posted this to .Framework.WMI a week ago but[/color]
    am yet to have[color=blue]
    >a response...
    >
    >I am having trouble changing properties of a Windows[/color]
    Service[color=blue]
    >application using the ManagementObjec t class (in[/color]
    particular the[color=blue]
    >StartMode property). I can change the property and[/color]
    calling[color=blue]
    >ManagementObje ct.Put doesn't throw any exceptions, but[/color]
    the setting[color=blue]
    >isn't changed. If I query the property after setting and[/color]
    putting, it[color=blue]
    >appears to be updated, but if you create a new[/color]
    ManagementObjec t[color=blue]
    >instance to retrieve the details again, the old property[/color]
    is returned.[color=blue]
    >What am I missing here? I have pasted some code below to[/color]
    demonstrate.[color=blue]
    >
    >//***BEGIN CODE***
    >using System;
    >using System.Manageme nt;
    >
    >namespace MOTest {
    > class MOTest {
    > static void Main(string[] args) {
    > string objectPath[/color]
    = "Win32_Service. Name='ebetCJMSv c'";[color=blue]
    > ManagementObjec t mo = new[/color]
    ManagementObjec t(objectPath);[color=blue]
    > if (mo != null) {
    > ShowStartMode(m o, "Before[/color]
    Put:");[color=blue]
    > if (mo[/color]
    ["StartMode"].ToString() == "Manual")[color=blue]
    > mo["StartMode"][/color]
    = "Auto";[color=blue]
    > else
    > mo["StartMode"][/color]
    = "Manual";[color=blue]
    > ManagementPath mp = mo.Put[/color]
    ();[color=blue]
    > ShowStartMode(m o, "After[/color]
    Put:");[color=blue]
    > ShowStartMode[/color]
    (objectPath, "After Reload:");[color=blue]
    >
    > Console.WriteLi ne("Press[/color]
    Enter to continue...");[color=blue]
    > Console.ReadLin e();
    > }
    > }
    >
    > static private void ShowStartMode[/color]
    (ManagementObje ct mo, string prefix)[color=blue]
    >{
    > if (mo != null) {
    > Console.WriteLi ne("{0}[/color]
    {1}", prefix, mo["StartMode"].ToString());[color=blue]
    > }
    > }
    >
    > static private void ShowStartMode(s tring[/color]
    objectPath, string prefix) {[color=blue]
    > ManagementObjec t mo = new[/color]
    ManagementObjec t(objectPath);[color=blue]
    > ShowStartMode(m o, prefix);
    > }
    > }
    >}
    >//***END CODE***
    >.
    >[/color]

    Comment

    • Sam

      #3
      ManagementObjec t.Put() not persisting

      Ignore my last post I was talking nonsense.

      StartMode is a read only property, so you need to invoke
      the ChangeStartMode method as in the following line of
      code:

      mo.InvokeMethod ("ChangeStartMo de", new object[]
      { "Automatic" });

      Note that a) you don't need to do a Put(), b) You'll need
      to do a Get() to update the current object and c) It
      appears to use "Automatic" for the method call rather
      than "Auto" which is the property value.

      The following is the if statement from you're example
      changed so it works...

      if (mo["StartMode"].ToString() == "Manual")
      mo.InvokeMethod ("ChangeStartMo de", new object[]
      { "Automatic" });
      else
      mo.InvokeMethod ("ChangeStartMo de", new object[]
      { "Manual" });
      mo.Get();

      HTH

      Sam[color=blue]
      >-----Original Message-----
      >I'm not 100% sure but you might have to do a Get() call
      >at the start to bind to the management object.
      >
      >HTH
      >
      >Sam
      >[color=green]
      >>-----Original Message-----
      >>I initially posted this to .Framework.WMI a week ago[/color][/color]
      but[color=blue]
      >am yet to have[color=green]
      >>a response...
      >>
      >>I am having trouble changing properties of a Windows[/color]
      >Service[color=green]
      >>application using the ManagementObjec t class (in[/color]
      >particular the[color=green]
      >>StartMode property). I can change the property and[/color]
      >calling[color=green]
      >>ManagementObj ect.Put doesn't throw any exceptions, but[/color]
      >the setting[color=green]
      >>isn't changed. If I query the property after setting[/color][/color]
      and[color=blue]
      >putting, it[color=green]
      >>appears to be updated, but if you create a new[/color]
      >ManagementObje ct[color=green]
      >>instance to retrieve the details again, the old[/color][/color]
      property[color=blue]
      >is returned.[color=green]
      >>What am I missing here? I have pasted some code below[/color][/color]
      to[color=blue]
      >demonstrate.[color=green]
      >>
      >>//***BEGIN CODE***
      >>using System;
      >>using System.Manageme nt;
      >>
      >>namespace MOTest {
      >> class MOTest {
      >> static void Main(string[] args) {
      >> string objectPath[/color]
      >= "Win32_Service. Name='ebetCJMSv c'";[color=green]
      >> ManagementObjec t mo = new[/color]
      >ManagementObje ct(objectPath);[color=green]
      >> if (mo != null) {
      >> ShowStartMode(m o, "Before[/color]
      >Put:");[color=green]
      >> if (mo[/color]
      >["StartMode"].ToString() == "Manual")[color=green]
      >> mo["StartMode"][/color]
      >= "Auto";[color=green]
      >> else
      >> mo["StartMode"][/color]
      >= "Manual";[color=green]
      >> ManagementPath mp = mo.Put[/color]
      >();[color=green]
      >> ShowStartMode(m o, "After[/color]
      >Put:");[color=green]
      >> ShowStartMode[/color]
      >(objectPath, "After Reload:");[color=green]
      >>
      >> Console.WriteLi ne("Press[/color]
      >Enter to continue...");[color=green]
      >> Console.ReadLin e();
      >> }
      >> }
      >>
      >> static private void ShowStartMode[/color]
      >(ManagementObj ect mo, string prefix)[color=green]
      >>{
      >> if (mo != null) {
      >> Console.WriteLi ne("{0}[/color]
      >{1}", prefix, mo["StartMode"].ToString());[color=green]
      >> }
      >> }
      >>
      >> static private void ShowStartMode(s tring[/color]
      >objectPath, string prefix) {[color=green]
      >> ManagementObjec t mo = new[/color]
      >ManagementObje ct(objectPath);[color=green]
      >> ShowStartMode(m o, prefix);
      >> }
      >> }
      >>}
      >>//***END CODE***
      >>.
      >>[/color]
      >.
      >[/color]

      Comment

      • David Clegg

        #4
        Re: ManagementObjec t.Put() not persisting

        Sam wrote:
        [color=blue]
        > Ignore my last post I was talking nonsense.[/color]

        Done :-)
        [color=blue]
        > StartMode is a read only property, so you need to invoke
        > the ChangeStartMode method as in the following line of
        > code:
        >
        > mo.InvokeMethod ("ChangeStartMo de", new object[]
        > { "Automatic" });
        >
        > Note that a) you don't need to do a Put(), b) You'll need
        > to do a Get() to update the current object and c) It
        > appears to use "Automatic" for the method call rather
        > than "Auto" which is the property value.[/color]

        Thanks Sam, I'll give that a go. BTW, is there a documented list of
        which properties are read only, and need to be set that way? I would
        have thought it would have been more helpful if the FCL had thrown an
        exception if I attempted to set a read only property.

        Comment

        • Sam

          #5
          Re: ManagementObjec t.Put() not persisting

          The Platform SDK contains all the properties of each WMI
          class.

          Follow this link for the Win32_Service class:

          http://msdn.microsoft.com/library/en-
          us/wmisdk/wmi/win32_service.a sp

          HTH

          Sam
          [color=blue]
          >-----Original Message-----
          >Sam wrote:
          >[color=green]
          >> Ignore my last post I was talking nonsense.[/color]
          >
          >Done :-)
          >[color=green]
          >> StartMode is a read only property, so you need to[/color][/color]
          invoke[color=blue][color=green]
          >> the ChangeStartMode method as in the following line of
          >> code:
          >>
          >> mo.InvokeMethod ("ChangeStartMo de", new object[]
          >> { "Automatic" });
          >>
          >> Note that a) you don't need to do a Put(), b) You'll[/color][/color]
          need[color=blue][color=green]
          >> to do a Get() to update the current object and c) It
          >> appears to use "Automatic" for the method call rather
          >> than "Auto" which is the property value.[/color]
          >
          >Thanks Sam, I'll give that a go. BTW, is there a[/color]
          documented list of[color=blue]
          >which properties are read only, and need to be set that[/color]
          way? I would[color=blue]
          >have thought it would have been more helpful if the FCL[/color]
          had thrown an[color=blue]
          >exception if I attempted to set a read only property.
          >.
          >[/color]

          Comment

          • David Clegg

            #6
            Re: ManagementObjec t.Put() not persisting

            Thanks once again. Exactly what I was looking for.

            --
            Cheers,
            David Clegg
            dclegg_at_ebeto nline_dot_com.

            There's no place like 127.0.0.1

            Comment

            Working...