Search code examples
powershellonedrive

Automatically Select the option with the longest length in a dropdown and choose that option after 30 seconds


I am trying to adapt this powershell script to automatically select the option with the longest string length and then pick that option after 30 seconds automatically. Full script included at end of post but here are the relevant segments

Default Selection issue:

foreach($onedriveaccount in $onedriveaccounts)
{
  $comboBox1.Items.add($onedriveaccount)
  # This loop should set the item with the longest length to $defaultcomboselect but it doesn't seem to actually pick it
  if($onedriveaccount.length -gt $defaultcombobiglen)
    {
    $defaultcomboselect=$comboBox1.items.Count
    $defaultcombobiglen=$onedriveaccount.length
    }
}

$comboBox1.SelectedItem=$defaultcomboselect
$Form1.Controls.Add($comboBox1)

This should show the dialog for 30 seconds and then break out of the loop and close but it just never opens

Timer issues:


# I want the script to time out after 30s and run the OneDriveSelected subroutine if the user does not pick the account. 
$timeout = new-timespan -Seconds 30
$sw = [diagnostics.stopwatch]::StartNew()

while ($sw -lt $timeout) {
[void]$form1.showdialog()
}
$form1.Close()

This should show the dialog for 30 seconds and then break out of the loop and close but it just never opens

Full script if there was something I missed:

$OneDrives = reg query "HKEY_CURRENT_USER\Software\Microsoft\OneDrive\Accounts" /v "UserFolder" /s
$OneDrivesFiltered=$OneDrives.replace("    UserFolder    REG_SZ","").trimstart().trimend()
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

function RedirectedFoldersGatherInfo {
$cmdoutput= get-date
$runtimelog= $runtimelog + $cmdoutput.ToString() + "`r"
$cmdoutput = reg query "HKEY_CURRENT_USER\Software\Microsoft\OneDrive" /v "UserFolder" /s
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg query "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Desktop"
$runtimelog= $runtimelog + $cmdoutput + "`r" + "`r"
$cmdoutput = reg query "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "Desktop"
$runtimelog= $runtimelog + $cmdoutput + "`r" + "`r"
$cmdoutput = reg query "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"
$runtimelog= $runtimelog + $cmdoutput + "`r" + "`r"
$cmdoutput = reg query "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "{F42EE2D3-909F-4907-8871-4C22FC0BF756}"
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg query "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "Personal"
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg query "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "{374DE290-123F-4565-9164-39C4925E467B}"
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg query "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "{374DE290-123F-4565-9164-39C4925E467B}"
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg query "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "{7D83EE9B-2244-4E70-B1F5-5393042AF1E4}"
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg query "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Favorites"
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg query "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "Favorites"
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg query "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "My Music"
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg query "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "{A0C69A99-21C8-4671-8703-7934162FCF1D}"
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg query "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "My Music"
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg query "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "My Pictures"
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg query "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "{0DDD015D-B06C-45D5-8C4C-F59713854639}"
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg query "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "My Pictures"
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg query "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "My Video"
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg query "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "{35286A68-3C57-41A1-BBB1-0EAE73D76C95}"
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg query "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "My Video"
$runtimelog= $runtimelog + $cmdoutput + "`r"
return $runtimelog
}

function RedirectedFoldersAlterInfo ($OnedrivePath) {
  
$cmdoutput= get-date
$runtimelog= $runtimelog + $cmdoutput.ToString() + "`r"
if($chkMutiDevice.Checked){
$hostname=hostname 
$desktoppath=$OnedrivePath + "\Desktop-" + $hostname
$cmdoutput = md $desktoppath
} else {
$desktoppath=$OnedrivePath + "\Desktop"
}
$cmdoutput = md $desktoppath
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg add """HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders""" /v "Desktop" /t reg_SZ /d """$desktoppath""" /f
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg add """HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders""" /v "Desktop" /t reg_SZ /d """$desktoppath""" /f
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg add """HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders""" /v "Personal" /t reg_SZ /d """$desktoppath""" /f
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = md $OnedrivePath\Documents
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg add """HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders""" /v "{F42EE2D3-909F-4907-8871-4C22FC0BF756}" /t reg_SZ /d """$OnedrivePath\Documents""" /f
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg add """HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders""" /v "Personal" /t reg_SZ /d """$OnedrivePath\Documents""" /f
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = md $OnedrivePath\Downloads
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg add """HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders""" /v "{374DE290-123F-4565-9164-39C4925E467B}" /t reg_SZ /d """$OnedrivePath\Downloads""" /f
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg add """HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders""" /v "{374DE290-123F-4565-9164-39C4925E467B}" /t reg_SZ /d """$OnedrivePath\Downloads""" /f
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg add """HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders""" /v "{7D83EE9B-2244-4E70-B1F5-5393042AF1E4}" /t reg_SZ /d """$OnedrivePath\Downloads""" /f
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = md $OnedrivePath\Favorites
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg add """HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders""" /v "Favorites" /t reg_SZ /d """$OnedrivePath\Favorites""" /f
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg add """HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders""" /v "Favorites" /t reg_SZ /d """$OnedrivePath\Favorites""" /f
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = md $OnedrivePath\Music
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg add """HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders""" /v "My Music" /t reg_SZ /d """$OnedrivePath\My Music""" /f
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg add """HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders""" /v "{A0C69A99-21C8-4671-8703-7934162FCF1D}" /t reg_SZ /d """$OnedrivePath\Music""" /f
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg add """HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders""" /v "My Music" /t reg_SZ /d """$OnedrivePath\Music""" /f
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg add """HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders""" /v "My Pictures" /t reg_SZ /d """$OnedrivePath\Music""" /f
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = md $OnedrivePath\Pictures
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg add """HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders""" /v "{0DDD015D-B06C-45D5-8C4C-F59713854639}" /t reg_SZ /d """$OnedrivePath\Pictures""" /f
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg add """HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders""" /v "My Pictures" /t reg_SZ /d """$OnedrivePath\Pictures""" /f
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = md $OnedrivePath\Videos
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg add """HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders""" /v "My Videos" /t reg_SZ /d """$OnedrivePath\Videos""" /f
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg add """HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders""" /v "{35286A68-3C57-41A1-BBB1-0EAE73D76C95}" /t reg_SZ /d """$OnedrivePath\Videos""" /f
$runtimelog= $runtimelog + $cmdoutput + "`r"
$cmdoutput = reg add """HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders""" /v "My Videos" /t reg_SZ /d """$OnedrivePath\Videos""" /f
$runtimelog= $runtimelog + $cmdoutput + "`r"
return $runtimelog
}

function updatestatus ($logupdate) {
#$oReturn=[System.Windows.Forms.Messagebox]::Show($logupdate)
$logupdate | out-file -filepath C:\windows\ltsvc\packages\onedrive\onedriveredirectlog.txt -append
$logupdate | out-file -filepath $env:userprofile\onedriveredirectlog.txt -append
}

function OneDriveSelected {
if($comboBox1.text){
    $runtimelog=RedirectedFoldersGatherInfo
    updatestatus($runtimelog)
    $runtimelog=RedirectedFoldersAlterInfo ($comboBox1.Text)
    updatestatus($runtimelog)
    $runtimelog=RedirectedFoldersGatherInfo
    updatestatus($runtimelog)
    $Form1.Close()
    }else{
    $oReturn=[System.Windows.Forms.Messagebox]::Show("You must pick a onedrive account")
    }
}

$oReturn=[System.Windows.Forms.Messagebox]::Show("If it has not already been done, setup the users onedrive and sync the root level with the PC. Then press OK")
$Form1 = New-Object System.Windows.Forms.Form
$Form1.ClientSize = New-Object System.Drawing.Size(407, 200)
$form1.topmost = $true
$onedriveaccounts =  $OneDrivesFiltered | select-string -pattern "C:\" -simplematch
$comboBox1 = New-Object System.Windows.Forms.ComboBox
$comboBox1.Location = New-Object System.Drawing.Point(25, 40)
$comboBox1.Size = New-Object System.Drawing.Size(350, 200)
$defaultscomboselect=0
$defaultcombobiglen=0
foreach($onedriveaccount in $onedriveaccounts)
{
  $comboBox1.Items.add($onedriveaccount)
  # This loop should set the item with the longest length to $defaultcomboselect but it doesn't seem to actually pick it
  if($onedriveaccount.length -gt $defaultcombobiglen)
    {
    $defaultcomboselect=$comboBox1.items.Count
    $defaultcombobiglen=$onedriveaccount.length
    }
}

# This should automatically select the option from earlier but when you run the script the combo select is just blank
$comboBox1.SelectedItem=$defaultcomboselect
$Form1.Controls.Add($comboBox1)

$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Point(25, 75)
$Button.Size = New-Object System.Drawing.Size(100, 25)
$Button.Text = "Select"
$Button.add_Click({OneDriveSelected})
$Form1.Controls.Add($Button)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(25, 15)
$label.Size = New-Object System.Drawing.Size(350, 25)
$label.Text = "Select OneDrive to Redirect to:"
$Form1.Controls.Add($label)

$chkMutiDevice = New-Object System.Windows.Forms.checkbox 
$chkMutiDevice.Location = New-Object System.Drawing.Size(150,75) 
$chkMutiDevice.Size = New-Object System.Drawing.Size(225,25) 
$chkMutiDevice.Text = "Multiple Devices"
$Form1.Controls.Add($chkMutiDevice)

# I want the script to time out after 30s and run the OneDriveSelected subroutine if the user does not pick the account. 
$timeout = new-timespan -Seconds 30
$sw = [diagnostics.stopwatch]::StartNew()

while ($sw -lt $timeout) {
[void]$form1.showdialog()
}
$form1.Close()
# This should show the dialog for 30 seconds and then break out of the loop and close but it just never opens 

OneDriveSelected

Solution

  • When you call $form.ShowDialog() without a loop on the console, you will see that it pauses the console until the form is closed, which means you are only calling $form.ShowDialog() once until it is closed and frees up the runspace to loop again with $form.ShowDialog(). It never checks stopwatch time until the form closes!

    Instead you can use System.Windows.Forms.Timer that will run concurrently with ShowDialog()

    Add-Type -AssemblyName System.Windows.Forms
    $form = [System.Windows.Forms.Form]::new()
    $timer = [System.Windows.Forms.Timer]::new()
    $timer.Interval = 30000
    $timer.add_tick({ $form.Close() })
    $timer.Start()
    $form.ShowDialog()