【问题标题】:PowerShell run script locally - Execute actions remotePowerShell 在本地运行脚本 - 远程执行操作
【发布时间】:2013-11-28 16:45:18
【问题描述】:

我需要一些帮助来执行远程脚本。我想要做的是连接到远程服务器并使用命令导入 .psm1。 问题似乎是 Powershell 在执行脚本的计算机上的用户路径中查找文件。我还将脚本复制到网络共享,但在那里执行的结果相同。

脚本.ps1

Invoke-Command -ComputerName servername.domain.com -Credential domain\admin.account –ScriptBlock {

#!! Microsoft MDT Section !!
# Import Microsoft MDT commands
#Add-PSSnapIn Microsoft.BDD.PSSnapIn
Import-Module –name .\MDTDB.psm1
#
#Connect to MDT database
Connect-MDTDatabase –sqlServer servername.domain.com –database DATABASE

#Import file with settings to add servers to database
#$vms = Import-Csv InputServerInfo.csv
$machines = Import-Csv .\InputServerInfo.csv
#
# Script commands goes here
}

错误信息:

The specified module '.\MDTDB.psm1' was not loaded because no valid module file was found in any module directory.
    + CategoryInfo          : ResourceUnavailable: (.\MDTDB.psm1:String) [Import-Module], FileNotFoundException
    + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
    + PSComputerName        : servername.domain.com

The term 'Connect-MDTDatabase' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    + CategoryInfo          : ObjectNotFound: (Connect-MDTDatabase:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
    + PSComputerName        : servername.domain.com

Could not find file 'C:\Users\admin.account\Documents\InputServerInfo.csv'.
    + CategoryInfo          : OpenError: (:) [Import-Csv], FileNotFoundException
    + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ImportCsvCommand
    + PSComputerName        : servername.domain.com

我尝试使用 Invoke-Command 和 New-PSSession,但我无法让它工作。我还记得 Dot Source 在执行之前将脚本加载到内存中的一些内容,但我不确定。

我真的会为这个问题提供一些建议。

【问题讨论】:

    标签: powershell powershell-remoting


    【解决方案1】:

    为远程计算机可访问的模块提供完整的 unc 路径

    Import-Module \\server\share\MDTDB.psm1
    

    【讨论】:

    • 当我尝试得到以下错误时:get-itemproperty:找不到路径'H:\-',因为它不存在。在 \\servername\share\MDTDB.psm1:130 char:26 + $mdtProperties = get-itemproperty $drivePath + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~ + CategoryInfo : ObjectNotFound: (H:\-:String) [Get-ItemProperty], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemPropertyCommand 似乎 Powershell 以某种方式希望我的本地映射 H:\ 驱动器存在于服务器也是?
    • 你已经通过了第一个障碍,因为它现在正在加载模块。 $drivepath 是如何设置的?您需要找到将 h:\ 传递给它的内容
    猜你喜欢
    • 1970-01-01
    • 2013-10-15
    • 2017-09-30
    • 1970-01-01
    • 2022-10-12
    • 2018-08-14
    • 1970-01-01
    • 2020-11-12
    • 1970-01-01
    相关资源
    最近更新 更多