windows 无法将脚本名称识别为 cmdlet、函数等;也不能在简单的脚本上找到位置参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34148182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Not Recognizing Script Name as cmdlet, function, etc; nor can positional perameter be found on simple script
提问by GuyWantingBoat
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):
我正在尝试做我的第一个脚本。简单地让 PowerShell 提取在记事本中输入的脚本并保存为名为“test”的 .ps1 文件(也尝试过 Script,但知道名称与它无关):
Write-Host "Hello, World!"
In PowerShell I am typing
在 PowerShell 中,我正在输入
& "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
已尝试在 PowerShell 中重命名文件
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.
我已经在RemoteSigned
和之间切换Unrestricted
,我尝试了一个代码,包括executionpolicy bypass
(我很抱歉,我没有写下那个就关闭了我的窗口)。据我所知,一切都是最新的,我运行的是 Windows 10、Windows PowerShell 和常规的 Windows 记事本。
回答by PSGuy
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.
首先,我强烈推荐使用 Windows PowerShell ISE 来编写脚本。它是免费的,并且提供了相当不错的控制台/编辑器体验,因为它是免费的(据说那里有更好的,但这对我来说总是很好)。我将 Visual Studio 用于其他东西,虽然它是一个指数级更好的产品(应该是),但 PowerShell ISE 的功能非常丰富。
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.
接下来,如果您刚刚开始,您应该查看 Don Jone 的“在一个月的午餐中学习 PowerShell 3.0”一书。它比最新版本晚了两个版本,但是,所有信息仍然是相关的,一旦你读完这本书,你就可以很容易地自己寻求其他任何帮助。它涵盖了所有基础知识,是学习语言的非常好的第一步。
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.
现在,回答您的问题:PowerShell 脚本通常具有 .ps1 文件扩展名。其他扩展通常用于模块 (.psm1) 或 Windows PowerShell 利用的其他帮助程序内容。对于大多数情况,您会坚持使用 .ps1,当您达到开始需要其他扩展名的程度时,我怀疑您可以轻松确定需要哪些扩展名。
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 脚本。第一个来自正常的命令提示符,并告诉 PowerShell 执行您的脚本。这如下所示:
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).
这将告诉 PowerShell 进程忽略您设置的任何 PowerShell 配置文件,如果您的配置文件脚本中有一堆内容可以执行读取控制台输入等操作(对于您当前的情况,我将假设您不,但你可能在未来)。另一个是 ExecutionPolicy 一:RemoteSigned 将告诉 PowerShell 基本上忽略从互联网上下载的任何内容,但允许任何源自网络内部的内容免费运行。可能不是最佳实践,但如果您可以相信您的脚本存储库是安全的,这不是一个可怕的策略。如果没有,则选择比这更严格的内容(您可以通过在 PowerShell 提示符中键入“Get-Help about_Execution_Policies”来阅读执行策略,
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.
第二种方式来自 Windows PowerShell 脚本内部。这实际上要容易得多。请注意,您必须将执行策略设置为允许脚本运行的内容,但此后,您将一帆风顺。
. .\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).
这称为“点源”脚本。在 Windows PowerShell 中执行此操作的优点是,如果您有类似充满函数的脚本之类的东西,它们会被添加到当前范围 (Get-Help about_Scopes),这意味着它们现在在您的当前会话中可用。一个很好的例子是在与主脚本一起分发的脚本中定义一个名为“Test-DomainConnection”的函数:您可以点源与主脚本一起分发的脚本(这通常在您将“标准" 来自主脚本的 PowerShell 函数),然后使用主脚本中的函数。这种方法有优点也有缺点,但它似乎是普遍推荐的(可能有一些社区扩展可以消除手动管理的需要)。
For additional information, you can call Get-Help about_Scriptsfrom 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.
有关其他信息,您可以从 Windows PowerShell 内部调用Get-Help about_Scripts。由于您使用的是 Windows 10,您可能需要在本地系统上提供帮助内容之前从管理 PowerShell 窗口运行 Update-Help。
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.
如果您有任何其他问题,请随时给我发消息 :) 我已经使用 PowerShell 一段时间了,也许可以提供帮助。