我有以下腳本:Powershell:如何在同一時間使用Invoke-Command和點源?
.
|-- invoke-command.ps1
|-- no-dot-sourcing.ps1
`-- with-dot-sourcing.ps1
這裏是它們的內容:
調用-command.ps1
$scriptPath = Split-Path -Parent $PSCommandPath
Invoke-Command `
-ComputerName "myserver" `
-Credential "myusername" `
-FilePath "$scriptPath\with-dot-sourcing.ps1"
無點sourcing.ps1
function getMessage() {
return "This script just shows a message - executed on $env:COMPUTERNAME"
}
Write-Host (getMessage)
with-dot-sourcing.ps1
$scriptPath = Split-Path -Parent $PSCommandPath
. "$scriptPath\no-dot-sourcing.ps1"
問題
如果我打電話調用命令與-FilePath "$scriptPath\no-dot-sourcing.ps1"
一切都工作得很好。我需要叫它with-dot-sourcing.ps1
,其原因是我有一些常用的功能,我在其他腳本中使用。所以,一種解決方案是將所有內容都包含在一個巨大的腳本中,然後一切都可以工作,但我認爲這不是一個好的解決方案。
如果我運行該腳本invoke-command.ps1
我得到以下錯誤:
Cannot bind argument to parameter 'Path' because it is an empty string.
+ CategoryInfo : InvalidData: (:) [Split-Path], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.SplitPathCommand
+ PSComputerName : myserver
The term '\no-dot-sourcing.ps1' 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: (\no-dot-sourcing.ps1:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
+ PSComputerName : myserver
如果它的事項:我使用本地計算機上的Windows 7旗艦版SP1並在服務器上我的Windows Server 2012數據中心。
複製依賴文件比將所有內容合併到一個文件更麻煩,謝謝! –