无法识别 Powershell 函数 [英] Powershell Function Not Recognised

查看:80
本文介绍了无法识别 Powershell 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的几个小时里,我一直在将我的第一个 powershell 脚本放在一起,但我一直看到一个我似乎无法深入了解的错误.

I've been putting my first powershell scripts together for the last couple of hours and I keep seeing an error that I can't seem to get to the bottom of.

我正在使用 Powershell ISE 工具编写和运行脚本.

I'm using the Powershell ISE tool to write and run the scripts.

为了查看它是否在我的脚本中,我创建了一个超级简单的测试脚本,但我遇到了同样的问题.整个测试脚本是:

To see if its something in my script I've created a super simple test script and I'm seeing the same problem. The entire test script is:

Test;

function Test
{
    New-Item C:\Users\jgreen\Desktop\jammer\ -type directory
}

当我点击 Run Script 按钮时,产生的错误是:

When I hit the Run Script button the error produced is:

Test : The term 'Test' 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.

如果我再次点击 Run Script 按钮,它就会工作并完成工作.我根本不明白出了什么问题.我只是不明白.我的脚本有问题吗?

If I simply hit the Run Script button again, it work and does the job. I simply do not understand what is wrong. I simply don't get it. Is there a problem with my script or not?

为什么在 PS ISE 中打开脚本后第一次运行的脚本会爆炸?

Why would a script that works bomb out the first time after opening the script in PS ISE?

推荐答案

您在定义函数之前调用它.它第二次工作的原因是第一次运行的结果.当它第一次运行时它定义了函数,所以当你第二次运行脚本时它知道函数是什么.

You are calling the function before it is defined. The reason it works the second time, is a result of the first run. When it is ran the first time it's defining the function, so when you run the script the second time it knows what the function is.

这篇关于无法识别 Powershell 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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