runspace

manage objects and variables from another PowerShell runspace

有些话、适合烂在心里 提交于 2019-12-12 03:15:31
问题 I am currently working with the System.Timers.Timer object in Powershell. The Problem i have encountered is, that when you register to the "Elapsed" event Register-ObjectEvent -InputObject $timer -EventName Elapsed -SourceIdentifier ThirtySecTimer -Action $scriptblock the scriptblock will run in a different Powershell Runspace / thread The problem here is, that i want to modify the timer object in the scriptblock and basically create a loop of shorter Intervals until it reaches x seconds and

Error when accessing UNC paths through powershell script when remoting

被刻印的时光 ゝ 提交于 2019-12-11 07:47:00
问题 I am trying to execute a program inside of a power shell script. The PS script is being called from a C# method using Runspaces. The program tries to make an update to a config file on a remote server. When I run this whole thing I get the following error: System.UnauthorizedAccessException: Access to the path \\some path is denied. The PS script is on a remote server. If I run the PS script directly on the server then the PS script and the program inside of it runs fine and is able to access

Run Powershell from C#

醉酒当歌 提交于 2019-12-11 03:59:10
问题 I need to start a powershell script from C# and get the PSSSecurityException on pipeline.Invoke() AuthorizationManager check failed. My code: RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create(); using (Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration)) { runspace.Open(); Pipeline pipeline = runspace.CreatePipeline(); Command scriptCommand = new Command(scriptfile); pipeline.Commands.Add(scriptCommand); pipeline.Invoke(); } Questions I suspect

New-PSSession and Runspacepool clarification

梦想的初衷 提交于 2019-12-11 03:38:45
问题 I need to run Exchange online cmdlets using powershell classes in C#. To run exchange online cmdlets i need to establish a remote powershell session. My doubts are: 1) If runspacepool size is 2, should i create that remote powershell session in both the runspaces in that runspacepool? If yes, how can I / Is there a way to loop through the runspaces to run the New-PSSession command in both the runspaces. 2) If session expires in ONE runspace, is there a way to get that particular runspace from

MVC Controller with Runspace Impersonation

夙愿已清 提交于 2019-12-10 23:29:16
问题 Trying to execute a Powershell cmdlet from a MVC 3 Controller using impersonation but keep receiving an "Requested registry access is not allowed." exception when calling Runspace.Open() StringBuilder stringBuilder = new StringBuilder(); RunspaceConfiguration rsConfig = RunspaceConfiguration.Create(); using (new Impersonator("username", "domain", "password")) { Runspace runspace = RunspaceFactory.CreateRunspace(rsConfig); runspace.Open(); Pipeline pipeLine = runspace.CreatePipeline(); string

Unable to execute two Office 365 commands simultaneously using different accounts in C#

自作多情 提交于 2019-12-08 03:28:22
问题 I am trying to query the users of two different Office 365 accounts simultaneously using C#. It works fine when trying from two Powershell Windows or when connecting and getting users one account after other by code. But not working when doing simultaneously using code. On checking I found that only one log file is generated when trying from C#. But two different log files are generated when trying from PowerShell window. Log folder location: %userprofile%\appdata\Local\Microsoft\Office365

how to establish and enter a remote session using runspace in powershell

隐身守侯 提交于 2019-12-07 22:58:32
问题 I am trying to establish a session with a remote host B from my machine A, within a C# code. I am using runspace API for that. the code snippet is provided below Runspace runspace = RunspaceFactory.CreateRunspace(); runspace.Open(); //constructing the vmname parameter here vmname = useralias + DateTime.Now.ToString(); Pipeline pipeline = runspace.CreatePipeline(); string scripttext = "$secpasswd = ConvertTo-SecureString '222_bbbb' -AsPlainText –Force"; string scripttext1 = "$mycreds = New

Custom PSHostUserInterface is ignored by Runspace

[亡魂溺海] 提交于 2019-12-06 05:29:37
问题 The Background I'm writing an application that programatically executes PowerShell scripts. This application has a custom PSHost implementation to allow scripts to output logging statements. Currently, the behavior I'm seeing is that some requests are properly forwarded to my custom PSHost and others are flat out ignored. Things get even stranger when I started inspecting the $Host variable in my scripts, which seem to suggest that my custom PSHost isn't even being used. The Code I have some

Memory leak using Powershell Remote Calls in C#

做~自己de王妃 提交于 2019-12-05 16:55:21
问题 I have a windows service that is doing a lot of exchange remote calls to get some server information. I noticed that as longs as the time passes the memory used by the service starts growing until a memory exception is thrown. I have searched and it looks like there is a known memory leak in the System.Management.Automation that does not dispose all the memory of the Runspace created while calling the close and/or dispose method. I reviewed a post that suggest using the

Custom PSHostUserInterface is ignored by Runspace

纵饮孤独 提交于 2019-12-04 10:52:35
The Background I'm writing an application that programatically executes PowerShell scripts. This application has a custom PSHost implementation to allow scripts to output logging statements. Currently, the behavior I'm seeing is that some requests are properly forwarded to my custom PSHost and others are flat out ignored. Things get even stranger when I started inspecting the $Host variable in my scripts, which seem to suggest that my custom PSHost isn't even being used. The Code I have some code that's executing PowerShell within a .NET application: var state = InitialSessionState