PowerShell function to prompt GUI for admins to select input file – import CSV;
If you are looking for a function to prompt users to select input file instead of defining a variable, here is one way of doing it. This function is very useful and it will help your administrators not to make mistake, assuming correct input file is selected.
#(1)-.Function to Prompt GUI to select input file
$InitialDir = "C:\Users\$env:USERNAME\Documents\CSV_Files\"
#(2)_. Create CSV folder
If (!(Test-Path $InitialDir)) {
New-Item -ItemType Directory -Force $InitialDir | Out-Null
}
Function Get-FileName ($InitialDir)
{
[System.Reflection.Assembly]::LoadWithPartialName('System.windows.forms') | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $InitialDir
$OpenFileDialog.filter = 'All files (*.*)| *.*'
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
}
#(3)_.Start reading CSV file
write-host 'You will need to locate <CSV> file for bulk import' -ForegroundColor Yellow
read-host 'Press <ENTER> to contiue'
$csvfile = Get-FileName -initialDirectory $InitialDir
$Mylist = Import-Csv -path $csvfile
|
Azure Solutions Architect
AWS Certified Cloud Practitioner
Azure Certified Security Engineer Associate
No comments:
Post a Comment