如何使用PowerShell remoting运行远程命令

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用PowerShell remoting运行远程命令相关的知识,希望对你有一定的参考价值。

参考技术A  如何启动PowerShell
remoting呢?
  首先,你必须启动PowerShell
remoting。Windows
Server
2012中该功能是默认启动,但如果是Windows
Server
2003、2008或2008
R2,可以通过下面两种方法启用PowerShell
remoting:手动或使用组策略。
  手动启动PowerShell
remoting的方法:在服务器中打开管理PowerShell控制台,输入以下命令:
  PS
C:\>
Enable-PSRemoting
-Force
  如果你真正想在所有的服务器上启动remoting,在冰冷的数据中心里手动为每台服务器进行配置就太不人道了。这个时候最好的方法就是使用组策略啦。
  计算机配置/组策略/管理模版/Windows组件/Windows远程管理(WinRN)/WinRM服务
  大多数环境中启动PowerShell
remoting是比较简单的,如果需要详细的remoting配置信息,这本免费的电子书能够帮到你《PowerShell
Remoting的秘密》。

powershell 来自http://www.abhinavrastogi.com/powershell_for_remote_tomcat_deployment.html

# Params:
# $tomcatUrl: Url of tomcat
# $warPath: Url of war file containing application
# $appVirtualPath: Virtual path of application to be deployed
# $user: User with permissions to deploy i.e. a user in tomcat-users.xml with manager-script role
# $password: Password for specified user
Function DeployToTomcat($tomcatUrl, $warPath, $appVirtualPath, $user, $password)
{
        # Webclient for making requests
        $webclient = new-object System.Net.WebClient

        # Object for keeping authorization credentials
        $credCache = new-object System.Net.CredentialCache

        # Tomcat uses basic auth by default
        $creds = new-object System.Net.NetworkCredential($user, $password)
        $credCache.Add($tomcatUrl + "manager/text/list", "Basic", $creds)
        $webclient.Credentials = $credCache

        # 1. Stop application
        $webpage = $webclient.DownloadString($tomcatUrl  + "manager/text/stop?path=/" + $appVirtualPath)
        write-output $webpage

        # 2. Undeploy application
        $webpage = $webclient.DownloadString($tomcatUrl + "manager/text/undeploy?path=/" + $appVirtualPath)
        write-output $webpage

        # 2A. Wait for application to be undeployed
        [System.Threading.Thread]::Sleep(10000)

        #3. Upload new application
        $uploadUrl = ($tomcatUrl  + "manager/text/deploy?path=/"  + $appVirtualPath)
        $bytes =  [System.IO.File]::ReadAllBytes($warPath)
        $uploadresults = $webclient.UploadData($uploadUrl, "PUT", $bytes)
        #write-output $uploadresults

        #3A. Wait for new application to be pushed to tomcat server and unzipped
        [System.Threading.Thread]::Sleep(10000)

        #4. Start new application
        $webpage = $webclient.DownloadString($tomcatUrl  + "manager/text/start?path=/" + $appVirtualPath)
        write-output $webpage

        #5. Get a list of running applications
        $webpage = $webclient.DownloadString($tomcatUrl  + "manager/text/list")
        write-output $webpage

        #6. Print url for application
        write-output ("Application running here: " + $tomcatUrl + $appVirtualPath)
}

以上是关于如何使用PowerShell remoting运行远程命令的主要内容,如果未能解决你的问题,请参考以下文章

python 使用PowerShell Remoting的Python(Windows等效于Unix ssh会话)

python 使用PowerShell Remoting的Python(Windows等效于Unix ssh会话)

PowerShell启用多跳远程控制

PowerShell启用多跳远程控制

如何从PowerShell /批处理文件中的网络路径运行命令

Windows 上的 SSH?使用 PowerShell Remoting 远程管理 Windows 服务器