簡體   English   中英

PowerShell在本地運行腳本-遠程執行操作

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM