I made a function and I thought it worked well. In my primary script I am importing the function and then calling it. The idea of the function is simply to make a filename with a path. At the end of the function I added a portion that would check if the directory exists and if not create it.
Here are the problems.
- I get no errors.
- The directory is not always created`.
- When the primary script runs and attempts to export the csv it doesn't because the directory doesn't exist. 3a) if I create the directory everything works.
The folder it tries to create is "C:\Reports" but I am an admin on the computer and I understand it should be in special folders. I suppose I can force that if you folks want. But I would think I would get an error if the directory could not create as written below.
Portion of the function that should be working.
$ProcessError = $null
IF (!(test-path $DirectoryName)) {
New-Item -Path $DirectoryName -ItemType Directory -ErrorAction SilentlyContinue -ErrorVariable ProcessError -Force
If ($ProcessError) {
Write-Warning "Problem trying to create $DirectoryName."
}
Return [string]$FullPath
}
This is the primary call to the function and what is strange is I get no error here as well in section 2 primary below!!
section 1 primary
If ($SimChange -eq $True) {
$ResultFileName = MRNAP -ReportName "ExampleGoogleLicenseReport" -Move -JustDate
}
Else {
$ResultFileName = MRNAP -ReportName "GoogleLicenseReport" -Move -JustDate
}
Section 2 primary
$ProcessError = $null
$Results | Export-Csv $ResultFileName -NoTypeInformation -ErrorVariable ProcessError -ErrorAction SilentlyContinue
If ($ProcessError) {
Write-Warning "Unable to save report output to $ResultFileName"
}
url to function that creates the filename. https://github.com/dcazman/MRNAP/blob/main/MRNAP.ps1
I find this all very frustrating and Section 2 primary above. Should be giving me that warning as written....I thought.