PowerShell在本地运行脚本 - 执行远程操作

时间:2013-11-26 15:32:02

标签: powershell powershell-remoting

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

Script.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的内容,它在执行前将脚本加载到内存中,但我不确定这一点。

我真的会对这个问题提出一些指示。

1 个答案:

答案 0 :(得分:1)

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

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