exit for loop go to next item if condition false??

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 3 months ago.

  • Author
    Posts
  • #5508

    by tommls at 2013-02-04 15:43:22

    I have this for loop:

    $servers = @('x5','x6','x7','x8')
    foreach ($server in $servers) {
    Set-Location "\\$server\C`$\Users\$UserID\AppData\Local\Temp"
    if (-not (Test-Path "Low")) {
    New-Item -Path "Low" -ItemType "Directory" -ErrorAction SilentlyContinue
    }
    icacls.exe "\\$server\C`$\Users\$UserID\AppData\Local\Temp\Low" /setintegritylevel `(OI`)`(CI`)low
    }

    Where the statement is in bold I need to before it executes test if the directory exists and if not, write-host the directory with 'does not yet exist' concatenated to it, then skip the remaining items and go to the next item, how is this done??

    I've done reading and I gather the code should become like this??????????? — but I don't know exactly how...

    foreach-object ($server in $servers) {
    if ((Test-Path "\\$server\C`$\Users\$UserID\AppData\Local\Temp")) { —-> need it to evaluate to true
    Set-Location "\\$server\C`$\Users\$UserID\AppData\Local\Temp"
    continue??
    else
    write-host "\\$server\C`$\Users\$UserID\AppData\Local\Temp" & "does not yet exist, try later" (show this instead of error messages if the set-location does not work because the directory does not work...)
    how to exit and go back up to the top and process the next object in ($server in $servers)???
    }
    if (-not (Test-Path "Low")) {
    New-Item -Path "Low" -ItemType "Directory" -ErrorAction SilentlyContinue
    }
    icacls.exe "\\$server\C`$\Users\$UserID\AppData\Local\Temp\Low" /setintegritylevel `(OI`)`(CI`)low
    }

    Thank you, Tom

    P.S. Finished code will be posted here — this is PS 2.0 code...

    by kittH at 2013-02-04 16:35:19

    If I am correct in what you are trying to do, what you are looking for is {Continue}
    if (-Not(Test-Path "\\$server\C`$\Users\$UserID\AppData\Local\Temp")) {Continue}
    You don't need the else, if it meets the condition (not finding that path) it will stop processing and move on to the next item in the loop.

    Putting it together I think this is what you want:

    $servers = @('x5','x6','x7','x8')
    foreach ($server in $servers) {
    If(-Not(Test-Path "\\Server\C`$\Users\$UserID\AppData\Local\Temp")) {
    "\\Server\C`$\Users\$UserID\AppData\Local\Temp does not exist"
    Continue}
    Set-Location "\\$server\C`$\Users\$UserID\AppData\Local\Temp"
    if (-not (Test-Path "Low")) {New-Item -Path "Low" -ItemType "Directory" -ErrorAction SilentlyContinue}
    icacls.exe "\\$server\C`$\Users\$UserID\AppData\Local\Temp\Low" /setintegritylevel `(OI`)`(CI`)low}

    by tommls at 2013-02-04 16:52:40

    OIC — I understand on the continue...

    On this line — "\\$Server\C`$\Users\$UserID\AppData\Local\Temp does not exist" — shouldn't I use Write-Host so I can see which server it is??

    Meanwhile, I will test this tomorrow...I'm still new to Powershell...

    Thank you for helping me on this.

    Thank you, Tom

You must be logged in to reply to this topic.