简体   繁体   English

Powershell 命令在控制台和脚本执行中显示不同的行为

[英]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 

I tried to check the data type of the variable but it shown `Object[]` and the string validation shown false. 我试图检查变量的数据类型,但它显示 `Object[]` 并且字符串验证显示为 false。 So I am bit confused because the result is different from what I executed all the powershell command directly in the powershell console. 所以我有点困惑,因为结果与我直接在 powershell 控制台中执行所有 powershell 命令的结果不同。 Here are the step I did in my powershell console 这是我在 powershell 控制台中执行的步骤
1) Copied the returned result and assigned to `$eTSuspend` manually 1) 复制返回的结果并手动分配给`$eTSuspend`
2) Executed $eTSuspend.GetType().Name - Yes,its a string data type 2) 执行 $eTSuspend.GetType().Name - 是的,它是一个字符串数据类型
3) $eTSuspend.Contains('successfully') - Yes, result returned true 3) $eTSuspend.Contains('successfully') - 是的,结果返回 true
 PS C:\Users\XXX.XXX> $eTSuspend.GetType().Name String PS C:\Users\XXX.XXX> $eTSuspend.Contains("successfully") True

I really cant figured out why both of the same command `GetType()` and `-Contains` returned a different result when I executed with script and powershell console 我真的不明白为什么当我使用脚本和 powershell 控制台执行时,相同的命令 `GetType()` 和 `-Contains` 返回了不同的结果

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"也不正确
      • There are no more items in the array to be tested, so it returns $false数组中没有要测试的项目,因此返回$false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM