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

在运行我的脚本之一时,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 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)是注释的还是未注释的]

我已经检查过,在第三方API调用期间,
$PSModulePath
和系统路径没有改变,它们没有改变,它们的值符合Frode下面的注释

我试图通过使用Sysinternal的过程监视器来查看“引擎盖下”发生了什么。这告诉我,在发生错误之前,Powershell:

  • 首先搜索路径以查找
    Get Item.exe
    Get Item.ps1
    等,但未找到任何内容

  • 然后查看
    $PSModulePath
    下的
    .psd1
    文件,其中包括
    C:\Windows\System32\windowsupershell\v1.0\Modules

  • 它会发现
    Get-Item
    cmdlet是由
    Microsoft.Powershell.Management.psd1导出的

  • 然后它从Microsoft.Powershell.Management.dll中读取

当我做出上述消除错误的更改时,我看到了完全相同的行为。因此,我不认为这是第三方DLL弄乱了设置并导致Powershell无法找到内置cmdlet的情况

我不知道这是否相关,但是:第三方程序集加载System.Management.Automation v1.0.0.0,这是根据.Net v2.0.50727编译的。这是因为第三方库本身就是Powershell主机(尽管我在代码中没有使用该功能)

我在web上的其他帖子中看到了“运行空间约束”的引用,但没有看到这个具体问题


是什么导致了问题?如何诊断?如何解决它?

如果出现任何问题,我将更新帖子以反映它们
$env:PSModulePath
返回了什么?它看起来应该类似于
C:\Users\frode\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\
除非脚本或模块中有内容对其进行了修改。是的,它所说的内容与您的类似。我在问题中也包含了更多的信息