从 ASP.NET 应用程序调用 New-PSSession 失败(创建空对象)

发布于 2024-12-07 13:53:06 字数 1537 浏览 1 评论 0原文

我有一个 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 变量为 null — 未创建/建立会话 — 因此,该脚本不会在远程计算机上运行。 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 无法创建远程会话的信息。

I've got a powershell script that I'm trying to run on a remote computer. I have a local PS script that creates a session to the remote machine and then calls the remote script, like this:

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

If I run the local script from a PS console, it works just as expected, and the remote script runs. However, when I try to execute it from my ASP.NET app, the $session variable is null -- a session is not created/established--and, therefore, the script is not run on the remote computer. Powershell does not return an error or any other indication that it failed...I can just see that $session -eq $null.

I've verified that the apppool that the web app is using is running under the same account that I log on with, so it should have the same permissions.
Here's how I'm calling the local script from within 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();

I'm hoping someone can see what I'm missing; the Googles don't seem to have much on powershell failing to create a remote session.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

極樂鬼 2024-12-14 13:53:06

据我了解,可能存在两个问题:

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

无论使用PowerShell 2.0 from ASP.NET Part 1 似乎是一本很好的食谱,可以满足您的需求。存在三个较旧的第 1 部分第 2 部分 第 3 部分

As far as I understand there can be two problems :

  1. Enter-PsSession is used for interactive remote session you'd better consider to replace it with New-PSSession.
  2. You may encounter credential troubles

Whatever Using PowerShell 2.0 from ASP.NET Part 1 seems to be a good cookbook for what you want to do.Exists three older ones Part1, Part2 Part3.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文