Link to home
Start Free TrialLog in
Avatar of Axis52401
Axis52401Flag for United States of America

asked on

problem with for loop, looping forever, not exiting

I have a for loop that won't stop looping. The value of $SharedFolderCount = $ServerShares.Count is equal to 2, but I don't know why this loop won't exit. Here is my script:

#----------------Get UserName-----------------------------------------------
$username = import-csv events.csv | select -First 1 -ExpandProperty domainname
$ProcessShares = Import-Csv ServerShares.csv | Where-Object {$_.Path -match '\A[A-Z]:\\\w+' -and !$_.Name.EndsWith('$')}
$ServerShares = $ProcessShares | % {$_.Path}

#----------------Disable Share Access For User-----------------------------------------------

#$SharedFolder = Read-Host -Prompt "Input Path to Shared Folder"
$SharedFolderCount = $ServerShares.Count
$SharedEvents = @()
Do {
      ForEach ($SharedData In $ServerShares) {
            $acl = Get-Acl $SharedData
            $rule = New-Object System.Security.AccessControl.FileSystemAccessRule("$username","Read","Deny")
            $acl.AddAccessRule($rule)

            Set-Acl $SharedData $acl
      }
      If ($SharedEvents.Count -lt 1) {
            Start-Sleep -Seconds 5
      }
} While ($SharedEvents.Count -le $SharedFolderCount)
Write-Output $DisabledShares | export-csv DisabledShares.csv -NoTypeInformation
Avatar of Raheman M. Abdul
Raheman M. Abdul
Flag of United Kingdom of Great Britain and Northern Ireland image

Execute this and see the output to understand what is going on.

#----------------Get UserName-----------------------------------------------
$username = import-csv events.csv | select -First 1 -ExpandProperty domainname
$ProcessShares = Import-Csv ServerShares.csv | Where-Object {$_.Path -match '\A[A-Z]:\\\w+' -and !$_.Name.EndsWith('$')}
$ServerShares = $ProcessShares | % {$_.Path}

#----------------Disable Share Access For User-----------------------------------------------

#$SharedFolder = Read-Host -Prompt "Input Path to Shared Folder"
[int]$SharedFolderCount = $ServerShares.Count
$SharedEvents = @()
Do {
      ForEach ($SharedData In $ServerShares) {
            $acl = Get-Acl $SharedData
            $rule = New-Object System.Security.AccessControl.FileSystemAccessRule("$username","Read","Deny")
            $acl.AddAccessRule($rule)

            Set-Acl $SharedData $acl
      }
      If ($SharedEvents.Count -lt 1) {
            Start-Sleep -Seconds 5
      }
write-host "SharedFolder count -> $SharedFolderCount     SharedEvents Count -> $($SharedEvents.Count)"
} While ($SharedEvents.Count -le $SharedFolderCount)

Write-Output $DisabledShares | export-csv DisabledShares.csv -NoTypeInformation

Open in new window

SOLUTION
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
$SharedEvents.Count  is always 0
and $SharedFolderCount is always 2
 
While ($SharedEvents.Count -le $SharedFolderCount)  condition is always satisfying since you are not updating both of them anywhere in your code and the while condition is always satisfying and is the reason for always looping

you have to either add into $sharedevents  array or decrease the $sharedFoldercount in your code between do and while
Avatar of Axis52401

ASKER

Ok, my next question is how do I increment. I've tried
$x = 0
$x++
While ($X.Count -le$SharedFolderCount)
But it still loops forever.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial