如何在 powershell 中使用端口号访问我自己的本地主机?

How can I access to my own localhost with port number in powershell?

我正在用 PowerShell 编写一个脚本,它将在我的本地主机端口 81 上 运行 的应用程序中自动执行一些操作。我需要通过 PowerShell 访问此应用程序的内容,以便我可以编辑和保存它。

我学到了很多关于 PSSession 的知识,但我要访问的应用程序在我自己的机器上,而不是远程的。有办法吗?


编辑:

我要访问的应用程序是 TeamCity。我将其端口设置为 81 并访问该应用程序,我只需要在我的网络浏览器中键入 http://localhost:81 即可打开 TeamCity 的 UI.

我通过应用程序本身了解了创建新项目及其构建的过程,但这样做会在某处创建一些文件,这就是我试图以编程方式执行的操作:

nsn -ComputerName localhost -port 81 -Credential Get-Credential

我收到这个错误:

nsn : [localhost] Connecting to remote server localhost failed with the following error message: The WinRM client cannot process the request. It cannot determine the content type of the HTTP response from the destination computer. The content type is absent or invalid. For more information, see the about_Remote_Troubleshooting Help topic.At line:1 char: 1 + nsn -ComputerName localhost -Port 81 -Credential Get-Credential + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotingTransportE xception + FullyQualifiedErrorId : -2144108297,PSSessionOpenFailed

我也试过了 set-location http://localhost:81 但得到错误提示说路径不是驱动器(早知道)。

即使缺少您尝试做的事情的上下文,您也应该能够只使用“http://localhost:81”作为您的 url 而不是实际的网址。 PSSession 不适用于此处,因为您(可能)没有进行远程处理(由于缺乏详细信息而完全无法判断)

编辑:感谢您更新您的答案。我以前没有使用过 TeamCity,但我对 Git 和 TFS 非常熟悉,而且它们都不会让您通过 WinRM 进行连接。此外,您将无法通过 WinRM 连接到本地主机,因为这违背了 Windows Remote 管理的目的。如果您知道在输入 New-PSSession (nsn) 后尝试 运行 的命令,只需 运行 那些并跳过启动 PSSession。

我很确定您需要使用 TeamCity 执行的唯一命令是 git 或类似 git 的命令,它们在 Powershell 之外。否则,您将通过某种 API 调用 http/https 而不是通过 WinRM。如果您正在尝试管理 TeamCity 所在的服务器,那就是另一回事了,这让我回到了不需要 WinRM 的观点,因为您不需要远程管理您已经在的服务器,您只需管理直接。