gpt4 book ai didi

Powershell v1 : Is it possible to assign the result of a switch statement to a variable?

转载 作者:行者123 更新时间:2023-12-04 17:42:25 26 4
gpt4 key购买 nike

是否可以将 switch 语句的结果分配给变量。

例如,而不是:

switch ($Extension) 
{
doc {$Location = "C:\Users\username\Documents\"; break}
exe {$Location = "C:\Users\username\Downloads\"; break}
default {$Location = "C:\Users\username\Desktop\"}
}

是否可以做类似的事情:
$Location = 
{
switch ($Extension)
{
doc {"C:\Users\username\Documents\"; break}
exe {"C:\Users\username\Downloads\"; break}
default {"C:\Users\username\Desktop\"}
}
}

尝试上述结果会导致 $location 包含整个代码块作为字符串。

最佳答案

对于 V1,我会将 switch 语句包装在一个函数中。

function Get-DocumentLocation($Extension)
{
switch ($Extension)
{
doc {"C:\Users\username\Documents\"; break}
exe {"C:\Users\username\Downloads\"; break}
default {"C:\Users\username\Desktop\"}
}
}

$Location = Get-DocumentLocation $extension

关于Powershell v1 : Is it possible to assign the result of a switch statement to a variable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/738172/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com