Powershell ps1 文件“未被识别为 cmdlet、函数、可运行程序或脚本文件." [英] Powershell ps1 file "is not recognized as a cmdlet, function, operable program, or script file."

查看:34
本文介绍了Powershell ps1 文件“未被识别为 cmdlet、函数、可运行程序或脚本文件."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I made a Powershell function just now and saved it to a ps1 file. However, when I try to execute it from within powershell, it won't run.

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

I've allready changed to the settings for running unsigned code by entering this command:

set-executionpolicy remotesigned

功能是这样的:

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

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

What it does is create a textfile in which all the path's to a certain file are listed.
I've put it directly under c:\ and named the file listAllPaths, same as the function.

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

When I enter the following command inside Powershell:

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

我收到一条错误消息:

I get an error saying:

术语listAllPaths.ps1"不是识别为 cmdlet、函数、可运行的程序或脚本文件.验证条款并重试.

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.

I've tried several things and I honestly don't know how to get this to work? What I expect is for a file to be created on the given path, c:\ in this example. That file having the name testingPDF.txt and the contents being the generated this.

谁能告诉我我忘记了什么.

Can someone tell me what I'm forgetting here.

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

And no, Google doesn't answer everything. Tried that one allready. I wouldn't come and ask it here if I hadn't allready tried the online search-engines.

推荐答案

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

This is a typical error across many platforms where your environment path does not include your current directory. so when you execute your script (or command or program etc), your runtime environment looks everywhere except your current/working directory.

试试

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

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

After reading your comments, I'm going to suggest you try this. I haven't actually verified the logic of your PS script. I'm merely trying to get your script to execute first.

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

Try editing your script as below, and execute as above.

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

listAllPaths

这篇关于Powershell ps1 文件“未被识别为 cmdlet、函数、可运行程序或脚本文件."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆