[英]Using PowerShell ISE, if I open it by clicking on the .ps1 file it works, if use File->Open the same file gets errors
使用 PowerShell ISE,
如果我通過單擊 Windows 資源管理器中的 .ps1 文件打開下面的文件,當我使用綠色箭頭運行它時,它可以工作。 如果使用 File->Open the same file,當我單擊綠色箭頭時會出現錯誤。
如果我通過單擊打開它然后加載它,那很好,所以當我雙擊時某些東西(路徑?)正在設置,這不是另一種方式......
讓我澄清一下...如果我以一種方式打開文件(雙擊)它“編譯”很好,如果我以另一種方式打開它(文件->打開)完全相同的文件不會“編譯”。 我希望弄清楚這是為什么。 我的猜測是,它用於查找所需其他文件的路徑變量通過兩種打開方法設置不同(因為許多錯誤都“未找到”)
這是文件的代碼(它基本上只是調用了一堆外部的東西)
###########################################################
# RUN (hit the green arrow) TO INITIALIZE
############################################################
############################################################
# set defaults
$path = Get-Location
$ProjectName = $path.Path
$ProjectName = $ProjectName.Replace("D:\Project Files\", "")
$ProjectName = $ProjectName.Replace("\_Powershell", "")
$ProjectName = $ProjectName.Replace("\WebApp", "")
$ProjectName = $ProjectName.Replace("\Webapp", "")
write-host "Project Name found: " $ProjectName
$Project = $ProjectName
$RootPath = 'D:\Project Files\' + $Project + '\WebApp'
$ModulePath = 'D:\Project Files\' + $Project + '\WebApp\_PowerShell'
############################################################
# call this one to remove and reload the modules (if modules are modified)
function ReloadTemplates {
RemoveLoadedModule WPK
RemoveLoadedModule JcdcAddFileToProjectGui
RemoveLoadedModule JcdcEoAndMap
RemoveLoadedModule JcdcEoAndMapGui
RemoveLoadedModule JcdcDal
RemoveLoadedModule JcdcDalGui
RemoveLoadedModule JcdcBll
RemoveLoadedModule JcdcBLLGui
RemoveLoadedModule JcdcView
RemoveLoadedModule JcdcViewGui
RemoveLoadedModule JcdcController
RemoveLoadedModule JcdcControllerGui
RemoveLoadedModule JcdcAutoMap
RemoveLoadedModule JcdcAutoMapGui
RemoveLoadedModule JcdcReport
RemoveLoadedModule JcdcReportGui
RemoveLoadedModule JcdcCv
RemoveLoadedModule JcdcCvGui
RemoveLoadedModule ReadWriteXML
# load the Templates
LoadTemplates
}
function RemoveLoadedModule {
param($PathAndName)
if(Get-Module -name $PathAndName)
{
Remove-Module $PathAndName
}
}
############################################################
# call this one to load the modules
function LoadTemplates {
Import-Module WPK
Import-Module "$ModulePath\JcdcAddFileToProjectGui.psm1"
Import-Module "$ModulePath\JcdcEoAndMap.psm1"
Import-Module "$ModulePath\JcdcEoAndMapGui.psm1"
Import-Module "$ModulePath\JcdcDal.psm1"
Import-Module "$ModulePath\JcdcDalGui.psm1"
Import-Module "$ModulePath\JcdcBll.psm1"
Import-Module "$ModulePath\JcdcBLLGui.psm1"
Import-Module "$ModulePath\JcdcView.psm1"
Import-Module "$ModulePath\JcdcViewGui.psm1"
Import-Module "$ModulePath\JcdcController.psm1"
Import-Module "$ModulePath\JcdcControllerGui.psm1"
Import-Module "$ModulePath\JcdcAutoMap.psm1"
Import-Module "$ModulePath\JcdcAutoMapGui.psm1"
Import-Module "$ModulePath\JcdcReport.psm1"
Import-Module "$ModulePath\JcdcReportGui.psm1"
Import-Module "$ModulePath\JcdcCv.psm1"
Import-Module "$ModulePath\JcdcCvGui.psm1"
Import-Module "$ModulePath\ReadWriteXML.psm1"
Initialize-AddFileToProject $RootPath $Project
Initialize-EOMAPGui $RootPath $Project
Initialize-DALGui $RootPath $Project
Initialize-BLLGui $RootPath $Project
Initialize-ViewGui $RootPath $Project
Initialize-ControllerGui $RootPath $Project
Initialize-AutoMapGui $RootPath $Project
Initialize-ReportGui $RootPath $Project
Initialize-CVGui $RootPath $Project
Initialize-XMLArguments
}
############################################################
# load the Templates on Run (when green arrow is clicked)
ReloadTemplates
############################################################
# Type one of the following in the command window
#Select-A + <tab> = Select-AddFileToProject -To create file to add to project
#Select-B + <tab> = Select-BLLWhatToBuild -To add a BLL
#Select-C + <tab> = Select-ControllerWhatToBuild -To add a Controller
#Select-D + <tab> = Select-DALWhatToBuild -To add a DAL
#Select-E + <tab> = Select-EoAndMapBuild -To add a Eo and Map
#Select-M + <tab> = Select-MapBuild -To add a AutoMap
#Select-R + <tab> = Select-ReportWhatToBuild -To add a Report
#Select-Vi + <tab> = Select-ViewWhatToBuild -To add a View
#Select-Vm + <tab> = Select-VmCvOrExcel -To add a CV, VM, D:\PowerShellTemplatesArgsor Excel
這是錯誤
PS U:\> D:\Project Files\CIS3G\Webapp\_Powershell\ManualRun.ps1
Project Name found: U:\
Import-Module : The specified module 'D:\Project Files\U:\\WebApp\_PowerShell\JcdcAddFileToProjectGui.psm1' was not loaded because no valid module file was found in any module direc
tory.
At D:\Project Files\CIS3G\Webapp\_Powershell\ManualRun.ps1:61 char:18
+ Import-Module <<<< "$ModulePath\JcdcAddFileToProjectGui.psm1"
+ CategoryInfo : ResourceUnavailable: (D:\Project File...ProjectGui.psm1:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
Import-Module : The specified module 'D:\Project Files\U:\\WebApp\_PowerShell\JcdcEoAndMap.psm1' was not loaded because no valid module file was found in any module directory.
At D:\Project Files\CIS3G\Webapp\_Powershell\ManualRun.ps1:62 char:18
+ Import-Module <<<< "$ModulePath\JcdcEoAndMap.psm1"
+ CategoryInfo : ResourceUnavailable: (D:\Project File...dcEoAndMap.psm1:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
Import-Module : The specified module 'D:\Project Files\U:\\WebApp\_PowerShell\JcdcEoAndMapGui.psm1' was not loaded because no valid module file was found in any module directory.
At D:\Project Files\CIS3G\Webapp\_Powershell\ManualRun.ps1:63 char:18
+ Import-Module <<<< "$ModulePath\JcdcEoAndMapGui.psm1"
+ CategoryInfo : ResourceUnavailable: (D:\Project File...oAndMapGui.psm1:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
Import-Module : The specified module 'D:\Project Files\U:\\WebApp\_PowerShell\JcdcDal.psm1' was not loaded because no valid module file was found in any module directory.
At D:\Project Files\CIS3G\Webapp\_Powershell\ManualRun.ps1:64 char:18
+ Import-Module <<<< "$ModulePath\JcdcDal.psm1"
+ CategoryInfo : ResourceUnavailable: (D:\Project File...ll\JcdcDal.psm1:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
Import-Module : The specified module 'D:\Project Files\U:\\WebApp\_PowerShell\JcdcDalGui.psm1' was not loaded because no valid module file was found in any module directory.
At D:\Project Files\CIS3G\Webapp\_Powershell\ManualRun.ps1:65 char:18
+ Import-Module <<<< "$ModulePath\JcdcDalGui.psm1"
+ CategoryInfo : ResourceUnavailable: (D:\Project File...JcdcDalGui.psm1:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
Import-Module : The specified module 'D:\Project Files\U:\\WebApp\_PowerShell\JcdcBll.psm1' was not loaded because no valid module file was found in any module directory.
At D:\Project Files\CIS3G\Webapp\_Powershell\ManualRun.ps1:66 char:18
+ Import-Module <<<< "$ModulePath\JcdcBll.psm1"
+ CategoryInfo : ResourceUnavailable: (D:\Project File...ll\JcdcBll.psm1:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
Import-Module : The specified module 'D:\Project Files\U:\\WebApp\_PowerShell\JcdcBLLGui.psm1' was not loaded because no valid module file was found in any module directory.
At D:\Project Files\CIS3G\Webapp\_Powershell\ManualRun.ps1:67 char:18
+ Import-Module <<<< "$ModulePath\JcdcBLLGui.psm1"
+ CategoryInfo : ResourceUnavailable: (D:\Project File...JcdcBLLGui.psm1:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
Import-Module : The specified module 'D:\Project Files\U:\\WebApp\_PowerShell\JcdcView.psm1' was not loaded because no valid module file was found in any module directory.
At D:\Project Files\CIS3G\Webapp\_Powershell\ManualRun.ps1:68 char:18
+ Import-Module <<<< "$ModulePath\JcdcView.psm1"
+ CategoryInfo : ResourceUnavailable: (D:\Project File...l\JcdcView.psm1:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
Import-Module : The specified module 'D:\Project Files\U:\\WebApp\_PowerShell\JcdcViewGui.psm1' was not loaded because no valid module file was found in any module directory.
At D:\Project Files\CIS3G\Webapp\_Powershell\ManualRun.ps1:69 char:18
+ Import-Module <<<< "$ModulePath\JcdcViewGui.psm1"
+ CategoryInfo : ResourceUnavailable: (D:\Project File...cdcViewGui.psm1:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
Import-Module : The specified module 'D:\Project Files\U:\\WebApp\_PowerShell\JcdcController.psm1' was not loaded because no valid module file was found in any module directory.
At D:\Project Files\CIS3G\Webapp\_Powershell\ManualRun.ps1:70 char:18
+ Import-Module <<<< "$ModulePath\JcdcController.psm1"
+ CategoryInfo : ResourceUnavailable: (D:\Project File...Controller.psm1:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
Import-Module : The specified module 'D:\Project Files\U:\\WebApp\_PowerShell\JcdcControllerGui.psm1' was not loaded because no valid module file was found in any module directory.
At D:\Project Files\CIS3G\Webapp\_Powershell\ManualRun.ps1:71 char:18
+ Import-Module <<<< "$ModulePath\JcdcControllerGui.psm1"
+ CategoryInfo : ResourceUnavailable: (D:\Project File...trollerGui.psm1:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
Import-Module : The specified module 'D:\Project Files\U:\\WebApp\_PowerShell\JcdcAutoMap.psm1' was not loaded because no valid module file was found in any module directory.
At D:\Project Files\CIS3G\Webapp\_Powershell\ManualRun.ps1:72 char:18
+ Import-Module <<<< "$ModulePath\JcdcAutoMap.psm1"
+ CategoryInfo : ResourceUnavailable: (D:\Project File...cdcAutoMap.psm1:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
Import-Module : The specified module 'D:\Project Files\U:\\WebApp\_PowerShell\JcdcAutoMapGui.psm1' was not loaded because no valid module file was found in any module directory.
At D:\Project Files\CIS3G\Webapp\_Powershell\ManualRun.ps1:73 char:18
+ Import-Module <<<< "$ModulePath\JcdcAutoMapGui.psm1"
+ CategoryInfo : ResourceUnavailable: (D:\Project File...AutoMapGui.psm1:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
Import-Module : The specified module 'D:\Project Files\U:\\WebApp\_PowerShell\JcdcReport.psm1' was not loaded because no valid module file was found in any module directory.
At D:\Project Files\CIS3G\Webapp\_Powershell\ManualRun.ps1:74 char:18
+ Import-Module <<<< "$ModulePath\JcdcReport.psm1"
+ CategoryInfo : ResourceUnavailable: (D:\Project File...JcdcReport.psm1:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
Import-Module : The specified module 'D:\Project Files\U:\\WebApp\_PowerShell\JcdcReportGui.psm1' was not loaded because no valid module file was found in any module directory.
At D:\Project Files\CIS3G\Webapp\_Powershell\ManualRun.ps1:75 char:18
+ Import-Module <<<< "$ModulePath\JcdcReportGui.psm1"
+ CategoryInfo : ResourceUnavailable: (D:\Project File...cReportGui.psm1:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
Import-Module : The specified module 'D:\Project Files\U:\\WebApp\_PowerShell\JcdcCv.psm1' was not loaded because no valid module file was found in any module directory.
At D:\Project Files\CIS3G\Webapp\_Powershell\ManualRun.ps1:76 char:18
+ Import-Module <<<< "$ModulePath\JcdcCv.psm1"
+ CategoryInfo : ResourceUnavailable: (D:\Project File...ell\JcdcCv.psm1:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
Import-Module : The specified module 'D:\Project Files\U:\\WebApp\_PowerShell\JcdcCvGui.psm1' was not loaded because no valid module file was found in any module directory.
At D:\Project Files\CIS3G\Webapp\_Powershell\ManualRun.ps1:77 char:18
+ Import-Module <<<< "$ModulePath\JcdcCvGui.psm1"
+ CategoryInfo : ResourceUnavailable: (D:\Project File...\JcdcCvGui.psm1:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
Import-Module : The specified module 'D:\Project Files\U:\\WebApp\_PowerShell\ReadWriteXML.psm1' was not loaded because no valid module file was found in any module directory.
At D:\Project Files\CIS3G\Webapp\_Powershell\ManualRun.ps1:78 char:18
+ Import-Module <<<< "$ModulePath\ReadWriteXML.psm1"
+ CategoryInfo : ResourceUnavailable: (D:\Project File...adWriteXML.psm1:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
The term 'Initialize-AddFileToProject' 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 includ
ed, verify that the path is correct and try again.
At D:\Project Files\CIS3G\Webapp\_Powershell\ManualRun.ps1:80 char:29
+ Initialize-AddFileToProject <<<< $RootPath $Project
+ CategoryInfo : ObjectNotFound: (Initialize-AddFileToProject:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
The term 'Initialize-EOMAPGui' 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, veri
fy that the path is correct and try again.
At D:\Project Files\CIS3G\Webapp\_Powershell\ManualRun.ps1:81 char:24
+ Initialize-EOMAPGui <<<< $RootPath $Project
+ CategoryInfo : ObjectNotFound: (Initialize-EOMAPGui:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
The term 'Initialize-DALGui' 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.
At D:\Project Files\CIS3G\Webapp\_Powershell\ManualRun.ps1:82 char:22
+ Initialize-DALGui <<<< $RootPath $Project
+ CategoryInfo : ObjectNotFound: (Initialize-DALGui:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
The term 'Initialize-BLLGui' 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.
At D:\Project Files\CIS3G\Webapp\_Powershell\ManualRun.ps1:83 char:22
+ Initialize-BLLGui <<<< $RootPath $Project
+ CategoryInfo : ObjectNotFound: (Initialize-BLLGui:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
The term 'Initialize-ViewGui' 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, verif
y that the path is correct and try again.
At D:\Project Files\CIS3G\Webapp\_Powershell\ManualRun.ps1:84 char:23
+ Initialize-ViewGui <<<< $RootPath $Project
+ CategoryInfo : ObjectNotFound: (Initialize-ViewGui:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
The term 'Initialize-ControllerGui' 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.
At D:\Project Files\CIS3G\Webapp\_Powershell\ManualRun.ps1:85 char:29
+ Initialize-ControllerGui <<<< $RootPath $Project
+ CategoryInfo : ObjectNotFound: (Initialize-ControllerGui:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
The term 'Initialize-AutoMapGui' 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, ve
rify that the path is correct and try again.
At D:\Project Files\CIS3G\Webapp\_Powershell\ManualRun.ps1:86 char:26
+ Initialize-AutoMapGui <<<< $RootPath $Project
+ CategoryInfo : ObjectNotFound: (Initialize-AutoMapGui:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
The term 'Initialize-ReportGui' 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, ver
ify that the path is correct and try again.
At D:\Project Files\CIS3G\Webapp\_Powershell\ManualRun.ps1:87 char:25
+ Initialize-ReportGui <<<< $RootPath $Project
+ CategoryInfo : ObjectNotFound: (Initialize-ReportGui:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
The term 'Initialize-CVGui' 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.
At D:\Project Files\CIS3G\Webapp\_Powershell\ManualRun.ps1:88 char:21
+ Initialize-CVGui <<<< $RootPath $Project
+ CategoryInfo : ObjectNotFound: (Initialize-CVGui:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
The term 'Initialize-XMLArguments' 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.
At D:\Project Files\CIS3G\Webapp\_Powershell\ManualRun.ps1:89 char:28
+ Initialize-XMLArguments <<<<
+ CategoryInfo : ObjectNotFound: (Initialize-XMLArguments:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
當我打開文件時,有什么方法可以設置路徑,這樣就不會發生這種情況?
每個項目都有這個文件夾和文件的不同版本,因為它用於生成項目特定的代碼......所以解決方案也需要適用於該場景。
不確定是否理解您的問題,但如果您想真正從腳本的位置開始,您可以使用$MyInvocation
自動 var。
$MyInvocation.MyCommand.Path
將為您提供腳本的路徑(.PS1 文件),然后您可以使用Split-Path
查找其目錄位置。 我認為在您的情況下Get-Location
並不總是返回您考慮的路徑。
$ModulePath = 'D:\Project Files\' + $Project + '\WebApp\_PowerShell'
Import-Module "$ModulePath\JcdcAddFileToProjectGui.psm1"
結果是
'D:\Project Files\U:\WebApp_PowerShell\JcdcAddFileToProjectGui.psm1'
Windows 不允許單個路徑中有多個驅動器號。 你的里面有 U:。 $Project = 'U:' 在評估$ModulePath
時。 這是 PowerShell 的起始工作目錄。
底線是您使用的是相對於腳本運行位置的路徑。 當您打開 powershell 時,它會從您的個人資料中設置起始位置,如果您的個人資料中沒有,則設置默認位置。 從文件啟動時,它使用該文件的位置。
您似乎在為每個項目硬編碼一組單獨的腳本 + 模塊等,但它們定義的名稱在項目范圍內是一致的。 我建議您對每個腳本中的模塊位置進行硬編碼。
我還建議您重新考慮這種編碼風格。 改變某些東西一定是一場噩夢。 如果您創建了一個加載單個模塊的腳本會怎樣。 然后為每個項目創建一個配置文件,並為您的腳本創建一個快捷方式,該腳本加載主腳本以及從配置文件中讀取的自定義部分。
模塊像腳本一樣位於同一目錄中? 如果它是真的,那么問題的原因在於當前的直接。 在開始或使用模塊之前添加以下行:
Set-Location "<...>"
並讓我們用您的腳本輸入文件夾的路徑,而不是 <...>。
我個人喜歡使用默認的 Var $PSScriptRoot
在你的情況下,我會啟動腳本Set-Location $PSScripRoot
您可以在$path = Get-Location
前面添加我的行,或者您可以完全消除 $path 行並只使用 $PSScriptRoot 行,但是您還需要在整個腳本中將 $path 變量替換為 $PSScriptRoot
正如其他人所說,當您雙擊腳本時,如果您通過 ISE 手動打開,Powershell 將使用該路徑,但它不會(它使用您在 ISE 中設置的任何最后一條路徑)Set-Location $PSScriptRoot 行所做的是采用本地路徑你的腳本總是不管你選擇如何啟動它
我希望這有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.