if语句最常见的用法是比较两个项。 PowerShell 具有特殊运算符,可用于不同的比较方案。 当使用比较运算符时,会将左右两侧的值进行比较。 -eq(等于) -eq在两个值之间执行相等检查,以确保它们彼此相等。 PowerShell $value=Get-MysteryValueif(5-eq$value) {# do something} ...
-contains運算子會檢查集合中是否包含您的值。 一旦找到匹配項目,就會傳回$true。 PowerShell $array=1..6if($array-contains3) {# do something} 這是查看集合是否包含您值的慣用方式。 每次使用Where-Object(或-eq)遍歷整個清單,速度會明顯變慢。
-eq :等于 -ne :不等于 -gt :大于 -ge :大于等于 -lt :小于 -le :小于等于 -contains :包含 $array -contains something -notcontains :不包含 !($a): 求反 -and :和 -or :或 -xor :异或 -not :逆 if-else if-else: if($value -eq 1){ code1 }else{ code2 } 循环语句 wh...
1,3,5 -contains 3 -notcontains -not -and -or -ne #不等于 1,3,5 -ne 3 PowerShell条件判断【if语句】 if(num−gt100)"1"elseif( num -eq 100){"0"} else {"-1"} PowerShell条件判断【switch语句】 代码语言:javascript 代码运行次数:0 运行 AI代码解释 $number = 49 switch($number) {...
6 if ( $array -contains 3 ) { # do something } これは、コレクションに値が含まれているかどうかを確認する場合の推奨される方法です。 Where-Object (または -eq) を使用すると、毎回リスト全体が処理され、大幅に低速になります。 バリエーション: -contains (大文字と小文字が区別...
As a result, $C contains four values: 5, 6, 7, and 8.When no data type is specified, PowerShell creates each array as an object array (System.Object[]). To determine the data type of an array, use the GetType() method. For example:...
When you run the function, the value you supply for a parameter is assigned to a variable that contains the parameter name. The value of that variable can be used in the function. The following example is a function calledGet-SmallFiles. This function has a$Sizeparameter. The function displa...
) { # 如果是,没有找到符合标准的文件,给出提醒并停止: "没有找到符合标准的文件."; break } # 如果返回结果为数组,表示有多个文件: if ($files -is [array]) { # 此种情况下,列出你想打开的文件: Write-Host -foregroundColor "Red" -backgroundColor "White" ` "你想打开这些文件吗?" foreach ...
This setting specifies the Microsoft .NET Framework type of the parameter value. For example, if the type isInt32, the parameter value must be an integer. If the type is string, the parameter value must be a character string. If the string contains spaces, the value must be enclosed in ...
Now, what if you’d like to know if the array $arrColorsdoesn’tcontain the colorpurple. In that case, just use the–notcontainsoperator, an operator that returns True if the target valuecan’tbe found in the array. In other words: ...