2016-11-09 192 views
1

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

我已将该功能复制到记事本并保存为.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

是否有人可以告诉我在哪里,我错了吗?

+2

'。 。\ Powershell-Generate-MachineKey.ps1'(注意前导点)。 –

+0

我是一个什么样的人,我一直在看这个约1小时。大!请添加此作为解决方案,我会接受:) –

回答

3

脚本只定义的函数,因此如果执行这样的:

.\Powershell-Generate-MachineKey.ps1 

它不会做任何事情,因为功能没有任何地方调用,并在不提供当前上下文。对于后者,你需要dot-source脚本

. .\Powershell-Generate-MachineKey.ps1 

该点的经营情况基本上执行在当前的背景下,而不是孩子方面的脚本。这样脚本中的定义在当前上下文中变得可用,并且您可以调用如下函数:

Generate-MachineKey