2016-11-09 196 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