zoukankan      html  css  js  c++  java
  • 在远程机器上跑PowerShell脚本

    当我们需要在远程机器上跑一些PowerShell命令或者文件的时候,可以运行以下命令

    $secpassword = ConvertTo-SecureString "Password" -AsPlainText –Force

    $mycreds = New-Object System.Management.Automation.PSCredential ("Username", $secpassword)

    Enter-PSSession -ComputerName ComputerName -Credential $mycreds

    你可能会遇到以下错误:

    Enter-PSSession : Connecting to remote server failed with the following error message : The client cannot connect to th e destination specified in the request. Verify that the service on the destination is running and is accepting requests
    . Consult the logs and documentation for the WS-Management service running on the destination, most commonly IIS or Win
    RM. If the destination is the WinRM service, run the following command on the destination to analyze and configure the
    WinRM service: "winrm quickconfig". For more information, see the about_Remote_Troubleshooting Help topic.
    At line:1 char:16
    + Enter-PSSession <<<< -ComputerName NEB-OM-336757.smx.net -Credential $mycreds
    + CategoryInfo : InvalidArgument: (
    ComputerName:String) [Enter-PSSession], PSRemotingTransportException
    + FullyQualifiedErrorId : CreateRemoteRunspaceFailed

    因为你没有在远程机器上开启允许远程执行PowerShell命令导致了这个错误的出现。

    你需要在远程机器上用管理员权限打开PowerShell控制台,执行命令Enable-PSRemoting –Force

    再执行以上命令界面就会出现,此时你就可以在这个控制台上输入命令执行远程操作了

    [ComputerName]: PS C:\Users\asttest\Documents>

    注意,这个Session直到PowerShell界面关闭,或者强制退出为止。比如运行Exit-PSSession

  • 相关阅读:
    jquery的get方式发送AJAX请求
    原生JS发送AJAX请求
    正则表达式(二)
    正则表达式(一)
    旅游攻略-北京三日游攻略(已实践)
    边旅游边赚钱的噱头,这是一种传销!
    hdu 1106 排序(水题)
    hdu 1258 Sum It Up(dfs+去重)
    hdu 1455 Sticks(dfs+剪枝)
    解决“LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏”问题
  • 原文地址:https://www.cnblogs.com/lilideng/p/Run_PowerShell_Remotely.html
Copyright © 2011-2022 走看看