繁体   English   中英

运行PowerShell脚本.PS1

[英]Running a PowerShell Script .PS1

我正在尝试使用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

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

该脚本仅定义了一个函数,因此,如果您像这样执行它:

.\Powershell-Generate-MachineKey.ps1

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

. .\Powershell-Generate-MachineKey.ps1

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

Generate-MachineKey

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM