by tommls at 2013-02-04 15:43:22
I have this for loop:by kittH at 2013-02-04 16:35:19
$servers = @(‘x5’,‘x6’,‘x7’,‘x8’)
foreach ($server in $servers) {
Set-Location "\$server\C$\Users\$UserID\AppData\Local\Temp"</strong><br> if (-not (Test-Path "Low")) {<br> New-Item -Path "Low" -ItemType "Directory" -ErrorAction SilentlyContinue<br> }<br> 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<br> <strong>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...)<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 "Low")) {<br> New-Item -Path "Low" -ItemType "Directory" -ErrorAction SilentlyContinue<br> }<br> 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…
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}</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 = @('x5','x6','x7','x8')<br>foreach ($server in $servers) {<br> If(-Not(Test-Path "\\Server\C
$\Users$UserID\AppData\Local\Temp")) {
"\Server\C$\Users\$UserID\AppData\Local\Temp does not exist"<br> Continue}<br> 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}</code></blockquote>by tommls at 2013-02-04 16:52:40<blockquote>OIC -- I understand on the continue...<br><br>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