简体   繁体   English

Powershell脚本执行时间更短

[英]Shorter execution of powershell script

Say I have a script to be executed in a single call, how do I do it? 假设我有一个脚本可以在单个调用中执行,该怎么办?

Like, say I have a powershell script saved at E:\\Fldr\\scrpt.ps1 . 例如,说我有一个Powershell脚本保存在E:\\Fldr\\scrpt.ps1 Now if I have to normally execute that script is PowerShell ISE then I would have to use: 现在,如果我必须正常执行该脚本是PowerShell ISE,则必须使用:

& "E:\\Fldr\\scrpt.ps1"

and the scrpt.ps1 gets executed. 然后scrpt.ps1被执行。

But whatI want is, when I write a word, say "exeScrpt" instead of & "E:\\Fldr\\scrpt.ps1" then I want scrpt.ps1 to get executed. 但是我想要的是,当我写一个单词时,说“ exeScrpt”而不是& "E:\\Fldr\\scrpt.ps1"然后我要执行scrpt.ps1。

Is there a way to do this? 有没有办法做到这一点?

Thank you for checking in.. 感谢您签到。

You can wrap your call to the script in a function: 您可以将对脚本的调用包装在一个函数中:

function Invoke-Script
{
    E:\Fldr\scrpt.ps1
}

Then you can run your script by executing a single command anywhere after the definition: 然后,您可以通过在定义后的任意位置执行一个命令来运行脚本:

Invoke-Script

Note that it is good practice to name your functions according to the Verb-Noun cmdlet naming standard , so something like Invoke-Script instead of exeScrpt . 请注意,按照Verb-Noun cmdlet命名标准命名函数是一种好习惯,因此类似Invoke-Script而不是exeScrpt If you really want a single word as the name, then you can additionally create an alias for your function: 如果您确实想要一个单词作为名称,则可以另外为函数创建一个别名

New-Alias -Name exeScrpt -Value Invoke-Script

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM