簡體   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