Axis52401
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.AccessCont rol.FileSy stemAccess Rule("$use rname","Re ad","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
#----------------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.AccessCont
$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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
$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
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
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.
$x = 0
$x++
While ($X.Count -le$SharedFolderCount)
But it still loops forever.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window