【发布时间】:2020-09-07 16:06:03
【问题描述】:
在过去的几周里,我在运行 powershell 脚本时开始遇到最奇怪的问题……
他们都显示这个错误信息,好像文件不存在一样?
下面的完整控制台输出显示我通过 Get-Content cmdlet 证明了该文件确实存在...
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Try the new cross-platform PowerShell https://aka.ms/pscore6
PS H:\> C:
PS C:\> cd C:\Users\Public
PS C:\Users\Public> .\test3.ps1
.\test3.ps1 : The term '.\test3.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.
At line:1 char:1
+ .\test3.ps1
+ ~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (.\test3.ps1:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
PS C:\Users\Public> get-content test3.ps1
Get-Process | Out-GridView -Wait
PS C:\Users\Public> .\test3.ps1
.\test3.ps1 : The term '.\test3.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.
At line:1 char:1
+ .\test3.ps1
+ ~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (.\test3.ps1:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
PS C:\Users\Public>
我已经将我的执行策略设置为无限制。
任何建议
操作系统 Windows 10 企业版 版本 1909(操作系统内部版本 18363.959)
PS C:\Users\Public>> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.18362.752
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.18362.752
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
更新:
当设置为在 PowerShell 版本 2 中运行时,我可以获得不同的结果...
PS C:\Users\Public> powershell -version 2 -ExecutionPolicy bypass -file test3.ps1
Out-GridView : A parameter cannot be found that matches parameter name 'Wait'.
At C:\Users\Public\test3.ps1:1 char:33
+ Get-Process | Out-GridView -Wait <<<<
+ CategoryInfo : InvalidArgument: (:) [Out-GridView], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.OutGridViewCommand
PS C:\Users\Public> powershell -version 3 -ExecutionPolicy bypass -file test3.ps1
The term 'C:\Users\Public\test3.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: (C:\Users\Public\test3.ps1:String) [], ParentContainsErrorRecordExceptio
n
+ FullyQualifiedErrorId : CommandNotFoundException
PS C:\Users\Public>
大概 -Wait 参数在版本 2 中不可用。我现在可以使用它在版本 2 中运行脚本,但显然这是非常有限的功能。
更新 2:
我完全忘记了我在今年早些时候遇到了这个确切的问题;甚至在超级用户上发布了一个关于它的问题…… https://superuser.com/questions/1536086/powershell-wont-run-ps1-files
我已经标记了一个答案,我认为这是由于从使用 CAG 到 CheckPointVPN 的过渡(早在 3 月份远程工作成为一项长期工作时);进办公室直接连上网络后就莫名其妙地解决了?
不过这一次;直接连接到办公室网络无法解决问题。
最近只有其他类似的东西发生了变化;是在远程工作时 IT 设置对 Windows 密码 30 天重置的支持(之前它不起作用,因此之前在锁定期间禁用了 30 天重置)。我在八月份休假前远程重置了密码,回来后无法运行 powershell 脚本?
我曾尝试在办公室重设密码,但仍未解决问题。
我还有什么可以检查的??
【问题讨论】:
-
如果你使用完全限定的路径,它可以工作吗?
-
Get-Item .\test3.ps1 |% FullName是否返回C:\users\public\test3.ps1? -
@MathiasR.Jessen 是的,它返回的正是那个
-
@OwlsSleeping 否,结果相同,只是错误消息显示完整路径
-
您可以尝试在提升的命令提示符下运行
dism /online /cleanup-image /restorehealth,然后运行sfc /scannow来修复所有Windows OS 系统文件。
标签: powershell powershell-cmdlet