我尽可能地简化了我的情况。 我有一个PowerShell脚本,使用Start-Process cmdlet以编程方式使用“以管理员身份运行”选项启动进程 :
param ( # Arguments to pass to the process, if any [string[]] $ArgList ) if($ArgList) { Start-Process -FilePath notepad.exe -ArgumentList $ArgList -Verb RunAs } else { Start-Process -FilePath notepad.exe -Verb RunAs }由于ArgumentList不能为NULL也不为空,我必须添加if-else以避免ParameterArgumentValidationError错误。
一切都按预期工作没有问题,但我只是想知道是否有更优雅(但仍然很简单)的方法来实现,而不使用if-else块。
I've simplified my situation as much as possible. I have a PowerShell script that programmatically starts a process with the "Run as administrator" option using the Start-Process cmdlet:
param ( # Arguments to pass to the process, if any [string[]] $ArgList ) if($ArgList) { Start-Process -FilePath notepad.exe -ArgumentList $ArgList -Verb RunAs } else { Start-Process -FilePath notepad.exe -Verb RunAs }Since ArgumentList cannot be NULL nor empty, I had to add the if-else to avoid a ParameterArgumentValidationError error.
Everything works as expected with no problem, but I was just wondering if there's a more elegant (but still simple) way to accomplish that without using the if-else blocks.
最满意答案
有一种方法可以使用splat运算符@ (参见Richard answerd)并使用带有条件的格式字符串。 但是,我认为If-else语句是最可读和最清晰的方法 ,我不会尝试简化它。
There is a way using the splat operator @ (see Richard answerd) and maybe using a format string with a condition. However, I think the If-else Statement is the most readable and clear way to do this and I wouldn't try to simplify it.
更多推荐
发布评论