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

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&quot;</strong><br> if (-not (Test-Path &quot;Low&quot;)) {<br> New-Item -Path &quot;Low&quot; -ItemType &quot;Directory&quot; -ErrorAction SilentlyContinue<br> }<br> icacls.exe &quot;\\$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&quot;)) { ----&gt; need it to evaluate to true<br> <strong>Set-Location &quot;\\$server\C$\Users$UserID\AppData\Local\Temp"

write-host "\$server\C$\Users\$UserID\AppData\Local\Temp&quot; &amp; &quot;does not yet exist, try later&quot; (show this instead of error messages if the set-location does not work because the directory does not work...)<br> how to exit and go back up to the top and process the next object in ($server in $servers)???<br> }<br> if (-not (Test-Path &quot;Low&quot;)) {<br> New-Item -Path &quot;Low&quot; -ItemType &quot;Directory&quot; -ErrorAction SilentlyContinue<br> }<br> icacls.exe &quot;\\$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&quot;&#41;&#41; {Continue}</code><br>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.<br><br>Putting it together I think this is what you want:<br><br><code>$servers = @&#40;&#39;x5&#39;,&#39;x6&#39;,&#39;x7&#39;,&#39;x8&#39;&#41;<br>foreach &#40;$server in $servers&#41; {<br> If&#40;-Not&#40;Test-Path &quot;\\Server\C$\Users$UserID\AppData\Local\Temp")) {
"\Server\C$\Users\$UserID\AppData\Local\Temp does not exist&quot;<br> Continue}<br> Set-Location &quot;\\$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&quot; /setintegritylevel (OI&#41;(CI&#41;low}</code></blockquote>by tommls at 2013-02-04 16:52:40<blockquote>OIC -- I understand on the continue...<br><br>On this line -- &quot;\\$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