PowerShell - 使用文件资源管理器让用户 select 路径

PowerShell - Getting the user to select a path with File Explorer

所以我目前正在制作一个 PowerShell 脚本来放置在我们的 DC 上,它将在 Active Directory 中创建用户,它将使用函数来显示菜单和执行命令,并将允许创建单个用户或 CSV。

我正在努力让它尽可能地成为白痴证明,并尽可能地消除人为错误。我想做的是用文件资源管理器 window 提示用户,这样当 CSV 函数查找 CSV 本身的路径以导入时,用户首先在资源管理器中打开它,无需键入完整的文件路径。

目前我的CSV函数供参考:

function csv {
 $Path = read-host "Where is the CSV stored? Make sure it is in a Readable Location"
 # Here is where The user would be prompted for the path of the CSV
 $Users = Import-Csv -Path $Path            
 foreach ($User in $Users)            
 {            
 $Displayname = $User.'Firstname' + " " + $User.'Lastname'            
 $UserFirstname = $User.'Firstname'            
 $UserLastname = $User.'Lastname'            
 $OU = $User.'OU'            
 $SAM = $User.'SAM'            
 $UPN = $User.'Firstname' + "." + $User.'Lastname' + "@" + $User.'Maildomain'            
 $Description = $User.'Description'            
 $Password = $User.'Password'            
 New-ADUser -Name "$Displayname" -DisplayName "$Displayname" -SamAccountName $SAM -UserPrincipalName $UPN -GivenName "$UserFirstname" -Surname "$UserLastname" -Description "$Description" -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) -Enabled $true -Path "$OU" -ChangePasswordAtLogon $false –PasswordNeverExpires $true            
 }
}

这就是我的问题,这是个好主意还是每次都使用更新的路径编辑脚本会更好?

感谢您对此的帮助!

迪伦

我使用以下函数,可调用,例如,$FileName = Select-File -StartingFolder "C:\Users\Michael\Documents" -NameFilter "CSV Files (*.CSV)|*.CSV"

function Select-File {

    [CmdletBinding()]

    param(
        [Parameter(ParameterSetName="Single")]
        [Parameter(ParameterSetName="Multi")]
        [Parameter(ParameterSetName="Save")]
        [string]$StartingFolder = [environment]::getfolderpath("mydocuments"),

        [Parameter(ParameterSetName="Single")]
        [Parameter(ParameterSetName="Multi")]
        [Parameter(ParameterSetName="Save")]
        [string]$NameFilter = "All Files (*.*)|*.*",

        [Parameter(ParameterSetName="Single")]
        [Parameter(ParameterSetName="Multi")]
        [Parameter(ParameterSetName="Save")]
        [switch]$AllowAnyExtension,

        [Parameter(Mandatory=$true,ParameterSetName="Save")]
        [switch]$Save,

        [Parameter(Mandatory=$true,ParameterSetName="Multi")]
        [Alias("Multi")]
        [switch]$AllowMulti
    )

    if ($Save) {
        $Dialog = New-Object -TypeName System.Windows.Forms.SaveFileDialog
    } else {
        $Dialog = New-Object -TypeName System.Windows.Forms.OpenFileDialog
        if ($AllowMulti) {
            $Dialog.Multiselect = $true
        }
    }
    if ($AllowAnyExtension) {
        $NameFilter = $NameFilter + "|All Files (*.*)|*.*"
    }
    $Dialog.Filter = $NameFilter
    $Dialog.InitialDirectory = $StartingFolder
    [void]($Dialog.ShowDialog())
    $Dialog.FileNames
}