简体   繁体   English

为什么将Get-PSSession传递到Exit-PSSession无效?

[英]Why does piping Get-PSSession to Exit-PSSession not work?

I'm creating a new Powershell session like so: 我正在创建一个新的Powershell会话,如下所示:

New-PSSession -ConnectionUri https://myserver:12345 -Credential myserver\username -Authentication default

This is working fine, I can do Get-PSSession and it provides me with that one session. 这工作正常,我可以执行Get-PSSession ,它为我提供了一个会话。 However, when trying to pipe the result to Exit-PSSession things go haywire. 但是,当尝试将结果传递给Exit-PSSession事情变得一团糟。 So this: 所以这:

Get-PSSession | Exit-PSSession

Or this: 或这个:

Get-PSSession 1 | Exit-PSSession

Will give an error: 会给出一个错误:

Exit-PSSession : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input. Exit-PSSession:输入对象不能绑定到该命令的任何参数,因为该命令不接受管道输入,或者该输入及其属性与接受管道输入的任何参数都不匹配。

I've tried fixing/understanding this by doing Get-PSSession | Get-Member 我尝试通过执行Get-PSSession | Get-Member解决/理解这一问题Get-PSSession | Get-Member Get-PSSession | Get-Member which confirms that a proper object is returned from the first command. Get-PSSession | Get-Member ,它确认从第一个命令返回了正确的对象。

I've also tried Get-Help Exit-PSSession , and saw that it has only [<CommonParameters>] , which makes me wonder about its purpose in the first place (it's probably a commandlet to exit the PSSession you're currently in?). 我也尝试过Get-Help Exit-PSSession ,并且看到它只有[<CommonParameters>] ,这让我首先想知道它的用途(它可能是退出您当前所在的PSSession的Commandlet吗? )。 In any case, this does explain why piping to that commandlet didn't work. 无论如何,这确实解释了为什么无法对该命令行开关进行配管。

I've also checked Get-Help Disconnect-PSSession , but that just disconnects and leaves the PSSession around, it seems. 我还检查了Get-Help Disconnect-PSSession ,但这似乎断开了连接并留下了PSSession。

Bottom line: I (think I) am looking for the opposite of New-PSSession , but can't seem to find it. 底线:我(想我)正在寻找 New-PSSession 相反的东西,但似乎找不到它。 What's the proper way to get rid of a PSSession? 摆脱PSSession的正确方法是什么?

You've not looked closely enough. 您看起来不够仔细。 Don't forget, it's easy to get all cmdlets related to a certain subject by doing something like this: 别忘了,通过执行以下操作很容易获得与某个主题相关的所有cmdlet:

 Get-Help PSSession

This gets a list of all cmdlets with "PSSession" it its name. 这将获得其名称为“ PSSession”的所有cmdlet的列表。 If you carefully review the output, there's Exit-PSSession and Disconnect-PSSession , but there's one more: Remove-PSSession . 如果仔细查看输出,则存在Exit-PSSessionDisconnect-PSSession ,但还有一个: Remove-PSSession If view the extended help you'll see that it does a bit more than its name may imply (emphasis mine): 如果查看扩展帮助,您会发现它的功能超出其名称所暗示的范围(强调我的意思):

The Remove-PSSession cmdlet closes Windows PowerShell sessions (PSSessions) in the current session . Remove-PSSession cmdlet将关闭当前会话中的Windows PowerShell会话(PSSessions) It stops any commands that are running in the PSSessions, ends the PSSession, and releases the resources that the PSSession was using. 它停止在PSSession中运行的所有命令,结束PSSession,并释放PSSession使用的资源。 If the PSSession is connected to a remote computer, Remove-PSSession also closes the connection between the local and remote computers. 如果PSSession连接到远程计算机,则Remove-PSSession也会关闭本地计算机和远程计算机之间的连接。

So it doesn't just "remove" the PSSession from sight, but it also closes the session and disconnects from the remote computer. 因此,它不仅可以“删除” PSSession,而且还可以关闭会话并与远程计算机断开连接。

Piping from Get-PSSession will work as expected: Get-PSSession管道将按预期工作:

Get-PSSession | Remove-PSSession

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM