Powershell找不到内置cmdlet
在运行我的脚本之一时,Powershell找不到内置cmdlet: 我的代码:Powershell找不到内置cmdlet,powershell,Powershell,在运行我的脚本之一时,Powershell找不到内置cmdlet: 我的代码: # (Some code which I think is irrelevant but runs fine) Then... # Load a third party assembly, which in turn loads other assemblies # so I have to change the Powershell process' directory so that # the other
# (Some code which I think is irrelevant but runs fine) Then...
# Load a third party assembly, which in turn loads other assemblies
# so I have to change the Powershell process' directory so that
# the other assemblies are visible
[System.IO.Directory]::SetCurrentDirectory($ThirdPartyInstallPath)
Add-Type -Path "$ThirdPartyInstallPath\ThirdPatyAssembly.dll"
# (1) Deliberately commented out call to Get-Item - so it does nothing
# $myFileInfo = Get-Item -Path "C:\My Path\File I know exists.txt"
# (2) call to third party API
ThirdPartyFunction()
# (3) Second call to Get-Item
# (*) This is the code that fails
$myFileInfo = Get-Item -Path "C:\My Path\File I know exists.txt"
如果我使用powershell.exe(但奇怪的是,不是ISE)运行此操作,则错误消息为:
The term 'Get-Item' 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 C:\Projects\Test Host\Test_Script.ps1:21 char:11
+ CategoryInfo : ObjectNotFound: (Get-Item:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
但是,脚本在以下情况下运行良好:
- 当我在Powershell ISE中运行相同的脚本时(通过加载并按下绿色的运行按钮),没有任何更改,一切都正常运行
- 回到在powershell.exe中运行,如果我取消注释(1),因此在第三方API调用之前包含一个Get Item调用,那么它可以正常工作
- 在powershell.exe中运行时,如果我注释掉第三方API调用(2),那么它工作正常[无论(1)是注释的还是未注释的]
$PSModulePath
和系统路径没有改变,它们没有改变,它们的值符合Frode下面的注释
我试图通过使用Sysinternal的过程监视器来查看“引擎盖下”发生了什么。这告诉我,在发生错误之前,Powershell:
- 首先搜索路径以查找
,Get Item.exe
等,但未找到任何内容Get Item.ps1
- 然后查看
下的$PSModulePath
文件,其中包括.psd1
C:\Windows\System32\windowsupershell\v1.0\Modules
- 它会发现
cmdlet是由Get-Item
Microsoft.Powershell.Management.psd1导出的
- 然后它从Microsoft.Powershell.Management.dll中读取
是什么导致了问题?如何诊断?如何解决它?如果出现任何问题,我将更新帖子以反映它们
$env:PSModulePath
返回了什么?它看起来应该类似于C:\Users\frode\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\
除非脚本或模块中有内容对其进行了修改。是的,它所说的内容与您的类似。我在问题中也包含了更多的信息