相关疑难解决方法(0)

在PowerShell中检查路径是否存在的更好方法

我只是不喜欢以下语法:

if (Test-Path $path) { ... }
Run Code Online (Sandbox Code Playgroud)

if (-not (Test-Path $path)) { ... }
if (!(Test-Path $path)) { ... }
Run Code Online (Sandbox Code Playgroud)

特别是在检查"不存在"这种常见用途时,括号太多且不易读.有什么更好的方法呢?

更新:我目前的解决方案是使用别名exist,并not-exist为解释在这里.

PowerShell存储库中的相关问题:https://github.com/PowerShell/PowerShell/issues/1970

powershell

100
推荐指数
4
解决办法
29万
查看次数

PowerShell函数返回true/false

我是使用PowerShell的新手,并想知道是否有人会尝试让PowerShell函数返回值.

我想创建一些将返回值的函数:

 Function Something
 {
     # Do a PowerShell cmd here: if the command succeeded, return true
     # If not, then return false
 }
Run Code Online (Sandbox Code Playgroud)

然后有第二个函数只有在上面的函数为真时才会运行:

 Function OnlyTrue
 {
     # Do a PowerShell cmd here...
 }
Run Code Online (Sandbox Code Playgroud)

powershell function return-value

8
推荐指数
3
解决办法
6万
查看次数

标签 统计

powershell ×2

function ×1

return-value ×1