提示用户的Powershell脚本



如何在脚本中添加类似于重命名的删除功能?它在哪里打开文件浏览器并允许用户选择要删除的文件?这可能吗?还是我必须换一种方式?我需要选项3的帮助。

}
#now start the loop calling the function named Show-Menu
do {
Show-Menu
#ask the user to make a selection and read this in as a variable
$options = Read-Host "Enter your choice"
switc
#Renames a folder
'2' {
$
# Se = ($renamefolder -split "\")[-1]
# Change our path so the directories can be referenced more easily via
until ($options -eq 'q')

它非常简单(注意下面的代码(。我想指出的几点。

我已经重命名了一个函数来复制它的结果

如果在脚本中选择文件夹浏览器对话框时单击Cancel,则会更进一步。对于do {} while ()循环,它不会:(

function Get-FolderPath() {  
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null

$OpenFileDialog = New-Object System.Windows.Forms.FolderBrowserDialog
do {
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.SelectedPath
}
while (!($OpenFileDialog.SelectedPath))
}
#Creates a function called Show-Menu
function Show-Menu {
param (
[string]$Title = 'My Folder Menu'
)
# Clear-Host
Write-Host "==========Choose your option============="
Write-Host "1. Press '1' to Create a Folder"
Write-Host "2. Press '2' to Rename a Folder"
Write-Host "3. Press '3' to Delete a Folder"
Write-Host "Q. Press 'Q' to exit the script"
Write-Host "========================================="
}
#now start the loop calling the function named Show-Menu
do {
Show-Menu
#ask the user to make a selection and read this in as a variable
$options = Read-Host "Enter your choice"
switch ($options) {
#Creates a new directory
'1' {
#Get parent folder path
$parentPath = Get-FolderPath
#Get folder name
$folderName = Read-Host -Prompt 'Input new directory name'
#Create folder in parent path that has selected name 
New-Item (Join-Path $parentPath $folderName) -ItemType Directory | Out-Null
}
#Renames a folder
'2' {
$renamefolder = Get-FolderPath
$newFolderName = Read-Host 'Please enter the new name for the folder'
# Split the path and get the last item in the array
$oldFolderName = ($renamefolder -split "\")[-1]
#The same can be made using:
#$oldFolderName = (Split-Path $renamefolder -Leaf)

# Change our path so the directories can be referenced more easily via ./
Push-Location (Join-Path $renamefolder -ChildPath ..)
Move-Item -Path (Join-Path -Path ./ -ChildPath $oldFolderName) -Destination (Join-Path ./ -ChildPath $newFolderName)
# Return process back to the original path
}
#Deletes a folder
'3' {
#Get folder path
$folderToDelete = Get-FolderPath
#Remove the item :)
Remove-Item $folderToDelete -Verbose #-WhatIf #for testing
}
}
}
until ($options -eq 'q')

相关内容

  • 没有找到相关文章

最新更新