我正在構建一個 PowerShell 實用程式,它將使用 powershell 腳本將 Get-localuser 命令從本地設備拉到串列框中來備份和恢復便簽。從串列框中選擇用戶組態檔,它會啟動一個命令來獲取檔案。我正在研究的是在您在串列框中選擇一個用戶后,它會發送一個復制檔案的命令謝謝。
示例:管理員是用戶:
%SystemRoot%\explorer.exe c:\users\Admin\AppData\Local\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState\pause copy plum.sqlite-wal cd C:\Users\Admin\Documents\SN Utility
作業編輯:代碼
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = 'StickyNotes Utility'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(10,120)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(185,120)
$cancelButton.Size = New-Object System.Drawing.Size(75,23)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(200,20)
$label.Text = 'Select a user to backup:'
$form.Controls.Add($label)
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10,40)
$listBox.Size = New-Object System.Drawing.Size(250,20)
$listBox.Height = 80
$listbox.Items.AddRange((get-localuser | select -expand name))
$form.Controls.Add($listBox)
$form.Topmost = $True
do
{
$result = $form.ShowDialog()
if ($ListBox.SelectedIndices.Count -lt 1 -and $result -eq [System.Windows.Forms.DialogResult]::OK)
{
Write-Warning 'Nothing was selected, please select a user.'
}
}
until (($result -eq [System.Windows.Forms.DialogResult]::OK -and $listBox.SelectedIndices.Count -ge 1) -or $result -ne [System.Windows.Forms.DialogResult]::OK)
{
$x = $listBox.SelectedItem
$x
}
更新構造
# get an array of subfolder full names in the $rootFolder
$subfolders = (Get-ChildItem -Path $rootFolder -Recurse -Directory).FullName
Add-Type -AssemblyName System.Windows.Forms
$form = New-Object System.Windows.Forms.Form
$form.Text = "SubFolders"
$form.Size = New-Object System.Drawing.Size(300,300)
$form.StartPosition = "CenterScreen"
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10,40)
$listBox.Size = New-Object System.Drawing.Size(260,180)
$listBox.Anchor = 'Top,Right,Bottom,Left'
# fill the listbox with subfolder names
$listbox.Items.AddRange((get-localuser | select -expand name))
# add an event handler on the listbox to do something with the selected item
$listBox.Add_Click({
# here put your code to perform some action with the selected subfolder
$selected = $listBox.GetItemText($listBox.SelectedItem)
# for demo, simply show a messagebox
[System.Windows.Forms.MessageBox]::Show("You selected subfolder`r`n`r`n$selected", "Subfolder")
})
$form.Controls.Add($listBox)
$form.ShowDialog()
$form.Dispose()
uj5u.com熱心網友回復:
如果我正確理解您的問題,我相信您想替換該行
[void] $listBox.Items.AddRange(@("user1", "user2", "user3"))
和
Get-Localuser | select -expand name | foreach {[void] $listbox.Items.Add($_)}
或者
$listbox.Items.AddRange((get-localuser | select -expand name))
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/465204.html
