【问题标题】:Powershell Script: User Input with Predefined Fields Already in Dialog BoxPowershell 脚本:对话框中已有预定义字段的用户输入
【发布时间】:2014-11-05 17:40:46
【问题描述】:

我一直在编写一个脚本,它将所有内容从我的下载文件夹移动到外部驱动器上的另一个文件夹(我们现在将其称为“备份驱动器”),按扩展名将其分类到文件夹中(最初是在 StackOverflow 上找到的)开始 --Thanks to Nicola Cossu--)。每隔一段时间,我需要使用脚本才能将内容从另一个驱动器拉到备份驱动器。

我想要一个脚本来询问我的源驱动器和目标驱动器,但我希望它能够预定义我的下载文件夹和备份驱动器文件夹。

我认为可行的方法之一是弹出对话框,让每个问题的位置都已填写,以便我仅在需要时对其进行编辑。我不确定如何执行此操作。

如果它需要硬编码为变量 $MySRC 和 $MyDST,那很好。 提前谢谢!!!

这是我的代码:

# Get Start Time of the Script
$startDTM = (Get-Date)

# Get Source and Destination -- this is where I am trying to get the predetermine variables to be
$source = Read-Host "Enter for Source"
$dest = Read-Host "Enter for Destination"

# This could be where my variables are located
# $MySRC = "C:\Users\ME\Downloads"
# $MyDST = "E:\Sorted Downloads"

# Logging information for personal reasons
echo ""
echo "This script is starting"
echo ""
echo "The source is " 
$source 
echo ""
echo "The destination is "
$dest
echo ""
echo ""

# Actual Script to Run
$file = gci -Recurse $source | ? {-not $_.psiscontainer} 
$file | group -property extension | 
        % {if(!(test-path(join-path $dest -child $_.name.replace('.','')))) { new-item -type directory $(join-path $dest -child $_.name.replace('.','')).toupper() }}
$file | % {  move-item $_.fullname -destination $(join-path $dest -child  $_.extension.replace(".",""))}

# End of Script Information -- Personal Reasons
echo "This Script is Done!"
echo 'Source: ' $source ' Destination: ' $dest
echo ""

# Get End Time:
$endDTM = (Get-Date)
# Echo Time Elapsed for the Script to Run:
"Elapsed Time: $(($endDTM-$startDTM).totalseconds) seconds"

【问题讨论】:

  • 这个问题有严重的TL;DR!我建议您编辑它以缩小您遇到的问题,而不是它存在的背景。还要避免在一个问题中提出多个问题。
  • 已修复 TL;DR,对不起那些人!

标签: powershell input field defined


【解决方案1】:

这可以通过 windows 窗体和一个简单的函数来完成。我把它放在手边,以防我需要提示输入文件夹:

Function Get-FolderPath{
[CmdletBinding()]
Param(
    [String]$Description,
    [String]$InitialDirectory = "C:\",
    [Switch]$NewFolderButton = $false)

    [void][System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")
    $FolderBrowserDialog = New-Object System.Windows.Forms.FolderBrowserDialog
    $FolderBrowserDialog.SelectedPath = $InitialDirectory
    $FolderBrowserDialog.Description = $Description
    $FolderBrowserDialog.ShowNewFolderButton = $NewFolderButton
    If($FolderBrowserDialog.ShowDialog() -eq "OK"){
        $FolderBrowserDialog.SelectedPath
    }
}

用法是这样的:

$MySRC = Get-FolderPath "Select source folder or drive:" "$env:USERPROFILE\Downloads"

这将弹出一个窗口文件夹选择对话框,询问源文件夹。还有一个开关可以在对话框上显示“新建文件夹”按钮。它会这样使用:

$MyDST = Get-FolderPath "Select the destination folder:" "E:\Sorted Downloads" -NewFolderButton

我建议在 While 循环中使用它来强制用户选择事物,或者如果 $MySRC 或 $MyDST 为空,则放入 If 以退出脚本。

While([string]::IsNullOrWhiteSpace($MySRC)){
    $MySRC = Get-FolderPath "Select source folder or drive:" "$env:USERPROFILE\Downloads"
}

【讨论】:

  • 你正在开发什么版本?我的Win7上好像没有安装。我会尝试更新 PS 并会回复这个...不过谢谢你的帖子!
  • 我在 Win7 上的 PowerShell v3 和 v4 以及 Win8.1 上的 PowerShell v4 上使用过这个
  • 甜蜜!经过一些调试(也就是当您更改脚本中的某些内容时,Powershell ISE v4 不会将更新后的脚本处理为干净状态),我能够让它同时选择 MyDST 和 MySRC 并在大约 20 秒内正确运行(移动几个已知的文件来回)。非常感谢@TheMadTechnician!
猜你喜欢
  • 1970-01-01
  • 2010-09-23
  • 1970-01-01
  • 1970-01-01
  • 2020-04-08
  • 1970-01-01
  • 2018-10-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多