简体   繁体   中英

PowerShell run script locally - Execute actions remote

I need some help to execute a script remote. What i want to do is to connect to a remote server and import a .psm1 with commands. The problem seems to be that Powershell looks for the files in the users path on the computer that is executing the script. I also copied the script to a network share but I get the same result executing there.

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
}

Error message:

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

I tried with both Invoke-Command and New-PSSession but I can't get it to work. I also rember something about Dot Source that loads the script in memory before executing but I'm not sure about this.

I really would appriciate some pointers for this matter.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM