Powershell .ps1脚本无法运行/不存在?

时间:2020-09-07 16:06:03

标签: powershell cmdlet

在过去的几周中,我开始遇到运行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脚本?

我曾在办公室尝试过重设密码,但是并没有解决问题。

我为其他需要检查的东西卡住了?

2 个答案:

答案 0 :(得分:0)

为了不让这个问题无答案,请在这里以我的评论作为答案

看到您已经尝试过的内容,我认为系统中的dll之一存在问题。由于PowerShell是Windows 10的一部分,因此据我所知您无法卸载/重新安装它。

您可以尝试修复丢失或损坏的系统文件。

打开提升权限的命令提示符并运行

dism /online /cleanup-image /restorehealth

这将使用Windows Update提供修复损坏所需的文件,因此需要Internet连接。如果这是不可能的,则可以使用switch指定另一个源
/Source:<PathToFreshWindowsFiles>

完成后,键入

sfc /scannow

扫描所有受保护的系统文件,并将损坏的文件替换为位于压缩文件夹“%WinDir%\ System32 \ dllcache”中的缓存副本

两个动作都需要花费一些时间才能完成。

完成后,重新启动计算机。

希望这会让您重回正轨。让我们知道是否有帮助。

答案 1 :(得分:0)

此问题归因于AppLocker阻止脚本运行。

为了检查是否是引起问题的原因,请打开“事件查看器”并导航至

> Applications and Services Logs
> > Windows
> > > AppLocker
> > > > MSI and Script

查找任何错误日志条目,并且应该声明类似

%OSDRIVE%\USERS\PUBLIC\TEST3.PS1 was prevented from running.

就我而言,此更改是2个月前由IT安全部门应用的;从那以后,IT服务台一直在缓慢处理投诉,并将选定的文件夹目录列入白名单以进行解决。

由于某种原因,我的同事本来应该受到的变化并没有受到影响;这使我相信这是我的机器或用户的问题。

相关问题