powershell-sdk

The RunSpace and its closure

与世无争的帅哥 提交于 2020-02-27 13:01:28
问题 While working with a script that uses a RunSpace, I found that it takes up more and more system memory. As far as I understand, this is due to the fact that open RunSpace do not close when completed. They remain in memory, accumulating megabytes. How to close the RunSpace, correctly? However, I do not know how long it will take - 1 second or 1 hour. Closes itself when completed. As an example, I will give arbitrary scripts. The first script is how I do the closing of the RunSpace as it is

The RunSpace and its closure

老子叫甜甜 提交于 2020-02-27 13:01:10
问题 While working with a script that uses a RunSpace, I found that it takes up more and more system memory. As far as I understand, this is due to the fact that open RunSpace do not close when completed. They remain in memory, accumulating megabytes. How to close the RunSpace, correctly? However, I do not know how long it will take - 1 second or 1 hour. Closes itself when completed. As an example, I will give arbitrary scripts. The first script is how I do the closing of the RunSpace as it is

Powershell ignores parameter passed via SessionStateProxy.SetVariable

你离开我真会死。 提交于 2019-12-30 11:11:33
问题 I have the following Powershell script. param([String]$stepx="Not Working") echo $stepx I then try using the following C# to pass a parameter to this script. using (Runspace space = RunspaceFactory.CreateRunspace()) { space.Open(); space.SessionStateProxy.SetVariable("stepx", "This is a test"); Pipeline pipeline = space.CreatePipeline(); pipeline.Commands.AddScript("test.ps1"); var output = pipeline.Invoke(); } After the above code snippet is run, the value "not working" is in the output

How can I run initialization code each time my snap-in is loaded?

旧巷老猫 提交于 2019-12-24 10:19:28
问题 I have a PowerShell snapin, and I would like to run a bit of initialization code (hooking some AppDomain events) each time my snapin is loaded (i.e. once for each powershell.exe process that is started). How can this be accomplished? 回答1: One approach, and this is probably a bit rube-goldberg in nature, is to hook into a provider's startup code. This assumes that in your snapin configuration you have declared a default drive to initialize. It just so happens that in PSCX we use a provider to

Cant run ps1 script from out program files folder

半腔热情 提交于 2019-12-19 09:20:21
问题 I can run this script perfectly from my desktop: private void Sleep_Click(object sender, EventArgs e) { PowerShell ps = PowerShell.Create(); ps.AddScript(@"D:\Desktop\alllightsoff.ps1"); ps.Invoke(); } But when i change the paths to program files it does nothing.. any ideas? private void Sleep_Click(object sender, EventArgs e) { PowerShell ps = PowerShell.Create(); ps.AddScript(@"C:\Program Files (x86)\Home Control\alllightsoff.ps1"); ps.Invoke(); } Has proberly something to do with

How to get ScriptProperty Values of PSObject.Properties in C#?

天涯浪子 提交于 2019-12-10 17:13:25
问题 I am trying to get drive information for a server through PowerShell 6.0 using the 'GET-PSDrive' command. Running the command directly in PowerShell, I see the values for 'Used' and 'Free' within the output table, but running the same command in code though using the Microsoft.Powershell.Sdk the 'Used' and 'Free' fields are not available. I see both items listed under the PSObject.Properties array, but trying to access the value I receive the exception: "There is no Runspace available to run

How to pass $_ ($PSItem) in a ScriptBlock

孤街醉人 提交于 2019-12-02 04:27:10
问题 I'm basically building my own parallel foreach pipeline function, using runspaces. My problem is: I call my function like this: somePipeline | MyNewForeachFunction { scriptBlockHere } | pipelineGoesOn... How can I pass the $_ parameter correctly into the ScriptBlock? It works when the ScriptBlock contains as first line param($_) But as you might have noticed, the powershell built-in ForEach-Object and Where-Object do not need such a parameter declaration in every ScriptBlock that is passed to

How to pass $_ ($PSItem) in a ScriptBlock

冷暖自知 提交于 2019-12-02 01:16:46
I'm basically building my own parallel foreach pipeline function, using runspaces. My problem is: I call my function like this: somePipeline | MyNewForeachFunction { scriptBlockHere } | pipelineGoesOn... How can I pass the $_ parameter correctly into the ScriptBlock? It works when the ScriptBlock contains as first line param($_) But as you might have noticed, the powershell built-in ForEach-Object and Where-Object do not need such a parameter declaration in every ScriptBlock that is passed to them. Thanks for your answers in advance fjf2002 EDIT: The goal is: I want comfort for the users of

Custom PowerShell Host and Converting PSObject back to base type

时光怂恿深爱的人放手 提交于 2019-11-30 09:11:20
问题 When hosting the PowerShell runtime is it possible to convert a PSObject back into its original type some how? For example: I have a cmdlet that calls WriteObject and pushes a collection of ClassXzy in the pipeline. When I call PowerShell.Invoke from the host end of things I retrieve a collection of PSObject s with a BaseObject property. Casting BaseObject to ClassXyz fails. Is there any way around mapping each property value to its corresponding original object? I'm assuming PowerShell does

Custom PowerShell Host and Converting PSObject back to base type

吃可爱长大的小学妹 提交于 2019-11-29 13:16:02
When hosting the PowerShell runtime is it possible to convert a PSObject back into its original type some how? For example: I have a cmdlet that calls WriteObject and pushes a collection of ClassXzy in the pipeline. When I call PowerShell.Invoke from the host end of things I retrieve a collection of PSObject s with a BaseObject property. Casting BaseObject to ClassXyz fails. Is there any way around mapping each property value to its corresponding original object? I'm assuming PowerShell does this somehow as you can pass PSObject s to cmdlets and they get translated to the Parameter types. But