Matthew Flynn
Matthew Flynn

Reputation: 3931

Running a PowerShell Script .PS1

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?

Upvotes: 2

Views: 2481

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200193

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

Upvotes: 4

Related Questions