I'm attempting to create a function with an optional parameter (called a switch?) that will export the results to a .csv. Below is what I've managed to cobble together and it mostly works. If I don't specify -ExportCsv, the Export-Csv command requests a path be input. If I DO specify -ExportCsv, then it just runs and outputs the results on screen. I'd like to run the Get-DisabledAccounts function and it display on screen. If the -ExportCsv parameter is specified, I want it to redirect output to a .csv file and bring up the save-file dialog. Where am I going wrong here?
Function Get-FileName
{ PARAM (
$InitialDirectory
)
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$SaveFileDialog = New-Object System.Windows.Forms.SaveFileDialog
$SaveFileDialog.initialDirectory = $initialDirectory
$SaveFileDialog.filter = "Comma Separated Values (*.csv)|*.csv|All Files (*.*)|(*.*)"
$SaveFileDialog.ShowDialog() | Out-Null
$SaveFileDialog.filename
$InitialDirectory= "$env:UserProfile\Desktop"
}
Function Get-DisabledAccounts
{ PARAM (
[string]$SearchBase="OU=Unicorn,DC=Squatty,DC=Potty",
[string]$LastLogonDate=(Get-Date).AddYears(-1).ToString('MM/dd/yyy'),
[switch]$ExportCsv=(Export-Csv -NoTypeInformation -Encoding UTF8 -Path Get-FileName)
)
Get-ADUser -SearchBase "$SearchBase" -Filter * -Properties sAMAccountName, LastLogonDate, HomeDirectory | Where-Object {$_.Enabled -eq "$null" -and $_.LastLogonDate -lt "$LastLogonDate"} | Select
Name, sAMAccountName, LastLogonDate, HomeDirectory
}