如何检查 Powershell 脚本是否正在远程运行 [英] How to check if a Powershell script is running remotely

查看:66
本文介绍了如何检查 Powershell 脚本是否正在远程运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以在本地或远程(通过 WinRM)运行的脚本,但是我希望它在远程机器上运行时的行为略有不同.我意识到我可以向脚本传递一个开关来识别它是本地运行还是远程运行,但我想知道脚本本身是否可以检测它是否是远程运行的?

解决方案

Get-Host 返回一个 Name 和其他信息:

<前>PS> (Get-Host).Name控制台主机PS> (Invoke-Command -ComputerName dev2 -Script {Get-Host}).Name服务器远程主机

I have a script that can be run either locally or remotely (via WinRM), however I would like it to behave slightly differently when run on a remote machine. I realise that I can pass in a switch to the script to identify whether it is running locally or remotely, but I want to know if it is possible for the script itself to detect whether it is running remotely?

解决方案

Get-Host returns, amongst other information a Name:

PS> (Get-Host).Name
ConsoleHost
PS> (Invoke-Command -ComputerName dev2 -Script {Get-Host}).Name
ServerRemoteHost

这篇关于如何检查 Powershell 脚本是否正在远程运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆