无法将脚本名称识别为 cmdlet、函数等;也不能在简单的脚本上找到位置参数 [英] Not Recognizing Script Name as cmdlet, function, etc; nor can positional perameter be found on simple script

查看:325
本文介绍了无法将脚本名称识别为 cmdlet、函数等;也不能在简单的脚本上找到位置参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写我的第一个脚本.简单地让 PowerShell 提取在记事本中输入的脚本并保存为名为test"的 .ps1 文件(也尝试过脚本,但知道名称与它无关):

Write-Host "Hello, World!"

在 PowerShell 中我正在输入

&"C:\Scripts\test.ps1"

以及

./test.ps1

我只遇到过这个:

./test.ps1.txt :术语./test.ps1.txt"不被识别为cmdlet、函数、脚本文件或可运行的程序.检查拼写名称,或者如果包含路径,请验证路径是否正确,然后重试.在行:1 字符:1+ ./test.ps1.txt+ ~~~~~~~~~~~~~~+ CategoryInfo: ObjectNotFound: (./test.ps1.txt:String) [], CommandNotFoundException

已尝试使用 PowerShell 重命名文件

PS C:\Scripts>重命名项目 test.ps1.txt test.ps1

我已经在 RemoteSignedUnrestricted 之间切换,我尝试了包括 executionpolicy bypass 的代码(我很抱歉,我没有关闭我的窗口把那个写下来).据我所知,一切都是最新的,我运行的是 Windows 10、Windows PowerShell 和常规的 Windows 记事本.

解决方案

首先,我强烈建议使用 Windows PowerShell ISE 编写脚本.它是免费的,并且提供了相当不错的控制台/编辑器体验,因为它是免费的(据说那里有更好的,但这对我来说总是很好).我将 Visual Studio 用于其他用途,虽然它是一个指数级更好的产品(并且应该如此),但 PowerShell ISE 的功能非常丰富.

接下来,如果您刚刚开始,您应该查看 Don Jone 的在一个月的午餐中学习 PowerShell 3.0"一书.它比最新版本晚了两个版本,但是,所有信息仍然是相关的,一旦你读完这本书,你就可以很容易地自己寻求其他任何帮助.它涵盖了所有基础知识,是学习语言的非常好的第一步.

现在,回答您的问题:PowerShell 脚本通常具有 .ps1 文件扩展名.其他扩展通常用于模块 (.psm1) 或 Windows PowerShell 利用的其他帮助程序内容.对于大多数情况,您会坚持使用 .ps1,当您达到开始需要其他扩展名的程度时,我怀疑您可以轻松确定需要哪些扩展名.

通常有两种方法可以调用 PowerShell 脚本.第一个是来自普通命令提示符,并告诉 PowerShell 执行您的脚本.如下图所示:

powershell.exe -File MyScript.ps1

我建议您使用一些其他参数,但使用情况取决于您的要求.这是我通常标记的内容:

powershell.exe -NoProfile -ExecutionPolicy RemoteSigned -File MyScript.ps1

这将告诉 PowerShell 进程忽略您设置的任何 PowerShell 配置文件,如果您的配置文件脚本中有一堆执行读取控制台输入之类的内容(对于您当前的情况,我会假设你没有,但你可能在未来).另一个是 ExecutionPolicy 一:RemoteSigned 将告诉 PowerShell 基本上忽略从 Internet 下载的任何内容,但允许任何源自网络内部的内容免费运行.可能不是最佳实践,但如果您可以相信您的脚本存储库是安全的,这不是一个可怕的策略.如果不是,则选择比这更严格的内容(您可以通过在 PowerShell 提示中键入Get-Help about_Execution_Policies"或访问有关它们的 TechNet 页面来阅读执行策略——如果不相同,内容应该相似).

第二种方式是从 Windows PowerShell 脚本内部.这实际上要容易得多.请注意,您必须将执行策略设置为允许脚本运行的内容,但此后,您将一帆风顺.

<预><代码>..\MyScript.ps1

这称为点源"您的脚本.在 Windows PowerShell 中执行此操作的优点是,如果您有类似充满函数的脚本之类的东西,它们会被添加到当前范围 (Get-Help about_Scopes),这意味着它们现在在您的当前会话中可用.一个很好的例子是在与主脚本一起分发的脚本中定义一个名为Test-DomainConnection"的函数:您可以点源与主脚本一起分发的脚本(这通常是在您将标准" 来自主脚本的 PowerShell 函数),然后使用主脚本中的函数.这种方法有利有弊,但似乎普遍推荐使用(可能有一些社区扩展可以消除手动管理的需要).

有关其他信息,您可以从 Windows PowerShell 内部调用 Get-Help about_Scripts.由于您使用的是 Windows 10,您可能需要在本地系统上提供帮助内容之前从管理 PowerShell 窗口运行 Update-Help.

如果您还有其他问题,请随时给我发消息 :) 我已经使用 PowerShell 一段时间了,也许可以提供帮助.

I am trying to do my first script. To simply get PowerShell to pull up a script typed up in notepad and saved as a .ps1 file titled "test" (have also tried Script, but know names have nothing to do with it):

Write-Host "Hello, World!"

In PowerShell I am typing

& "C:\Scripts\test.ps1"

As well as

./test.ps1

And am only met with this:

./test.ps1.txt : The term './test.ps1.txt' 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
+ ./test.ps1.txt
+ ~~~~~~~~~~~~~~
    + CategoryInfo: ObjectNotFound: (./test.ps1.txt:String) [], CommandNotFoundException

Have tried renaming the file within PowerShell with

PS C:\Scripts> Rename-Item test.ps1.txt test.ps1

I have switched between RemoteSigned and Unrestricted, I have tried a code including executionpolicy bypass (I do apologize, I closed my window without writing that one down). As far as I know everything is up to date and I am running Windows 10, Windows PowerShell, and regular Windows Notepad.

解决方案

First, I'd HIGHLY recommend using the Windows PowerShell ISE for writing scripts. It's free, and provides a pretty decent console/editor experience, given that it's free (there are allegedly better ones out there, but this has always done just fine for me). I use Visual Studio for other stuff, and while it is an EXPONENTIALLY better product (and should be), the PowerShell ISE is pretty feature-rich.

Next, if you're just getting started, you should check out Don Jone's "Learn PowerShell 3.0 in a Month of Lunches" book. It's two versions behind the most current, however, all of the information is still relevant, and once you've finished the book, you'll be able to seek help for anything else pretty easily on your own. It covers all the basics, and is a very good first step to learning the language.

Now, to answer your question: PowerShell scripts commonly have the .ps1 file extension. Other extensions are generally used for modules (.psm1) or other helper content that Windows PowerShell leverages. For most things, you'll stick to .ps1, and when you've reached a point where you start needing the other extensions, I suspect you will have no problems identifying which ones you need.

There are two ways generally call a PowerShell script. The first is from a normal command prompt, and telling PowerShell to execute your script. This is shown below:

powershell.exe -File MyScript.ps1

There are some additional parameters that I'd recommend you use, but usage is dependent on your requirements. Here's what I usually tag on mine:

powershell.exe -NoProfile -ExecutionPolicy RemoteSigned -File MyScript.ps1

This will tell the PowerShell process to ignore any PowerShell profiles you have set up, which is ideal if you have a bunch of stuff in your profile script that does things like read console input (for your current situation, I'm going to assume you don't, but you may in the future). The other is that ExecutionPolicy one: RemoteSigned will tell PowerShell to basically ignore anything that's been downloaded from the interwebs, but allow anything originating inside your network to run free. Probably not the best practice, but this isn't a TERRIBLE policy if you can trust that your script repository is secured. If not, then go for something tighter than this (you can read up on execution policies by typing "Get-Help about_Execution_Policies" in the PowerShell prompt, or by visiting the TechNet page about them -- the content should be similar if not identical).

The second way is from inside of a Windows PowerShell script. It's actually much easier to do. Note that you must set your execution policy to something that will allow scripts to run, but thereafter, you're smooth sailing.

. .\MyScript.ps1

This is called "dot-sourcing" your script. The advantage of doing this from within Windows PowerShell is that if you've got something like a script full of functions, they get added to the current scope (Get-Help about_Scopes), which means they're now available in your current session. A good example would be defining a function called "Test-DomainConnection" in a script you distribute with your main script: You'd dot-source the script that is distributed with the main one (this is done usually when you separate your "standard" PowerShell functions from your main script), and then use the functions in the main script. There are pros and cons to this approach, but it seems to be generally recommended (there may be some community extensions out there that remove the need to manage this manually).

For additional information, you can call Get-Help about_Scripts from inside Windows PowerShell. Because you're using Windows 10, you may need to run Update-Help from an administrative PowerShell window before the help content is available on your local system.

If you have any more questions, feel free to message me :) I've been doing PowerShell for a while and may be able to help out.

这篇关于无法将脚本名称识别为 cmdlet、函数等;也不能在简单的脚本上找到位置参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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