2

I am trying to generate a MachineKey for my application using the PowerShell script found in kb2915218.

I have copied the function into notepad and saved as a .PS1 file. Now if I look at this file through explorer it is being recognised as a PowerShell file.

I then have run PowerShell and CD to the directory of my .PS1 file.

I then ran the following command:

Set-ExecutionPolicy Unrestricted

followed by:

.\Powershell-Generate-MachineKey.ps1

(the name of my script). And finally I then tried running the command

Generate-MachineKey

However I get the message:

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

Can someone please tell me where I am going wrong here?

2
  • 3
    . .\Powershell-Generate-MachineKey.ps1 (note the leading dot). Commented Nov 9, 2016 at 16:56
  • what a wally i am, I've been looking at this for about an 1hr. Great! please add this as a solution and i'll accept :) Commented Nov 9, 2016 at 16:58

1 Answer 1

4

The script just defines a function, so if you execute it like this:

.\Powershell-Generate-MachineKey.ps1

it won't do anything, because the function isn't invoked anywhere and also isn't made available in the current context. For the latter you need to dot-source the script

. .\Powershell-Generate-MachineKey.ps1

The dot-operator basically executes the script in the current context instead of a child context. That way the definitions from the script become available in the current context, and you can invoke the function like this:

Generate-MachineKey
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.