gpt4 book ai didi

Powershell ps1 文件 "is not recognized as a cmdlet, function, operable program, or script file."

转载 作者:行者123 更新时间:2023-12-03 11:56:37 24 4
gpt4 key购买 nike

刚才做了一个Powershell函数,保存到ps1文件中。
但是,当我尝试从 powershell 中执行它时,它不会运行。

我已经通过输入以下命令更改为运行未签名代码的设置:

set-executionpolicy remotesigned

功能是这样的:
Function listAllPaths([string]$fromFolder, [string]$filter, [string]$printfile){
Get-ChildItem -Path $fromFolder -Include $filter -Recurse -Force -Name > $printfile
}

它所做的是创建一个文本文件,其中列出了某个文件的所有路径。
我把它直接放在 c:\下,并将文件命名为 listAllPaths,与函数相同。

当我在 Powershell 中输入以下命令时:
PS> listAllPaths.ps1 c:\ *.pdf testingPDF.txt

我收到一条错误消息:

The term 'listAllPaths.ps1' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.



我已经尝试了几件事,老实说我不知道​​如何让它发挥作用?
我期望的是在给定的路径上创建一个文件,在本例中为 c:\。该文件名为 testingPDF.txt,内容是生成的 this.txt。

有人可以告诉我我在这里忘记了什么。

不,谷歌不会回答所有问题。已经试过了。如果我还没有尝试过在线搜索引擎,我就不会来这里问它。

最佳答案

这是环境路径不包含当前目录的许多平台的典型错误。因此,当您执行脚本(或命令或程序等)时,除了当前/工作目录之外,您的运行时环境看起来无处不在。

尝试

PS> .\listAllPaths.ps1 c:\ *.pdf testingPDF.txt

编辑:
阅读您的评论后,我建议您尝试一下。我还没有真正验证过你的 PS 脚本的逻辑。我只是想让你的脚本先执行。

尝试如下编辑您的脚本,并按上述方式执行。
Function listAllPaths([string]$fromFolder, [string]$filter, [string]$printfile){
Get-ChildItem -Path $fromFolder -Include $filter -Recurse -Force -Name > $printfile
}

listAllPaths

关于Powershell ps1 文件 "is not recognized as a cmdlet, function, operable program, or script file.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/762927/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com