![]()
# 引入Windows Forms组件
Add-Type -AssemblyName System.Windows.Forms
# 创建主窗体
$form = New-Object -TypeName System.Windows.Forms.Form
$form.StartPosition = "CenterScreen"
$form.Size = New-Object -TypeName System.Drawing.Size(530, 600)
$form.Text = "批量新建文件"
# 参数选择
$groupBoxLeft = New-Object -TypeName System.Windows.Forms.GroupBox
$groupBoxLeft.Location = New-Object -TypeName System.Drawing.Point(10, 10)
$groupBoxLeft.Size = New-Object -TypeName System.Drawing.Size(240, 120)
$groupBoxLeft.Text = "配置信息"
$labelFileType = New-Object -TypeName System.Windows.Forms.Label
$labelFileType.Text = "选择文件类型"
$labelFileType.AutoSize = $true
$labelFileType.Location = New-Object -TypeName System.Drawing.Point(10, 25)
$comboBox = New-Object -TypeName System.Windows.Forms.ComboBox
$comboBox.Location = New-Object -TypeName System.Drawing.Point(100,20)
$comboBox.Items.AddRange(@(".xlsx", ".txt", ".docx"))
$labelFileCount = New-Object -TypeName System.Windows.Forms.Label
$labelFileCount.Text = "文件数量"
$labelFileCount.AutoSize = $true
$labelFileCount.Location = New-Object -TypeName System.Drawing.Point(10, 55)
$textBoxFileCount = New-Object -TypeName System.Windows.Forms.TextBox
$textBoxFileCount.Size = New-Object -TypeName System.Drawing.Size(120, 30)
$textBoxFileCount.Location = New-Object -TypeName System.Drawing.Point(100, 50)
$labelFilePath = New-Object -TypeName System.Windows.Forms.Label
$labelFilePath.Text = "创建路径"
$labelFilePath.AutoSize = $true
$labelFilePath.Location = New-Object -TypeName System.Drawing.Point(10, 85)
$textBoxFilePath = New-Object -TypeName System.Windows.Forms.TextBox
$textBoxFilePath.Size = New-Object -TypeName System.Drawing.Size(120, 30)
$textBoxFilePath.Location = New-Object -TypeName System.Drawing.Point(100, 80)
$groupBoxLeft.Controls.AddRange(@($labelFileType, $comboBox, $labelFileCount, $textBoxFileCount, $labelFilePath, $textBoxFilePath))
# 执行区域
$groupBoxRight = New-Object -TypeName System.Windows.Forms.GroupBox
$groupBoxRight.Location = New-Object -TypeName System.Drawing.Point(260, 10)
$groupBoxRight.Size = New-Object -TypeName System.Drawing.Size(240, 120)
$groupBoxRight.Text = "功能"
$buttonSubmit = New-Object -TypeName System.Windows.Forms.Button
$buttonSubmit.Text = "开始创建"
$buttonSubmit.Size = New-Object -TypeName System.Drawing.Size(220, 90)
$buttonSubmit.Location = New-Object -TypeName System.Drawing.Point(10, 20)
$buttonSubmit.Cursor = [System.Windows.Forms.Cursors]::Hand
$groupBoxRight.Controls.AddRange(@($buttonSubmit))
# 日志部分
$groupBoxBottem = New-Object -TypeName System.Windows.Forms.GroupBox
$groupBoxBottem.Text = "日志功能"
$groupBoxBottem.Size = New-Object -TypeName System.Drawing.Size(490, 400)
$groupBoxBottem.Location = New-Object -TypeName System.Drawing.Point(10, 150)
$logBoxBottem = New-Object -TypeName System.Windows.Forms.TextBox
$logBoxBottem.Location = New-Object -TypeName System.Drawing.Point(10, 20)
$logBoxBottem.Size = New-Object -TypeName System.Drawing.Size(470, 370)
$logBoxBottem.Multiline = $true
$logBoxBottem.ReadOnly = $true
$logBoxBottem.WordWrap = $true
$groupBoxBottem.Controls.Add($logBoxBottem)
$buttonSubmit.Add_Click({
$fileType = $comboBox.SelectedItem
$fileCount = $textBoxFileCount.Text
$filePath = $textBoxFilePath.Text
$logBoxBottem.AppendText("$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') 开始执行创建任务【文件类型 $fileType 创建数量 $fileCount】`r`n")
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false
$word = New-Object -ComObject Word.Application
foreach($item in 1..$fileCount){
$fileName = "$item$fileType"
switch ($fileType) {
".xlsx" {
$workbook = $excel.Workbooks.Add()
$targetPath = Join-Path -Path $filePath -ChildPath $fileName
$workbook.SaveAs($targetPath)
$workbook.Close($false)
}
".docx" {
$document = $word.Documents.Add()
$targetPath = "$filePath\$fileName"
$document.SaveAs([ref] $targetPath)
$document.Close($false)
}
default {
$null = New-Item -Path $filePath -Name $fileName -ItemType File
}
}
}
if($excel){
$excel.Quit()
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($excel) | Out-Null
}
if($word){
$word.Quit()
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($word) | Out-Null
}
$logBoxBottem.AppendText("$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') 创建任务执行完成【文件类型 $fileType 创建数量 $fileCount】`r`n")
})
$form.Controls.AddRange(@($groupBoxLeft, $groupBoxRight, $groupBoxBottem))
$form.Add_Shown({$form.Activate()})
[void]$form.ShowDialog()