标签: runspace

如何托管Powershell脚本或应用程序,以便可以通过WSManConnectionInfo访问它?(如Office 365)

我知道连接到远程运行空间的唯一方法包括以下参数

   WSManConnectionInfo connectionInfo = 
     new WSManConnectionInfo(false, "localhost", 80, "/Powershell", "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
Run Code Online (Sandbox Code Playgroud)

要么

   WSManConnectionInfo connectionInfo = 
     new WSManConnectionInfo(false, "localhost", 5985, "/wsman", "http://schemas.microsoft.com/powershell/Microsoft.Powershell", credential);
Run Code Online (Sandbox Code Playgroud)
  • 如何设置我自己的自定义Powershell对象,以便通过HTTP公开它?

  • 使用的正确参数是什么以及如何设置它们?

c# powershell cmdlets powershell-remoting runspace

19
推荐指数
1
解决办法
2867
查看次数

使用C#中的Powershell远程调用进行内存泄漏

我有一个Windows服务,它正在进行大量的交换远程调用以获取一些服务器信息.我注意到,随着时间的推移,服务使用的内存开始增长,直到抛出内存异常.我已经搜索过,看起来有一个已知的内存泄漏System.Management.Automation,它不会Runspace在调用close和/或dispose方法时释放所创建的所有内存.我回顾了建议使用后CreateOutOfProcessRunspaceRunspaceFactory,但不知道如何使用它.

以下是如何重现该问题:( System.Management.Automation参考dll)

for (int i = 0; i < 1000; i++)
{
    var runspace = RunspaceFactory.CreateRunspace();
    runspace.Open();
    runspace.Close();
    runspace.Dispose();
}
Run Code Online (Sandbox Code Playgroud)

如果运行此代码,您将看到内存如何递增.由于要求,尽可能保持连接打开不是一个好的解决方案.

你知道我怎么能解决这个问题,即使使用CreateOutOfProcessRunspace的方法RunspaceFactory或如何处置属性格式记忆?

提前致谢

编辑

我正在使用V3并更改运行空间创建以使用CreateRunspacePool方法,看起来泄漏已经消失.非常感谢你的帮助!

c# memory powershell memory-leaks runspace

10
推荐指数
1
解决办法
3490
查看次数

在Web应用程序中托管powershell运行空间

我正在编写一个执行powershell脚本的Web服务(活动目录,目录管理等).

现在,每个Web请求都会创建Runspace实例.由于Runspace初始化是一项耗时的操作,因此通常我必须导入像ActiveDirectory这样的模块,这些模块的运行速度也很慢.

在此博客文章" 使用C#管理Exchange 2007收件人"中,Runspace实例保留在静态字段中.

如果我将Runspace实例保存在静态字段中,我会徘徊,它是否是线程安全的?这样做可能有其他缺点吗?

谢谢

c# powershell powershell-2.0 runspace powershell-hosting

9
推荐指数
1
解决办法
2483
查看次数

范围vs Runspace vs Session vs AppDomain

我正在努力为PowerShell划清界限.我的理解非常有限:

  • 范围包含用户/脚本定义的变量和函数,并且可以存在具有PS调用堆栈的范围层次结构.
  • Runspace规定了给定PS实例可以访问的内置功能.Runspace可以跨越网络边界.
  • Session是Powershell的特定实例.这些也可以跨越网络边界.
  • 应用程序域(或AppDomain)包含已加载的程序集.在许多情况下,一旦将数据加载到AppDomain中,就无法卸载它.必须处理AppDomain以支持新的AppDomain.如果通过其他应用程序调用PS脚本,则创建的PS实例将继承调用应用程序的AppDomain.

谁能更好地解释这些概念?是否有某种维恩图或其他能够充实这些信息的东西?在线文档并没有太大帮助.

powershell session appdomain runspace

9
推荐指数
1
解决办法
576
查看次数

在powershell中使用Task类

我正在尝试使用Powershell中的Task类异步运行操作.但我得到以下异常:

Id                     : 1
Exception              : System.AggregateException: One or more errors occurred. ---> System.Management.Automation.PSInvalidOperationException: There is no Runspace available to
                         run scripts in this thread. You can provide one in the DefaultRunspace property of the System.Management.Automation.Runspaces.Runspace type. The script
                         block you attempted to invoke was:  Write-Host "hey!"
                            at System.Management.Automation.ScriptBlock.InvokeAsDelegateHelper(Object dollarUnder, Object dollarThis, Object[] args)
                            at System.Threading.Tasks.Task.Execute()
                            --- End of inner exception stack trace ---
                         ---> (Inner Exception #0) System.Management.Automation.PSInvalidOperationException: There is no Runspace available to run scripts in this thread. …
Run Code Online (Sandbox Code Playgroud)

powershell asynchronous task runspace

7
推荐指数
1
解决办法
3609
查看次数

我不明白我得到的NewNotImplementedException运行时错误

我正在尝试实现一个远程运行空间,它需要connectionInfo与Exchange通信,导入模块需要与活动目录通信.这是问题代码:

runspace = System.Management.Automation.Runspaces.RunspaceFactory.
CreateRunspace(psConnectionInfo);
runspace.InitialSessionState.ImportPSModule(new[] { "ActiveDirectory" });
runspace.Open();
Run Code Online (Sandbox Code Playgroud)

我得到的运行时错误是:

无法执行操作,因为操作"文件中的偏移32处的NewNotImplementedException:line:column:0:0"未实现

如果我省略该runspaceInitialSessionState行,则不会收到错误但是SetADServerSettingsViewEntireForest 的PowerShell命令无法运行,因为它无法识别.

堆栈跟踪:

无法执行操作,因为未实现操作"文件中的偏移32处的NewNotImplementedException:line:column:0:0".在System.Management.Automation.RemoteRunspace.get_InitialSessionState()位于c:\ Users\rtanner.CATNET\Documents\Visual Studio 2013\Projects\ManageUserForwardsWS\ManageUserForwardsWS\ManageUserForwards.asmx中的ManageUserForwardsWS.ManageUserForwards.SetExchangeCredentials(String userName,String PwString) .cs:第122行

我也可以使用此代码生成相同的错误:

        Pipeline pipeline = runspace.CreatePipeline();

        PowerShell powershell = PowerShell.Create();
        powershell.Runspace = pipeline.Runspace;

        powershell.Runspace.InitialSessionState.ImportPSModule(new[] { "ActiveDirectory" });
Run Code Online (Sandbox Code Playgroud)

这是StackTrace:

无法执行操作,因为未实现操作"文件中的偏移32处的NewNotImplementedException:line:column:0:0".在System.Management.Automation.RemoteRunspace.get_InitialSessionState()位于c:\ Users\rtanner.CATNET\Documents\Visual Studio 2013\Projects\ManageUserForwardsWS\ManageUserForwardsWS\ManageUserForwards.asmx中的ManageUserForwardsWS.ManageUserForwards.SetForward(String sAMAccountName,String fowardAddress) .cs:第151行

这个附加信息有帮助吗?有想法该怎么解决这个吗?

c# powershell exchange-server runspace

7
推荐指数
1
解决办法
1221
查看次数

Powershell System.Management.Automation 参考程序集 4.0 中“Write-Host”输出的位置

我正在将 System.Management.Automation 与参考程序集 4.0 和 C# 一起使用

我需要查看 Write-Host 的输出。文档说 Write-Host 将在输出流中输出。使用 powershell 4.0 的参考程序集时,在 C# 中获取 Write-Host 输出的输出流是什么?

我知道后来在 Powershell 5.0 版本中添加了信息管道,并且 Write-Host 和 Write-Information 始终将输出传输到信息管道。

但我需要使用 powershell 4.0 的参考程序集查看 Write-Host 的输出。使用以下代码,我无法在任何地方看到 Write-Host 的输出。不在输出处且不在输出集合中。

目前我已经订阅了以下流。

using (var powerShell = PowerShell.Create(iss))
{           
    var psScript = "Write-Host test input";
    powerShell.AddScript(psScript);

    powerShell.Streams.Debug.DataAdding += OnDebugDataAdding; 
    powerShell.Streams.Error.DataAdding += OnErrorAdding;
    powerShell.Streams.Warning.DataAdding += OnWarningAdding;
    powerShell.Streams.Verbose.DataAdding += OnVerboseAdding;

    var outputCollection = new PSDataCollection<PSObject>();
    outputCollection.DataAdding += OnOutputDataAdding; // all spitted outputs getting into outputCollection

    powerShell.Invoke(null, outputCollection);
}
Run Code Online (Sandbox Code Playgroud)

c# pipeline runspace powershell-4.0

6
推荐指数
1
解决办法
1180
查看次数

为什么无法为远程 RunspacePool 传递 InitialSessionState?

为什么以下方法没有接受 RunspaceConnectionInfo 用于指定远程服务器信息)和 InitialSessionState 的重载

http://msdn.microsoft.com/en-us/library/system.management.automation.runspaces.runspacefactory.createrunspacepool(v=vs.85).aspx

完整上下文:

我正在构建一个 RunspacePoolCache,它将缓存使用 RunspaceFactory 创建的远程 RunspacePool。缓存以远程服务器信息为关键。在池的 RunspacePoolStateInfo.State 处于打开状态之前,同一个 RunspacePool 将用于在同一远程服务器上执行 Powershell 脚本。(无耻插件:这行得通吗?)

现在,我想添加一组对创建的 RunspacePool 通用的 Powershell 管理单元。在执行的脚本中添加管理单元有时会导致以下异常:

An item with the same key has already been added
Run Code Online (Sandbox Code Playgroud)

即使我在 Powershell 脚本中执行以下操作(但频率较低),也会发生这种情况:

if ((Get-PSSnapin | ? { $_.Name -eq 'VeeamPSSnapIn' }) -eq $null) {
    Add-PsSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue
}
Run Code Online (Sandbox Code Playgroud)

这就是我尝试通过InitialSessionState加载管理单元的地方。但从提供的方法集来看,似乎只能在创建本地 RunspacePools 时指定 InitialSessionState。

c# powershell caching runspace

5
推荐指数
1
解决办法
1445
查看次数

Powershell 中的 Runspace Dispose(特定于 GUI)

我是 StackOverflow 的活跃读者,因为它通常可以帮助我解决所有问题。
但是对于我的第一个问题,我会就 Powershell 中的运行空间(特别是内存管理)向您寻求帮助。

我创建了一些带有 GUI 的 PS 应用程序,现在我使用运行空间来避免在运行大型操作时挂起 GUI。我的问题是我无法在运行空间完成后处理它们。

关于下面的代码,这是一个简单的脚本,试图了解我如何处理我的运行空间。我试图合并一个独特的运行空间来监视和处理应用程序运行空间,我从proxb 的脚本(顺便说一句,巨大的粉丝)中启发了自己,但它似乎不起作用。

每次我执行运行空间时,我都会获得 10Mb 的内存,巨大的泄漏!

泄露

你能帮我解决这个问题吗?

[xml]$xaml = @'
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Runspace" FontFamily="Calibri" Height="400" Width="400" FontSize="14">
    <Grid Margin="10,10,10,10">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="10"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <TextBox x:Name="TB_Results" Grid.Row="0" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto"/>
        <Button x:Name="BT_Run" Grid.Row="2" Content="Run" Height="30"/>
    </Grid>
</Window>
'@

# Inits
Add-Type -AssemblyName PresentationFramework            
Add-Type -AssemblyName PresentationCore
Add-Type -AssemblyName WindowsBase
$gui = [hashtable]::Synchronized(@{})
$reader = (New-Object Xml.XmlNodeReader $xaml)
$gui.Window = [Windows.Markup.XamlReader]::Load($reader)
$xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]") | ForEach-Object { $gui.Add($_.Name,$gui.Window.FindName($_.Name)) …
Run Code Online (Sandbox Code Playgroud)

powershell dispose runspace

5
推荐指数
1
解决办法
2301
查看次数

新的 PowerShell 7 ForEach-Object Parallel 是如何实现的?

PowerShell 7 引入了一个非常需要的功能来并行运行管道输入。

PowerShell 7的文档没有提供有关如何实现的任何详细信息。

已经利用PoshRSJobInvoke-Parallel之前的模块,我知道,运行空间在传统上认为是在运行PowerShell的工作在PowerShell中并行操作的更有效的方法。我已经阅读了一些混合内容,表明这是现在使用线程而不是运行空间,但找不到任何其他特定内容。

我真的很感激对以下方面的一些技术见解:

  1. 从 .NET 的角度来看,执行的生命周期是什么
  2. 新功能是运行空间还是线程?(或者运行空间只是 System.Management.Automation 中的一个 .NET 线程?)
  3. 既然我们正在进入并行操作,这是否会给传统调试带来任何复杂性?从历史上看,我在使用运行空间进行调试时遇到了困难,并且不确定哪些选项可能已得到改进

powershell runspace powershell-core

5
推荐指数
1
解决办法
2044
查看次数