首页 文章

运行PowerShell脚本.PS1

提问于
浏览
2

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

  • 4

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

    .\Powershell-Generate-MachineKey.ps1
    

    它赢得了't do anything, because the function isn' t在任何地方调用,并且在当前上下文中也不可用 . 对于后者,你需要dot-source脚本

    . .\Powershell-Generate-MachineKey.ps1
    

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

    Generate-MachineKey
    

相关问题