Cargue el script de PowerShell para que se pueda llamar en cualquier momento

I am new to powershell and was trying to use the example script posted aquí (El script itself) to calculate a file hash. I can get it to work using dot notation

. 'E:\Tools\Power Shell Scripts\Get-FileHash.ps1' E:\testfile.bin

however I want to be able to use it like the author does and just type

Get-Filehash E:\testfile.bin

I have found I can do

function Get-FileHash {. 'E:\Tools\Power Shell Scripts\Get-FileHash.ps1'}

but that does not behave correctly, it prompts me for the file when I do Get-Filehash E:\testfile.bin

What do I need to put in my profile script to correctly load this script and Get-Help Get-FileHash funciona correctamente?

preguntado el 01 de febrero de 12 a las 23:02

3 Respuestas

Actually, the best practice in this situation is to put the function in a módulo en lugar de en un guión. As ominous as that sounds, all it means is rename the containing file from a .ps1 extensión a un .psm1 extension and then store it under your Documents directory (...\Documents\WindowsPowerShell\Modules\<Name>\<Name>.psm1) dónde <Name> is the base name of your file.

You then load the module with this command:

Import-Module Name

Once loaded, you can call the functions contained within the module y Get-Help will recognize the commands as well. Among other things, modules provide encapsulation so one file does not pollute the context space of another. And by the way, you do not need to write cmdlets in C#; you can also write them in PowerShell itself. By way of example, take a look at my open-source PowerShell library which is all written in PowerShell and includes plenty of help recognized by Get-Help.

There is a lot more to know to really get the full benefit of using functions and modules--I refer you to my article Down the Rabbit Hole-A Study in PowerShell Pipelines, Functions, and Parameters en Simple-Talk.com.

Respondido 03 Feb 12, 05:02

Thanks, that method was the exact thing I was looking for. Do I need to put Import-Module in to my start-up script or do I only need to do it once? - Scott Chamberlain

It depends:-) Normally it should be in your startup (i.e. your profile) a menos que you use an IDE that remembers the modules you loaded manually, such as PowerGUI's IDE. (That's the one I use and I highly recommend it for its intellisense and debugging capabilities.) - Michael Sorens

Gracias por la PowerGUI recommendation, I was just using the built in ISE. - Scott Chamberlain

@MichaelSorens what IDE do you use today? PowerGUI is effectively obsolete. - John Zabroski

I long ago switched to the built-in PowerShell ISE -- but be sure to also check out the amazing plugin for it, ISE Steroids that provides a variety of powerful aids including linting, refactoring, and a variable inspector. (I have no relationship with the authors of that plugin.) - Michael Sorens

Podrías hacerlo:

Set-Alias Get-Filehash "E:\Tools\Power Shell Scripts\Get-FileHash.ps1"

Respondido 02 Feb 12, 03:02

The alias works but doing Get-Help Get-Filehash does not return the detailed help that I would get if I did Get-Help "E:\Tools\Power Shell Scripts\Get-Filehash.ps1" How can I get the help to behave correctly too? - Scott Chamberlain

I think you have to write it as a CmdLet in that case, there's plenty of examples of those online. - Mike Christensen

I searched for a while but I only saw doing C# code in to a cmdlet examples, I could not find a script to cmdlet example. - Scott Chamberlain

Yea I believe you have to compile it to a managed DLL. From what I understand, Get-Help will instantiate the CmdLet class and call a help method on it, so I'm not sure if you can implement that functionality in a script. Then again, I'm not a PowerShell expert and only use it on occasion. - Mike Christensen

Put the directory in which your script resides on your ejecutable PATH (es decir, la PATH Variable ambiental).

BTW, your function wrapper work-around doesn't work because you are not passing the argument to the script:

function Get-FileHash {. 'E:\Tools\Power Shell Scripts\Get-FileHash.ps1' $args}

or you could create an alias (as suggested in other answers)

Respondido 02 Feb 12, 03:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.