【问题标题】:Running a PowerShell script on a remote Windows server using WinRM使用 WinRM 在远程 Windows 服务器上运行 PowerShell 脚本
【发布时间】:2019-03-05 08:14:54
【问题描述】:

我编写了一个脚本,允许使用 WinRM 连接到 Windows 服务器计算机,以便运行存在于 Windows 服务器计算机 PS_Block_Access_Internet_GPO.ps1 上的脚本,但尽管会话创建良好,但该脚本未执行。

除了脚本需要管理员权限才能执行,那么如何使用PowerShell为脚本提供所需的权限。

Enable-PSRemoting
Enter-PSSession -ComputerName Server.Admin.6NLG-AD
.\PS_Block_Internet_Access_GPO.ps1

【问题讨论】:

    标签: powershell winrm


    【解决方案1】:

    要针对远程计算机运行本地脚本,我将使用Invoke-Command,这不需要远程计算机上存在脚本。

    Invoke-Command -ComputerName 'Server.Admin.6NLG-AD' -FilePath C:\Folder\myScript.ps1
    

    由于您的脚本希望创建 GPO,您可能需要使用对您的域具有适当权限的备用用户帐户...

    您可以使用Credential 参数来指定这样的帐户:

    Invoke-Command -ComputerName 'Server.Admin.6NLG-AD' -FilePath C:\Folder\myScript.ps1 -Credential Domain\Username
    

    【讨论】:

    • 这适用于我要从中打开会话的机器上存在的脚本?
    • 是的,脚本存在于本地计算机上,其中的命令针对远程计算机运行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多