In a powershell cmdlet that I am writing in C#, I need to get the name of the script that has called me.
I have derived my cmdlet class from PSCmdlet; there is a ton of information attached to this class at runtime, but I don't see where I can get the information I am looking for.
Is it possible to get the script name? If so, where does it live?
The automatic variable $MyInvocation should contain the name of the script in the InvocationName property.
Thanks, mjolinor... put me on the right path.
MyInvocation.InvocationName gives you the name of the command that the cmdlet was invoked under, but the calling script name is right close by...
I found what I was looking for here (from within the PSCmdlet-derived class):
var callingScript = MyInvocation.ScriptName;
It contains the full path of the script that called the cmdlet.
Related
I'm using XNA and attempting to load an image using a string
image = Game.Content.Load<Texture2D>(playerCharacter.image);
the character class is abstract, the PlayerCharacter classes are all derived from it and the image variable is set to something like "PlayerSprites/Char1"
I get a null exception when running this code. The path is correct, but I don't know if a path is the correct way to do this.
Don't use the file path. Use the asset name. Click on the asset in the solution explorer. You'll see a bunch of properties pop up. Look at the one that says "Asset Name". That is literally the string you pass in to the loadcontent method. Hope I helped!
I have written a C# Tool where i can enter script parameters with an GUI which is generated based on the parmeter definitions of the script.
Now i want to have a dropdown list which offers me a dynamically generated set of values. The informations for this dropdown list should come from the parameter definition of the script.
(In my case i want to select an existing AD OU by Listing all Child-Objects of the Base OU.)
One way to get a list of valid parameters is to use "ValidateSet" for Parameter definition. There is abway to get te ValidateSet from the Script an build the dropdown list. But ValidateSet is a static deffinition and i have to update the script each time the list should be changed.
A good way for dynamic validation is "ValidateScript". The script command would be something like Test-Path. This would work for validation, but for my GUI i would not be able to generate a list of valid values.
Maby i can dynamically generate a custom enum type and use it as parameter type. A dropdown list for enum types is already implemented for GUI.
But i think i's not a good idea and may not work to generate a enum type dynamically.
So, any other ideas for a list of valid values which is dynamically built?
I tried doing that with an enum once, and it got problematic due to differences in the valid character sets between enum values and AD names.
If you've wanting to keep the GUI separate from the script, you might investigate using AST to extract the parameter validation code from the script, and then run it outside the script to build your list.
You can use a dynamic parameter in you Powershell script.
A good example a of a ValidateSet parameter attribute dynamically generated from a scriptblock and added to a dynamic parameter can be found here :
http://blogs.technet.com/b/pstips/archive/2014/06/10/dynamic-validateset-in-a-dynamic-parameter.aspx
DynamicParam works well for PowerShell.exe.
But i have Problems to read the ValidateSet with C# Program.
Here is the Code i use:
InitialSessionState initial = InitialSessionState.CreateDefault();
initial.ImportPSModule(new string[] { #"C:\Users\kritzinger\OneDrive\Test-DynamicValidateSet.psm1" });
Runspace runspace = RunspaceFactory.CreateRunspace(initial);
runspace.Open();
PowerShell ps = PowerShell.Create();
ps.Runspace = runspace;
ps.Commands.AddCommand("Get-Command").AddArgument("Test-DynamicValidateSet").AddParameter("ArgumentList", "Path");
Collection<PSObject> get_Command = ps.Invoke();
PSObject command = get_Command[0];
Dictionary<String, ParameterMetadata> parameters = command.Properties["parameters"].Value as Dictionary<String, ParameterMetadata>;
At the last line i get the following Exception when i try to access the Value:
An unhandled exception of type
'System.Management.Automation.GetValueInvocationException' occurred in
System.Management.Automation.dll
Additional information: Exception getting "Parameters": "Cannot
retrieve the dynamic parameters for the cmdlet. The pipeline has been
stopped."
I get the same Exeption when i try to access the Value in VisualStudio Watch window.
With a static ValidateSet deffinition the c# code works well.
I am building a customizable setup application with WiX, having started with this tutorial:
http://bryanpjohnston.com/2012/09/28/custom-wix-managed-bootstrapper-application/
The setup needs to be customizable, so I need to set some Variables from inside my MainViewModel this is an example:
var customProductName = "The Custom Product";
this.Bootstrapper.Engine.StringVariables["WixBundleName"] = theCustomProduct;
This works how expected. However, I cannot set the Variable WixBundleManufacturer. I get a System.ArgumentException: Value does not fall within the expected range.
Is it somehow possible to set the manufacturer value from inside my view model at runtime?
No, the WixBundleManufacturer is read-only variable set from the authored Bundle element Manufacturer attribute. You could open a feature request.
The feature request was implemented in v3.10.0.1719. The variable is now writable like any other Burn variable.
MongoDB for C#, I started following their tutorial but the compile error I get is on this line:
http://www.mongodb.org/display/DOCS/CSharp+Driver+Quickstart
var update = Update.Set("Name", "Harry");
saying
System.Windows.Forms.Control.Update()' is a 'method', which is not
valid in the given context.
The only difference I see is that they have used a Console Application but I created a C#WinForms applications and pasted their code inside a button click .
Update is simply ambiguous in the context you are using the call. You need to qualify the Update statement to include the namespace it is in.
var update = MongoDB.Driver.Builders.Update.Set("Name", "Harry");
This will probably get annoying, so you can also create an alias in your header.
using U = MongoDB.Driver.Builders.Update;
Then, you can change your statement to be this:
var update = U.Set("Name", "Harry");
I guess your c#WinForms contains an method called Update, which c# tries to access instead of the MongoDB one. Have you checked that you're imported everything needed and that your accessing the right Object?
Apologies if I've framed the question incorrectly but I'm not sure where it fits in exactly.
I am executing a powershell script from C# which returns a collection of type PSObject. The data I want is contained in the field BaseObject and when debugging it tells me its type is (PowerShellInside.NetCmdlets.Commands.MessageInfoObject) and I can see all the information there. So my question is assuming that a 3rd party vendor assembly is not available to be referenced, what is the correct approach to retrieving data from this object say
(PowerShellInside.NetCmdlets.Commands.MessageInfoObject).Subject
Do you create your own version of this class omitting what you dont need or is there some neat dynamic typing that can be done.
I'm not a POSH developer so I'll take this from the C# angle.
I'll assume PSObject is the equivalent of System.Object and MessageInfoObject is the type returned from the script... I'd say using something like the following should work:
dynamic msgInfo = ExecutePOSHScript(...);