如何比较两个foreach循环

发布于 2025-01-22 20:57:01 字数 907 浏览 0 评论 0原文

我当前的脚本检查某些客户端上是否存在特定文件夹。 我想在检查文件夹是否存在之前检查客户端是在线还是离线。 我当前的脚本看起来像这样:

$CDS = Get-content C:\Users\XY\Desktop\Clientliste.txt

Foreach($c in $CDS) {

    IF (Test-Connection -BufferSize 32 -Count 1 -ComputerName $c -Quiet) {
            
        Foreach ($c in $CDS) {
            $Test = Test-Path -path "\\$c\c$\apps\perl"
            Start-Sleep -s 0.25 

            If ($Test -eq $True) {
                Write-Host "Path exists on $c."
            }
            Else {
                Write-Host "Path NOT exist on $c."
            }
        }
    }
    Else {
        Write-Host "The remote computer " $c " is Offline"
    }
}

我不知道如何链接foreach循环,以便它们一起工作。 因为当我现在运行脚本时,它会在第二个请求第二请求之后,然后在第二 foreach 循环中,并且在完成第二个foreach时会首先离开循环。 我不要那个。我希望,如果客户在线,它会检查路径是否存在,然后转到下一个客户端,然后再次检查它是否在线,然后...

也许您可以帮助我:)

My current script checks if a specific folder on some clients exists.
I'd like to check if the client is online or offline before checking if the folder exists.
My current script looks like this:

$CDS = Get-content C:\Users\XY\Desktop\Clientliste.txt

Foreach($c in $CDS) {

    IF (Test-Connection -BufferSize 32 -Count 1 -ComputerName $c -Quiet) {
            
        Foreach ($c in $CDS) {
            $Test = Test-Path -path "\\$c\c$\apps\perl"
            Start-Sleep -s 0.25 

            If ($Test -eq $True) {
                Write-Host "Path exists on $c."
            }
            Else {
                Write-Host "Path NOT exist on $c."
            }
        }
    }
    Else {
        Write-Host "The remote computer " $c " is Offline"
    }
}

I don't know how to link the foreach loops so that they work together.
Because when I run my script now, it goes after the first if request in the 2nd foreach loop and it does leave it first, when it finishes the 2nd foreach loop.
I don't want that. I want that if the client is online, it checks if the paths exists and then goes to the next client and checks again if it is online and then...

Maybe you can help me :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

讽刺将军 2025-01-29 20:57:01

为什么从文本文件中的项目上进行了另一个相同的循环,您已经测试了机器已可以触及的?

只需删除第二个循环,然后做:

$CDS = Get-Content -Path 'C:\Users\XY\Desktop\Clientliste.txt'
foreach($computer in $CDS) {
    if (Test-Connection -BufferSize 32 -Count 1 -ComputerName $computer -Quiet) {
        if (Test-Path -Path "\\$computer\C$\apps\perl" -PathType Container) {
            Write-Host "Path exists on computer '$computer'."
        }
        else {
            Write-Host "Path NOT exist on computer '$computer'."
        }
    }
    else {
        Write-Host "The remote computer '$computer' is Offline"
    }
}

Why do another identical loop over the items from the text file is you have already tested the machine is reachable?

Just remove that second loop and do:

$CDS = Get-Content -Path 'C:\Users\XY\Desktop\Clientliste.txt'
foreach($computer in $CDS) {
    if (Test-Connection -BufferSize 32 -Count 1 -ComputerName $computer -Quiet) {
        if (Test-Path -Path "\\$computer\C$\apps\perl" -PathType Container) {
            Write-Host "Path exists on computer '$computer'."
        }
        else {
            Write-Host "Path NOT exist on computer '$computer'."
        }
    }
    else {
        Write-Host "The remote computer '$computer' is Offline"
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文