自动选择下拉列表中长度最长的选项,并在 30 秒后选择该选项

问题描述 投票:0回答:1

我正在尝试调整此 powershell 脚本以自动选择字符串长度最长的选项,然后在 30 秒后自动选择该选项。完整的脚本包含在帖子末尾,但以下是相关片段

默认选择问题:

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)

这应该显示对话框 30 秒,然后跳出循环并关闭,但它永远不会打开

计时器问题:


# 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()

这应该显示对话框 30 秒,然后跳出循环并关闭,但它永远不会打开

完整脚本(如果我错过了什么):

$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
powershell onedrive
1个回答
0
投票

当您在控制台上没有循环地调用 $form.ShowDialog() 时,您会看到它会暂停控制台直到表单关闭,这意味着您只调用 $form.ShowDialog() 一次,直到它关闭并释放向上运行空间以使用 $form.ShowDialog() 再次循环。在表单关闭之前,它从不检查秒表时间!

您可以使用同时运行的

System.Windows.Forms.Timer
使用 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()
© www.soinside.com 2019 - 2024. All rights reserved.