PowerShell -gt 比较运算符不起作用 [英] PowerShell -gt comparison operator not working

查看:46
本文介绍了PowerShell -gt 比较运算符不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 PowerShell 脚本来读取文件的输出,然后如果数字高于我想要的数字,它会发送一封电子邮件.脚本如下 -

I am currently using a PowerShell script to read the output of a file and then if the number is higher than I want, it sends an email. Script is below -

$Output = 'D:\alec.data\QueuedJobss.txt'
d:
set-location -Path 'D:\program files\veritas\netbackup\bin\admincmd'
.\bpdbjobs -summary -L > $Output

$Queued = (Select-String -Path $Output -Pattern '(?<=Queued:\s+)\d+').Matches.Value

if ($Queued -gt 1) 

它正在创建文件,但没有将其发送给我.我知道我的电子邮件脚本正在工作,因为它们与我一直使用的相同.似乎很难解释字符串.我没有收到任何关于代码的错误.它正在读取的输出看起来像这样 -

It is creating the file, but it's not sending it to me. I know my email scripts are working because they're the same ones I always use. It seems it's having a hard time interpreting the string. I do not receive any errors on the code. The output it's reading from looks like this -

Summary of jobs on usctest01
Queued:                                6
Waiting-to-Retry:                        0
Active:                           0
Successful:                   25863
Partially Successful:           113
Failed:                         184
Incomplete:                       0
Suspended:                        0
Total:                        26160

推荐答案

如果您通过运行 $Queued | 在 $Queued 上签出 get-membergm 你会看到:TypeName: System.String

if you check out get-member on $Queued by running $Queued | gm you will see this: TypeName: System.String

so $Queued 是一个字符串,因此 -gt 不起作用.但是,如果您将变量转换为整数,如下所示([int] 告诉变量为整数),您可以使用 -gt 如您的示例所示:

so $Queued is a string and thus -gt does not work. however if you cast the the variable as integer as follows (the [int] tells the variable to be an integer) you can use -gt as shown in your example:

[int]$Queued = (Select-String -Path $Output -Pattern '(?<=Queued:\s+)\d+').Matches.Value

正在运行 $Queued |gm 现在会告诉你:TypeName: System.Int32

running $Queued | gm now will show you this: TypeName: System.Int32

这篇关于PowerShell -gt 比较运算符不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆