简体   繁体   English

使用 PowerShell ISE,如果我通过单击 .ps1 文件打开它,它可以工作,如果使用 File->Open 相同的文件会出错

[英]Using PowerShell ISE, if I open it by clicking on the .ps1 file it works, if use File->Open the same file gets errors

Using PowerShell ISE,使用 PowerShell ISE,

If I open the file below by clicking on the .ps1 file in windows explorer, when I run it with the green arrow, it works.如果我通过单击 Windows 资源管理器中的 .ps1 文件打开下面的文件,当我使用绿色箭头运行它时,它可以工作。 If use File->Open the same file, when I click the green arrow I get errors.如果使用 File->Open the same file,当我单击绿色箭头时会出现错误。

If I open it by clicking and then load it, it's fine, so something (the path?) is getting set when I double click that is not the other way...如果我通过单击打开它然后加载它,那很好,所以当我双击时某些东西(路径?)正在设置,这不是另一种方式......

Let me clarify... If I open the file one way(double click) it 'compiles' fine, if I open it the other way (file->Open) the exact same file does not 'compile'.让我澄清一下...如果我以一种方式打开文件(双击)它“编译”很好,如果我以另一种方式打开它(文件->打开)完全相同的文件不会“编译”。 I'm hoping to figure out why this is.我希望弄清楚这是为什么。 My guess is that the path variable it uses to find the other files needed is set different via the two methods of opening (because many of the errors are 'not found')我的猜测是,它用于查找所需其他文件的路径变量通过两种打开方法设置不同(因为许多错误都“未找到”)

here is the code of the file (it basically just calls a bunch of external stuff)这是文件的代码(它基本上只是调用了一堆外部的东西)

###########################################################
# 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

Here are the errors这是错误

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

Is there some way to set the path when I do a file open so this does not occur?当我打开文件时,有什么方法可以设置路径,这样就不会发生这种情况?

There are different versions of this folder and file for each project, as it's used to generate project specific code... so the solution needs to work for that scenerio too please.每个项目都有这个文件夹和文件的不同版本,因为它用于生成项目特定的代码......所以解决方案也需要适用于该场景。

Not sure to understand your question, but if you want to really begin at the location of your script, you can use $MyInvocation automatic var.不确定是否理解您的问题,但如果您想真正从脚本的位置开始,您可以使用$MyInvocation自动 var。

$MyInvocation.MyCommand.Path will give you the path of your script (.PS1 file) and then you can use Split-Path to find its directory location. $MyInvocation.MyCommand.Path将为您提供脚本的路径(.PS1 文件),然后您可以使用Split-Path查找其目录位置。 I think that in your case Get-Location does not always return the path you think about.我认为在您的情况下Get-Location并不总是返回您考虑的路径。

$ModulePath = 'D:\Project Files\' + $Project + '\WebApp\_PowerShell'
Import-Module "$ModulePath\JcdcAddFileToProjectGui.psm1"

Results in结果是

'D:\Project Files\U:\WebApp_PowerShell\JcdcAddFileToProjectGui.psm1' 

Windows does not allow more than one drive letter in a single path. Windows 不允许单个路径中有多个驱动器号。 Yours has U: in it.你的里面有 U:​​。 $Project = 'U:' at the time $ModulePath was evaluated. $Project = 'U:' 在评估$ModulePath时。 That is your starting working directory for PowerShell.这是 PowerShell 的起始工作目录。

Bottom line is that you're using paths relative to where the script is run.底线是您使用的是相对于脚本运行位置的路径。 When you open powershell, it sets starting location from your profile, or default location if you don't have one in your profile.当您打开 powershell 时,它会从您的个人资料中设置起始位置,如果您的个人资料中没有,则设置默认位置。 When you launch from a file, it uses that file's location.从文件启动时,它使用该文件的位置。

You seem to be hard coding a separate set of script + modules, etc., for each project, but they have defined names that are consistent across the range of projects.您似乎在为每个项目硬编码一组单独的脚本 + 模块等,但它们定义的名称在项目范围内是一致的。 I suggest you hardcode the module locations in each script.我建议您对每个脚本中的模块位置进行硬编码。

I'd also suggest that you reconsider this style of coding.我还建议您重新考虑这种编码风格。 It must be a nightmare to change something.改变某些东西一定是一场噩梦。 What if you created a single script that loads a single module.如果您创建了一个加载单个模块的脚本会怎样。 Then create a config file for each project and a shortcut to your script that loads the main script with the customized parts read from the config file.然后为每个项目创建一个配置文件,并为您的脚本创建一个快捷方式,该脚本加载主脚本以及从配置文件中读取的自定义部分。

The modules lay in same directory like a script?模块像脚本一样位于同一目录中? If it true, then reason of issue is in curent directoty.如果它是真的,那么问题的原因在于当前的直接。 Add folowing line in begin or before you operate with modules:在开始或使用模块之前添加以下行:

Set-Location "<...>"

and lets type path to the folder with your script instead <...>.并让我们用您的脚本输入文件夹的路径,而不是 <...>。

I personally like using the default Var $PSScriptRoot我个人喜欢使用默认的 Var $PSScriptRoot

In your case I would have started the script在你的情况下,我会启动脚本
Set-Location $PSScripRoot

this will set the location to where ever your script is this should make it so either a double click or an open file location and press play will excute your script这会将位置设置为您的脚本所在的位置,因此双击或打开文件位置并按播放将执行您的脚本

you can either add my line in front of $path = Get-Location or you can eliminate the $path line entirely and just use the $PSScriptRoot line but then you also need to replace the $path variable with $PSScriptRoot in your entire script您可以在$path = Get-Location前面添加我的行,或者您可以完全消除 $path 行并只使用 $PSScriptRoot 行,但是您还需要在整个脚本中将 $path 变量替换为 $PSScriptRoot

As others have said when you double click your script Powershell uses that path if you open manually via ISE it does not (it uses whatever the last path u set in ISE instead) what the line Set-Location $PSScriptRoot does is take the local path of your script always regardless how you choose to start it正如其他人所说,当您双击脚本时,如果您通过 ISE 手动打开,Powershell 将使用该路径,但它不会(它使用您在 ISE 中设置的任何最后一条路径)Set-Location $PSScriptRoot 行所做的是采用本地路径你的脚本总是不管你选择如何启动它

I hope this helps!我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何用cmd打开Powershell并使用.ps1文件 - How to open Powershell with cmd and use a .ps1 file 当我运行.ps1文件时,Powershell只会出错 - Powershell only errors out when i run the .ps1 file 如何从命令行在 Powershell ISE 中打开 ps1 脚本? - How can I open a ps1 script in Powershell ISE from the command line? 有没有办法通过双击 a.ps1 文件来使 PowerShell 脚本工作? - Is there a way to make a PowerShell script work by double clicking a .ps1 file? 脚本在 Powershell CLI 中有效,但在 ISE 中或作为 PS1 无效 - Script Works in Powershell CLI but not in ISE or as a PS1 ps1在ise中起作用,但在powershell或调度任务中不起作用 - ps1 works in ise but not in powershell or schedule task PowerShell 仅在控制台中运行而不作为 PS1 可执行文件时工作 - PowerShell works only when running in a console and not as a PS1 executable file 如何通过Java在PowerShell_ISE.exe中运行.ps1文件 - How to run a .ps1 file in PowerShell_ISE.exe through Java 右键单击 ps1 文件 &gt; 编辑:不再与 ISE 关联(卸载 Sapien Powershell studio 后) - Right click on ps1 file > Edit: Is no longer assosiated with ISE (After uninstalling Sapien Powershell studio) 为什么我的脚本在Powershell ISE中可以工作,而在.ps1文件中却不能工作? - Why does my script work in powershell ISE but not when it is in a .ps1 file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM