[英]Powershell command shown different behavior in console and script execution
I am doing a powershell script where it will trigger a product specific command and manipulate the returned result.我正在做一个 powershell 脚本,它将触发特定于产品的命令并操纵返回的结果。 The example of the returned result will look like the following
返回结果的示例如下所示
XXXX command completed Thu Jan 13 16:59:59 2022 :XXXX, Account 'XXXX' on 'XXXX' modified successfully
So if the result contain modified successfully, it will do task A or vice versa based on the result.因此,如果结果包含修改成功,它将根据结果执行任务 A,反之亦然。 But I cant get the expected output when I run it inside my powershell script.
但是当我在我的 powershell 脚本中运行它时,我无法得到预期的 output。 Please refer to my script below
请参考我下面的脚本
write-host "The type of variable :"$eTSuspend.GetType().Name
write-host "The string : "$eTSuspend.Contains('successfully')
FYI, $eTSuspend
is the variable store the returned result by the command and here are my output from my script仅供参考,
$eTSuspend
是存储命令返回结果的变量,这是我的脚本中的 output
The type of variable : Object[]
The string : False
PS C:\Users\XXX.XXX> $eTSuspend.GetType().Name String PS C:\Users\XXX.XXX> $eTSuspend.Contains("successfully") True
You're testing for two different kinds of containment.您正在测试两种不同类型的收容措施。
String.Contains()
performs a substring search - if the substring argument is found at any offset in the string, it returns true: String.Contains()
执行substring 搜索- 如果在字符串中的任何偏移处找到 substring 参数,则返回 true:
"abc def".Contains("def")
returns $true
, because the susbtring "def"
can be extracted from "abc def"
at offset 4. "abc def".Contains("def")
返回$true
,因为子字符串"def"
可以从偏移量 4 处的"abc def"
中提取。Array.Contains()
and the -contains
operator tests for collection containment - that is, "does this array/list/collection contain an exact copy of the argument among it's members": Array.Contains()
和-contains
运算符测试集合包含- 也就是说,“此数组/列表/集合是否包含其成员中参数的精确副本”:
@("abc", "def").Contains("def")
returns true because at least one of the items in the array satisfies that constraint - "def"
and "def"
are equivalent @("abc", "def").Contains("def")
返回 true,因为数组中的至少一项满足该约束 - "def"
和"def"
是等价的@("abc", "def").Contains("de")
returns false because none of the items in the array is exactly equivalent to "de"
, in other words: @("abc", "def").Contains("de")
返回 false,因为数组中的所有项目都不完全等同于"de"
,换句话说:
"abc" -eq "de"
is not true "abc" -eq "de"
不正确"def" -eq "de"
is not true either "def" -eq "de"
也不正确$false
$false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.