Answered by:
Typecast problem

Question
-
Hi!
I'm currently working with a PS script that is able to create some folders and views in OpsMgr.
It's working quite well, except when setting the parentfolder on a folder.
In C# I would have done like this
newFolder.ParentFolder = mp.GetFolder("Name of Parent Folder")
In PS I try something like this:
$NewFolder.ParentFolder = [Microsoft.EnterpriseManagement.Configuration.ManagementPackFolder] $mp.GetFolder('Name of Parent Folder')
If I try this, it gives me an exception like this:
Exception setting "ParentFolder": "Cannot convert "Name of Parent folder" to "Microsoft.EnterpriseManagement.Configuration.ManagementPackElementReference`1[Microsoft.EnterpriseManagement.Configuration.ManagementPackFolder]"."
Is this something PS specific or what?
Regards HenrikThursday, August 20, 2009 11:53 AM
Answers
-
I'm not a professional .NET programmer so I may actually say some inaccurate things here.
This .NET generic class is a pain. I came up against it several months ago, and it got the best of me. So, yes, this is something PowerShell specific.
I was told by a senior Microsoft OpsMgr engineer that it should be possible to deal with this generic class, but he was never available to help me (for free), and I eventually moved on.
I came across this while trying to translate this C# to PowerShell:
http://blogs.msdn.com/boris_yanushpolsky/archive/2008/01/17/creating-overrides-using-the-sdk.aspx
It might have come up another time also...
What I think it comes down to for you, is that PowerShell isn't accepting you put a simple string in GetFolder().
Very odd when it appears it is actually supposed to take a simple string:
http://msdn.microsoft.com/en-us/library/microsoft.enterprisemanagement.configuration.managementpack.getfolder.aspx- Marked as answer by .Anders BengtssonMicrosoft employee Monday, November 23, 2009 10:53 PM
Friday, August 21, 2009 1:55 AM
All replies
-
I'm not a professional .NET programmer so I may actually say some inaccurate things here.
This .NET generic class is a pain. I came up against it several months ago, and it got the best of me. So, yes, this is something PowerShell specific.
I was told by a senior Microsoft OpsMgr engineer that it should be possible to deal with this generic class, but he was never available to help me (for free), and I eventually moved on.
I came across this while trying to translate this C# to PowerShell:
http://blogs.msdn.com/boris_yanushpolsky/archive/2008/01/17/creating-overrides-using-the-sdk.aspx
It might have come up another time also...
What I think it comes down to for you, is that PowerShell isn't accepting you put a simple string in GetFolder().
Very odd when it appears it is actually supposed to take a simple string:
http://msdn.microsoft.com/en-us/library/microsoft.enterprisemanagement.configuration.managementpack.getfolder.aspx- Marked as answer by .Anders BengtssonMicrosoft employee Monday, November 23, 2009 10:53 PM
Friday, August 21, 2009 1:55 AM -
Thanks Marco, Nice to know that I'm not alone:-)
I really don't like to give up on this, but maybe it's best to get back to C# to save siome time.Friday, August 21, 2009 7:04 AM -
"Mark as Answer", no activity for a month. Feel free to re-open this
Anders Bengtsson | Microsoft MVP - Operations Manager | http://www.contoso.seMonday, November 23, 2009 10:53 PM