首页 > 解决方案 > Importing a .csv after prompting for file path for AD account creation in Powershell

问题描述

I'm completely new to scripting and I got thrown into creating an account creator for my company, since the previous employee that did this left.

Currently I have it set to prompt the end user which file they would like to import into the script via the below script.

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

$OpenFIleDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.InitialDirectory = $InitialDirectory
$OpenFileDialog.Filter = "CSV (*.csv) | *.csv"
$OpenFileDialog.ShowDialog() | Out-Null
}
$Path = Get-Filename $OpenFileDialog.Filename

This successfully prompts the user which file to use. The issue is after this however. I don't believe it is then importing the file. In my script that I know works, I have it set to a specific file and it then imports that file.

$users = Import-CSV 'filepath'

Any help would be greatly appreciated for this newbie.

标签: powershell

解决方案


Which variable are you actually using for the import? This works fine:

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

$OpenFIleDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.InitialDirectory = $InitialDirectory
$OpenFileDialog.Filter = "CSV (*.csv) | *.csv"

$OpenFileDialog.ShowDialog() | Out-Null
$Path = $OpenFileDialog.Filename

$MyListOfUsers = Import-CSV -Path $Path

推荐阅读