[英]PowerShell run script locally - Execute actions remote
我需要一些帮助来远程执行脚本。 我想做的是连接到远程服务器并使用命令导入.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有所了解,该文件在执行之前将脚本加载到了内存中,但是我不确定。
对于这件事,我真的会提出一些建议。
提供完整的unc路径到远程计算机可以访问的模块
Import-Module \\server\share\MDTDB.psm1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.