【问题标题】:How I can execute extern Module Commands on a remote Computer in Powershell without install the Module?如何在不安装模块的情况下在 Powershell 中的远程计算机上执行外部模块命令?
【发布时间】:2015-06-24 09:05:40
【问题描述】:

我想在远程计算机上执行 powershell 命令,我在本地主机上使用了一个外部模块。我对其进行了测试并得到消息,我不能在远程计算机上使用该命令,但在本地它可以工作。

所以我已经通过 Import-Module 在远程计算机上安装了模块并且它可以工作。

但这很糟糕,我不想在公司的每台计算机上都安装该模块。

这是我的 PS 脚本:

$cred = Get-Credential

$isConnect = Test-WSMan edv-004

if($isConnect){

    #Get-WUHistory is from a extern Module and list the Updatehistory of a computer
    Invoke-Command -ComputerName edv-004 -ScriptBlock { Get-WUHistory } -Credential $cred

    Write-Host "Skript wurde auf ausgeführt..."
}

在我运行这个脚本之前,我已经这样做了:

Enable-PSRemoting -Force
Set-Item WSMan:\localhost\Client\TrustedHosts * -Force  #for testing 
Restart-Service WinRM

那么我如何在不安装在远程计算机上的情况下执行外部模块命令呢? :/

【问题讨论】:

    标签: powershell updates remote-access execute winrm


    【解决方案1】:

    您可以从 shared location 导入模块(如果您的执行策略允许):

    Invoke-Command -ComputerName edv-004 -ScriptBlock {
      Import-Module \\server\share\YourModule
      Get-WUHistory
    } -Credential $cred
    

    但是你不能在不先导入模块的情况下从模块运行函数。

    【讨论】:

    • 这是个好主意,但不是我想要的...问题是我必须在每台计算机上执行执行策略:(
    • 如果您正在管理一个域并且通过注册表设置来设置执行策略会更容易,那么您可以使用此方法首先更新它:blogs.technet.com/b/operationsguy/archive/2011/04/21/…
    • 无论如何您都需要更改每个主机上的执行策略,因为默认值为Restricted。在启用 PSRemoting 的同时进行。
    猜你喜欢
    • 2019-04-11
    • 2023-03-12
    • 2010-12-15
    • 2011-11-23
    • 2017-03-20
    • 2017-01-29
    • 2012-03-21
    • 2011-08-12
    相关资源
    最近更新 更多