简体   繁体   English

Powershell如果

[英]Powershell if then

I'm having an issue with the if then. 我在if方面遇到问题。

    $result = getMachineInfo $rlprddeploy $mdt
"-MachineID-    -LastContactAt-          -LastIP-   -Deployment Receiver Version-"
$machine = $result.MachineID
$result | % { '{0,-10} {1,23} {2,16} {3,20}' -f $_.MachineID, $_.LastContactAt, $_.LastIP, $_.DeploymentReceiverVersion }
#$result | ft -auto

if $_.DeploymentReceiverVersion is less than 5.46.54 then I'm going to send a file to do an update. 如果$ _。DeploymentReceiverVersion小于5.46.54,那么我将发送文件进行更新。 If the version is 5.55 then nothing needs to be done. 如果版本为5.55,则无需执行任何操作。

I've been banging my head against this for a week now and I can't figure it out. 我已经为此花了一周的时间,对此我一无所知。 Every $_.blahblah is information pulled from an SQL table on a server. 每个$ _。blahblah都是从服务器上的SQL表提取的信息。

We've been sending out updates manually and I'd like to stop that. 我们一直在手动发送更新,我想停止这样做。

PowerShell's If statement can be written just like any C like language: PowerShell的If语句可以像任何C语言一样编写:

if ($true) {Write-Host "True Yo" }

That said, you haven't posted exactly what your issues is, so this somewhat generic: 就是说,您尚未确切发布问题所在,因此有些通用:

Creating a custom object: 创建一个自定义对象:

$dep = New-Object psobject -Property @{DeploymentReceiverVersion = "5.000.11"; AppName = "OxenTails" }

Comparing this is going to pose a potential problem: 比较这会带来潜在的问题:

$dep.DeploymentReceiverVersion.GetType()

The version is a string, so the comparison is comparing strings: 版本是一个字符串,因此比较是在比较字符串:

$dep.DeploymentReceiverVersion -lt "5.000.12" 

gives an expected result, but 给出了预期的结果,但是

$dep.DeploymentReceiverVersion -lt "5.000.101"

does not. 才不是。

This however could work to compare each set of numbers: 但是,这可以用来比较每组数字:

$parts = $dep.DeploymentReceiverVersion.Split('.')
if ([int]$parts[0] -lt 6 -and [int]$parts[1] -lt 001 -and [int]$parts[2] -lt 101) {
#Do Stuff!
}

Additionally, as pointed out by @TheMadTechnician there is the System.Version type [Version] 此外,正如@TheMadTechnician指出的那样,还有System.Version类型[Version]

It can be used with one of the overload methods: 可以与以下重载方法之一一起使用:

[Version]::new(parts[0],parts[1], parts[2]) -lt [Version]::new(5,46,54)

Which is marginally more readable. 这一点更具可读性。

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

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