Author Posts

December 12, 2017 at 12:12 am

Hi there
looking for some advice
i need to do some kind of a loop where if a condition is met, but also checked every X seconds, since it changes all the time as i am adding new users to queue:
while ((get-moverequest).count -le than X) {
then go through a list from file and add more people to the queue like this:
foreach($s in import-csv c:\files\data.csv){new-moverequest $s.userprincipalname}
}

but what i see is its just running through them without stoping when the number is lower than X

any idea of what would be the best way to tackle this?
i also tried something like this, which seems to work fine but then once it loops through all the users(and waits) it quits:
$s1=import-csv C:\files\2.csv
foreach($s in $s1){
$inprogress=(Get-MoveRequest -MoveStatus inprogress).count
$Queued=(Get-MoveRequest -MoveStatus Queued).count
$monitor=$inprogress + $Queued
if($monitor -le 15){
$dbID++
$dbName = "DB{0:00}" -f $dbID
new-moverequest -Identity $s.userprincipalname -TargetDatabase $dbName -SuspendWhenReadyToComplete:$true
if ($dbID -eq 80) {$dbID = 1}
}
else {Start-Sleep 90;$monitor}
}

thanks

December 12, 2017 at 12:41 am

This will keep checkimg every 90 sec until ctrl-c

while ($true) {
    Import-Csv C:\files\2.csv | foreach {
        $inprogress=(Get-MoveRequest -MoveStatus inprogress).count
        $Queued=(Get-MoveRequest -MoveStatus Queued).count
        $monitor=$inprogress + $Queued
        if($monitor -le 15){
            $dbID++
            $dbName = "DB{0:00}" -f $dbID
            new-moverequest -Identity $PSItem.userprincipalname -TargetDatabase $dbName -SuspendWhenReadyToComplete:$true
            if ($dbID -eq 80) {$dbID = 1}
        } else {
            "inprogress + Queued = $monitor"
        }
    }
    Start-Sleep 90
}

December 12, 2017 at 12:50 am

Thanks you very much
for fast reply and for giving me something new to learn
seems to work fine
Thanks again

December 12, 2017 at 12:59 am

btw, stupid question but
how do i make powershell ise or any other editor show me the lines where { begins and } ends like the code you posted here?
this helps me better understand how powershel sees the code(i know there is + – signs in ise but this seems more readable:))
Thanks again

December 12, 2017 at 2:41 am

The PowerShell ISE, VSCode and most other editors do this by default.

All code blocks (function, region, block comments, Do-While, ForEach, etc..) show this.

Collapsing and expanding All code in the ISE script pane, is a option in the PoSH ISE edit menu, or just use its keyboard shortcut:" CRTL+M

If your ISE is not showing this, then you need to reset its defaults.
'blogs.technet.microsoft.com/heyscriptingguy/2015/11/13/set-powershell-ise-to-default-values'

Take a look at the Tools > Options, to see other possible settings.

December 13, 2017 at 9:17 pm

another small question,
i notice that while it works
it cycles the database number(dbid++) even if the previous users(from the list) fails.
which then gives me uneven distribution(instead of 1-80 and then reset to 1)
could i bypass it by using another if() command like this:

while ($true) {
Import-Csv C:\files\2.csv | foreach {
$inprogress=(Get-MoveRequest -MoveStatus inprogress).count
$Queued=(Get-MoveRequest -MoveStatus Queued).count
$monitor=$inprogress + $Queued
if($monitor -le 15){
if(new-moverequest -Identity $PSItem.userprincipalname -TargetDatabase $dbName -SuspendWhenReadyToComplete:$true){
$dbID++
$dbName = "DB{0:00}" -f $dbID}
if ($dbID -eq 80) {$dbID = 1}
} else {
"inprogress + Queued = $monitor"
}
}
Start-Sleep 90

thanks again

December 14, 2017 at 11:06 am

You need $dbID incremented before attempting new-moverequest, so line 7 ($dbID++) stays. To undo that if the new-moverequest fails, simply enclode the new-moverequest in a try/catch block, and decrement $dbID back in case of failure:

while ($true) {
    Import-Csv C:\files\2.csv | foreach {
        $inprogress=(Get-MoveRequest -MoveStatus inprogress).count
        $Queued=(Get-MoveRequest -MoveStatus Queued).count
        $monitor=$inprogress + $Queued
        if($monitor -le 15){
            $dbID++
            $dbName = "DB{0:00}" -f $dbID
            try {
                new-moverequest -Identity $PSItem.userprincipalname -TargetDatabase $dbName -SuspendWhenReadyToComplete:$true -ErrorAction Stop
            } catch {
                $dbID--
            }
            if ($dbID -eq 80) {$dbID = 1}
        } else {
            "inprogress + Queued = $monitor"
        }
    }
    Start-Sleep 90
}