Powershell ps1文件"未被识别为cmdlet,函数,可操作程序或脚本文件."

Kdg*_*Dev 21 powershell

我刚刚创建了一个Powershell函数并将其保存到ps1文件中.但是,当我尝试从powershell中执行它时,它将无法运行.

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

set-executionpolicy remotesigned
Run Code Online (Sandbox Code Playgroud)

功能是这样的:

Function listAllPaths([string]$fromFolder, [string]$filter, [string]$printfile){
Get-ChildItem -Path $fromFolder -Include $filter -Recurse -Force -Name > $printfile
}
Run Code Online (Sandbox Code Playgroud)

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

当我在Powershell中输入以下命令时:

PS> listAllPaths.ps1 c:\ *.pdf testingPDF.txt
Run Code Online (Sandbox Code Playgroud)

我收到一个错误说:

术语"listAllPaths.ps1"未被识别为cmdlet,函数,可操作程序或脚本文件.验证该术语,然后重试.

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

谁能告诉我,我忘了这里.

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

Cha*_*tan 11

这是您的环境路径不包含当前目录的许多平台上的典型错误.因此,当您执行脚本(或命令或程序等)时,您的运行时环境将在除当前/工作目录之外的任何位置.

尝试

PS> .\listAllPaths.ps1 c:\ *.pdf testingPDF.txt
Run Code Online (Sandbox Code Playgroud)

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

尝试按如下方式编辑脚本,然后按上述步骤执行.

Function listAllPaths([string]$fromFolder, [string]$filter, [string]$printfile){
Get-ChildItem -Path $fromFolder -Include $filter -Recurse -Force -Name > $printfile
}

listAllPaths
Run Code Online (Sandbox Code Playgroud)


Mat*_*ton 7

我可能会离开这里,但是你的脚本是在定义一个函数而不是执行它吗?也许你需要"源"脚本:

. .\listallpaths.ps1
Run Code Online (Sandbox Code Playgroud)

...所以现在定义了"listallpaths"函数.