运行PowerShell脚本.PS1

时间:2016-11-09 16:53:21

标签: powershell

我正在尝试使用kb2915218中的PowerShell脚本为我的应用程序生成一个MachineKey。

我已将该功能复制到记事本中并保存为.PS1文件。现在,如果我通过资源管理器查看此文件,它将被识别为PowerShell文件。

然后我将PowerShell和CD运行到我的.PS1文件目录。

然后我运行了以下命令:

Set-ExecutionPolicy Unrestricted

接下来是:

.\Powershell-Generate-MachineKey.ps1

(我的脚本名称)。最后我尝试运行命令

Generate-MachineKey

但是我收到了消息:

Generate-MachineKey : The term 'Generate-MachineKey' 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
+ Generate-MachineKey
+ ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Generate-MachineKey:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

有人可以告诉我这里哪里出错吗?

1 个答案:

答案 0 :(得分:4)

脚本只定义了一个函数,所以如果你这样执行它:

.\Powershell-Generate-MachineKey.ps1

它不会做任何事情,因为该函数不会在任何地方调用,也不会在当前上下文中可用。对于后者,您需要dot-source脚本

. .\Powershell-Generate-MachineKey.ps1

点运算符基本上在当前上下文中执行脚本而不是子上下文。这样,脚本中的定义在当前上下文中可用,您可以像这样调用函数:

Generate-MachineKey