【问题标题】:Calling New-PSSession from ASP.NET app fails (creates null object)从 ASP.NET 应用程序调用 New-PSSession 失败(创建空对象)
【发布时间】:2011-10-10 23:29:02
【问题描述】:

我有一个我正试图在远程计算机上运行的 powershell 脚本。我有一个本地 PS 脚本,它创建与远程机器的会话,然后调用远程脚本,如下所示:

param($serverName, $rootPath, $appsToInstall)
$session = Enter-PsSession -ComputerName $serverName

$command = {
param($path,$apps) 
$path\temp\IISsetup.ps1 $path $apps
}

$output = Invoke-Command -Session $session -scriptblock $command -ArgumentList 
   $rootPath,$appsToInstall

Remove-PSSession -Session $session

如果我从 PS 控制台运行本地脚本,它会按预期工作,并且远程脚本会运行。但是,当我尝试从我的 ASP.NET 应用程序执行它时,$session 变量为空——未创建/建立会话——因此,该脚本不在远程计算机上运行。 Powershell 不会返回错误或任何其他失败的指示...我只能看到 $session -eq $null

我已验证该网络应用正在使用的应用池在我登录时使用的同一帐户下运行,因此它应该具有相同的权限。 以下是我在 ASP.NET 中调用本地脚本的方式:

using (Runspace remoteRunspace = RunspaceFactory.CreateRunspace())
                {
                    remoteRunspace.Open();
                    using (PowerShell ps = PowerShell.Create())
                    {
                        ps.Runspace = remoteRunspace;

                        var cmd = new Command(Path.Combine(ServerPath, "LocalIISSetupLauncher.ps1"));
                        cmd.Parameters.Add("serverName", this.Server.ServerName);
                        cmd.Parameters.Add("rootPath", LocalIisDirectory);
                        cmd.Parameters.Add("appsToInstall", Server.InstalledApplications.ToList().ToArray());

                        ps.Commands.AddCommand(cmd);

                        var results = ps.Invoke();

我希望有人能看到我缺少的东西;谷歌似乎没有太多关于 powershell 无法创建远程会话的问题。

【问题讨论】:

  • 我没有得到答案,但我建议您查看 IIS 的配置。我认为您需要将网站配置为匿名访问\不进行模拟以避免任何双跳问题。
  • "$session = Enter-PsSession -ComputerName $serverName" 没有意义 - 那不应该是 $session = new-pssession?

标签: powershell powershell-remoting


【解决方案1】:

据我了解可能有两个问题:

  1. Enter-PsSession 用于交互式远程会话,您最好考虑将其替换为 New-PSSession
  2. 您可能会遇到凭据问题

无论Using PowerShell 2.0 from ASP.NET Part 1 似乎都是您想做的好食谱。存在三个较旧的Part1Part2Part3

【讨论】:

  • 您链接到“使用 ASP.NET 第 1 部分中的 PowerShell 2.0”似乎很糟糕。
猜你喜欢
  • 1970-01-01
  • 2022-01-23
  • 2016-05-21
  • 2011-01-07
  • 2015-07-01
  • 2021-05-05
  • 2017-09-11
  • 1970-01-01
  • 2021-08-25
相关资源
最近更新 更多