假设我有一个简单的范围,通过Push-Location和Pop-Location以书结尾:
Function MyFunction($Location) { Push-Location $Location # do other stuff here Pop-Location }有没有办法在范围的开始处设置它,以便我不必记得将Pop-Location放在最后? 像这样的东西:
Function MyFunction($Location) { Setup-BothPushAndPopHere $Location # do other stuff here # at the end of the scope, Pop-Location is automatically called }Let's say I have a simple scope that is book-ended with Push-Location and Pop-Location:
Function MyFunction($Location) { Push-Location $Location # do other stuff here Pop-Location }Is there any way to set it up at the beginning of the scope so that I don't have to remember to put the Pop-Location at the end? Something like this:
Function MyFunction($Location) { Setup-BothPushAndPopHere $Location # do other stuff here # at the end of the scope, Pop-Location is automatically called }最满意答案
简短的回答:不。
我对Push-Location和Pop-Location看法是,你通常应该避免它们,并使脚本改为在命令中使用路径名称; 换句话说,而不是:
Push-Location $Location Get-ChildItem Pop-Location做就是了:
Get-ChildItem $Location(简单例子)
如果您必须使用该模式,请考虑try / finally :
Push-Location $Location try { # ... } finally { Pop-Location }因为这有助于意外异常或用户中断程序执行。
当代码不在我的控制范围内时,我通常使用try / finally模式; 大多数时候加载SQLPS模块时,因为它将当前位置更改为SQL服务器提供程序,根据我的经验,使用当前位置的所有内容都变得更慢。
正如Eris指出的那样,它在处理本机应用程序时也很有用。 如果使用空格来绕开路径名称引号是很痛苦的,或者应用程序无法正确处理它,情况尤其如此。
Short answer: No.
My take on Push-Location and Pop-Location is that you should generally avoid them, and adapt your script to use the path names in commands instead; in other words, instead of:
Push-Location $Location Get-ChildItem Pop-LocationJust do:
Get-ChildItem $Location(simplified example)
If you must use the pattern, consider try/finally:
Push-Location $Location try { # ... } finally { Pop-Location }As this helps with unexpected exceptions or the user interrupting program execution.
I typically use the try/finally pattern when the code is outside my control; most often when loading the SQLPS module since it changes the current location to the SQL server provider which in my experience causes everything that uses the current location to become much slower.
As Eris points out, it's also useful when dealing with native applications. This can be especially true if it's painful to escape quotes around path names with spaces, or the application wouldn't handle it correctly anyway.
如何在范围结束时自动调用Pop-Location(How to automatically call Pop-Location at end of scope)假设我有一个简单的范围,通过Push-Location和Pop-Location以书结尾:
Function MyFunction($Location) { Push-Location $Location # do other stuff here Pop-Location }有没有办法在范围的开始处设置它,以便我不必记得将Pop-Location放在最后? 像这样的东西:
Function MyFunction($Location) { Setup-BothPushAndPopHere $Location # do other stuff here # at the end of the scope, Pop-Location is automatically called }Let's say I have a simple scope that is book-ended with Push-Location and Pop-Location:
Function MyFunction($Location) { Push-Location $Location # do other stuff here Pop-Location }Is there any way to set it up at the beginning of the scope so that I don't have to remember to put the Pop-Location at the end? Something like this:
Function MyFunction($Location) { Setup-BothPushAndPopHere $Location # do other stuff here # at the end of the scope, Pop-Location is automatically called }最满意答案
简短的回答:不。
我对Push-Location和Pop-Location看法是,你通常应该避免它们,并使脚本改为在命令中使用路径名称; 换句话说,而不是:
Push-Location $Location Get-ChildItem Pop-Location做就是了:
Get-ChildItem $Location(简单例子)
如果您必须使用该模式,请考虑try / finally :
Push-Location $Location try { # ... } finally { Pop-Location }因为这有助于意外异常或用户中断程序执行。
当代码不在我的控制范围内时,我通常使用try / finally模式; 大多数时候加载SQLPS模块时,因为它将当前位置更改为SQL服务器提供程序,根据我的经验,使用当前位置的所有内容都变得更慢。
正如Eris指出的那样,它在处理本机应用程序时也很有用。 如果使用空格来绕开路径名称引号是很痛苦的,或者应用程序无法正确处理它,情况尤其如此。
Short answer: No.
My take on Push-Location and Pop-Location is that you should generally avoid them, and adapt your script to use the path names in commands instead; in other words, instead of:
Push-Location $Location Get-ChildItem Pop-LocationJust do:
Get-ChildItem $Location(simplified example)
If you must use the pattern, consider try/finally:
Push-Location $Location try { # ... } finally { Pop-Location }As this helps with unexpected exceptions or the user interrupting program execution.
I typically use the try/finally pattern when the code is outside my control; most often when loading the SQLPS module since it changes the current location to the SQL server provider which in my experience causes everything that uses the current location to become much slower.
As Eris points out, it's also useful when dealing with native applications. This can be especially true if it's painful to escape quotes around path names with spaces, or the application wouldn't handle it correctly anyway.
发布评论