创建 Powershell ListBox 便签备份实用程序
Create a Powershell ListBox Sticky Notes Backup Utility
我正在构建一个 PowerShell 实用程序,它将使用 powershell 脚本将 Get-localuser 命令从本地设备拉入列表框来备份和恢复便签。从列表框中选择用户配置文件,它会启动一个命令来获取文件。我正在研究的是你select列表框中的用户,它发送一个复制文件的命令谢谢。
示例:管理员是用户:
%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
}
列表框中的目录列表
$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'
$listbox.Items.AddRange((get-localuser | select -expand name))
$listBox.Add_Click({
$selected = $listBox.GetItemText($listBox.SelectedItem)
[System.Windows.Forms.MessageBox]::Show("You selected subfolder`r`n`r`n$selected", "Subfolder")
})
$form.Controls.Add($listBox)
$form.ShowDialog()
$form.Dispose()
如果我没有正确理解你的问题,我相信你想更换行
[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))
我正在构建一个 PowerShell 实用程序,它将使用 powershell 脚本将 Get-localuser 命令从本地设备拉入列表框来备份和恢复便签。从列表框中选择用户配置文件,它会启动一个命令来获取文件。我正在研究的是你select列表框中的用户,它发送一个复制文件的命令谢谢。
示例:管理员是用户:
%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
}
列表框中的目录列表
$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'
$listbox.Items.AddRange((get-localuser | select -expand name))
$listBox.Add_Click({
$selected = $listBox.GetItemText($listBox.SelectedItem)
[System.Windows.Forms.MessageBox]::Show("You selected subfolder`r`n`r`n$selected", "Subfolder")
})
$form.Controls.Add($listBox)
$form.ShowDialog()
$form.Dispose()
如果我没有正确理解你的问题,我相信你想更换行
[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))