提示用户的 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')