Discussion:
Using New-Object With ManagementClass
(too old to reply)
urkec
2008-03-06 17:43:01 UTC
Permalink
How can I convert this C# code to PowerShell:

static void Main()
{
PutOptions options = new PutOptions();
options.Type = PutType.CreateOnly;

ManagementClass newClass =
new ManagementClass(
"root/default",
String.Empty,
null);

newClass["__Class"] = "MyClass";
newClass.Put(options);

}

The code creates an empty WMI class , names it 'MyClass' and saves it in
root\default namespace. This is what I have tried:

$options = New-Object System.Management.PutOptions;
$options.Type = [System.Management.PutType]::CreateOnly;
$newclass = New-Object System.Management.ManagementClass ("root\default",
[String]::Empty, $null);
$newclass["__CLASS"] = "MyClass";
$newclass.Put($options);

but I get this error:

Exception calling "Put" with "1" argument(s): "You cannot call a method on a
null-valued expression."
At line 5, position 14
$newclass.Put($options);

Thanks.
--
urkec
Oisin (x0n) Grehan [MVP]
2008-03-06 18:02:20 UTC
Permalink
Post by urkec
static void Main()
{
   PutOptions options = new PutOptions();
   options.Type = PutType.CreateOnly;
   ManagementClass newClass =
      new ManagementClass(
      "root/default",
      String.Empty,
      null);
   newClass["__Class"] = "MyClass";
   newClass.Put(options);
}
The code creates an empty WMI class , names it 'MyClass' and saves it in
$options = New-Object System.Management.PutOptions;
$options.Type = [System.Management.PutType]::CreateOnly;
$newclass = New-Object System.Management.ManagementClass ("root\default",
[String]::Empty, $null);
$newclass["__CLASS"] = "MyClass";
$newclass.Put($options);
Exception calling "Put" with "1" argument(s): "You cannot call a method on a
null-valued expression."
At line 5, position 14
$newclass.Put($options);
Thanks.
--
urkec
Your script succeeded for me on XP SP2, running as Administrator.
What's your setup?

PS C:\> $newclass.put($options)

Path : \\.\root\default:MyClass
RelativePath : MyClass
Server : .
NamespacePath : root\default
ClassName : MyClass
IsClass : True
IsInstance : False
IsSingleton : False

- Oisin
urkec
2008-03-06 18:17:01 UTC
Permalink
Post by Oisin (x0n) Grehan [MVP]
Post by urkec
static void Main()
{
PutOptions options = new PutOptions();
options.Type = PutType.CreateOnly;
ManagementClass newClass =
new ManagementClass(
"root/default",
String.Empty,
null);
newClass["__Class"] = "MyClass";
newClass.Put(options);
}
The code creates an empty WMI class , names it 'MyClass' and saves it in
$options = New-Object System.Management.PutOptions;
$options.Type = [System.Management.PutType]::CreateOnly;
$newclass = New-Object System.Management.ManagementClass ("root\default",
[String]::Empty, $null);
$newclass["__CLASS"] = "MyClass";
$newclass.Put($options);
Exception calling "Put" with "1" argument(s): "You cannot call a method on a
null-valued expression."
At line 5, position 14
$newclass.Put($options);
Thanks.
--
urkec
Your script succeeded for me on XP SP2, running as Administrator.
What's your setup?
PS C:\> $newclass.put($options)
Path : \\.\root\default:MyClass
RelativePath : MyClass
Server : .
NamespacePath : root\default
ClassName : MyClass
IsClass : True
IsInstance : False
IsSingleton : False
- Oisin
The same, Windows XP SP2, administrator account. C# code works with the same
account. I am still using PowerShell 1.0, can that be the reason?

Thank you.
--
urkec
Trevor Barry
2008-03-06 18:05:54 UTC
Permalink
Post by urkec
static void Main()
{
PutOptions options = new PutOptions();
options.Type = PutType.CreateOnly;
ManagementClass newClass =
new ManagementClass(
"root/default",
String.Empty,
null);
newClass["__Class"] = "MyClass";
newClass.Put(options);
}
The code creates an empty WMI class , names it 'MyClass' and saves it in
$options = New-Object System.Management.PutOptions;
$options.Type = [System.Management.PutType]::CreateOnly;
$newclass = New-Object System.Management.ManagementClass ("root\default",
[String]::Empty, $null);
$newclass["__CLASS"] = "MyClass";
$newclass.Put($options);
Exception calling "Put" with "1" argument(s): "You cannot call a method on a
null-valued expression."
At line 5, position 14
$newclass.Put($options);
Thanks.
--
urkec
Hi urkec,

this is a known bug in PowerShell. Just rerun the Put a few times and it
will work. According to the support engineer I spoke to this will be fixed
in PowerShell 2.0.

Also you'll need to be running PowerShell as an administrator to get the
script to work.

Trevor
urkec
2008-03-06 18:31:01 UTC
Permalink
Post by Trevor Barry
Post by urkec
static void Main()
{
PutOptions options = new PutOptions();
options.Type = PutType.CreateOnly;
ManagementClass newClass =
new ManagementClass(
"root/default",
String.Empty,
null);
newClass["__Class"] = "MyClass";
newClass.Put(options);
}
The code creates an empty WMI class , names it 'MyClass' and saves it in
$options = New-Object System.Management.PutOptions;
$options.Type = [System.Management.PutType]::CreateOnly;
$newclass = New-Object System.Management.ManagementClass ("root\default",
[String]::Empty, $null);
$newclass["__CLASS"] = "MyClass";
$newclass.Put($options);
Exception calling "Put" with "1" argument(s): "You cannot call a method on a
null-valued expression."
At line 5, position 14
$newclass.Put($options);
Thanks.
--
urkec
Hi urkec,
this is a known bug in PowerShell. Just rerun the Put a few times and it
will work. According to the support engineer I spoke to this will be fixed
in PowerShell 2.0.
Also you'll need to be running PowerShell as an administrator to get the
script to work.
Trevor
Thanks to both Oisin and Trevor.At first I thaught my code was wrong, so I
kept changing it, never getting it to work. When Oisin confirmed that it does
work, I suspected this might be something with my version of PowerShell,
which Trevor confirmed. Thank you.
--
urkec
Loading...