powershell - Windows Powershell 'if' 总是返回 $true - 我做错了什么?
问题描述
问题几乎说明了一切。我有一个简单的脚本(如下),但 IF 不会合作。即使值不匹配,它也总是评估为真。提前感谢您的任何建议。
Add-Type -AssemblyName System.Windows.Forms
$pos = [System.Windows.Forms.Cursor]::Position
$x = $pos.X
$y = $pos.Y
while ($true)
{
Write-Host "on loop strt: x = $($x), y = $($y)"
$check = (($x -eq $pos.X) -and ($y -eq $pos.Y))
write-host $check
if (($x -eq $pos.X) -and ($y -eq $pos.Y))
{
for ($i = 0; $i -lt 2500; $i++)
{
$pos = [System.Windows.Forms.Cursor]::Position
$x = ($pos.X % 1024) + 1
$y = ($pos.Y % 768) + 1
[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($x, $y)
for ($d = 0; $d -lt 1000; $d++) {} #add a little delay
}
}
Write-Host "On loop exit: x = $($x), y = $($y)"
Start-Sleep -Seconds 5
$pos = [System.Windows.Forms.Cursor]::Position
$x = $pos.X
$y = $pos.Y
}
解决方案
问题似乎试图直接与 pos.X 和 pos.Y 进行比较——我添加了另外 2 个变量 $a 和 $b 进行比较。
Add-Type -AssemblyName System.Windows.Forms
$pos = [System.Windows.Forms.Cursor]::Position
$x = $pos.X
$y = $pos.Y
$a = $x
$b = $y
while ($true)
{
if ($x -eq $a -and $y -eq $b)
{
for ($i = 0; $i -lt 2500; $i++)
{
$pos = [System.Windows.Forms.Cursor]::Position
$x = [int]($pos.X % 1024) + 1
$y = [int]($pos.Y % 768) + 1
[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($x, $y)
for ($d = 0; $d -lt 1000; $d++) {}
}
}
else
{
$pos = [System.Windows.Forms.Cursor]::Position
$x = $pos.X
$y = $pos.Y
$a = $x
$b = $y
}
Start-Sleep -Seconds 5
$pos = [System.Windows.Forms.Cursor]::Position
$x = [int]$pos.X
$y = [int]$pos.Y
}
推荐阅读
- python - Python Connexion:自动验证 Accept 标头
- sql - 扩展日期范围以从包含排除日期的表格中获取一系列日期范围之间的所有日期
- python - 如何将函数的输出作为列表返回
- azure - 关于通过 ARM 支持某些资源的问题
- spring-boot - 如何桥接 Bridge Spring 和 Camel 属性占位符?
- android - 如何将数据从 recyclerview 适配器传递到片段
- r - 使用 getSymbols() 的最快和/或更好的方法是什么?将每个股权分开还是全部合并?
- angular - 从 Angular 应用程序向 K8s 集群内的服务发出请求时出现无法解释的 504
- sql - Vertica 中的 LOCF(最后的观察结果)?
- chef-infra - 引导时发生脚本 galera new cluster 错误