1

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.

1 Answer 1

1

Give a full unc path to the module that is accessible to the remote computers

Import-Module \\server\share\MDTDB.psm1
Sign up to request clarification or add additional context in comments.

2 Comments

When I try that I get the following error: get-itemproperty : Cannot find path 'H:\-' because it does not exist. At \\servername\share\MDTDB.psm1:130 char:26 + $mdtProperties = get-itemproperty $drivePath + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (H:\-:String) [Get-ItemProperty], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemPropertyCommand Seems that Powershell somehow want my localy mapped H:\ drive to exist on the server also?
You got past the first hurdle since it is loading the module now. How is $drivepath being set? You need to find what is passing h:\ to it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.