0

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.

  1. I get no errors.
  2. The directory is not always created`.
  3. 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.

dcaz
  • 847
  • 6
  • 15
  • 1
    `Return [string]$FullPath`... $FullPath is not defined so that function returns $null. Also, if the path in `$DirectoryName` DOES exist, it will return nothing. – Theo Nov 23 '21 at 16:36
  • @Theo I didn't add the entire function just a section. That is why I gave the url but f you think more value to the question would be the entire function I will add it. ```$FullPath``` gets created before the lines above. – dcaz Nov 23 '21 at 16:46
  • 1
    Have you tried removing `-ErrorAction SilentlyContinue`? Would be interesting to see exactly which error it throws that doesn't get appended to the error variable :) – Mathias R. Jessen Nov 23 '21 at 16:56
  • @MathiasR.Jessen I will. I thought that was the point of the Variable. – dcaz Nov 23 '21 at 16:58
  • 1
    It is, and what you've shown _should_ work - but maybe you've accidentally hit a bug that only occurs on certain error types from the io/filesystem APIs, who knows :) – Mathias R. Jessen Nov 23 '21 at 17:02
  • @MathiasR.Jessen What really bothers me is when I export in the primary script there is no error either. I am going to drop the ```silentlycontinue``` and related variable and try it. – dcaz Nov 23 '21 at 17:11
  • @MathiasR.Jessen I did some research and https://stackoverflow.com/questions/16906170/create-directory-if-it-does-not-exist Talks about the same thing. What is really strange is ```$fullPath``` above is becoming an array even though the line says ```return[string]$filename``` – dcaz Nov 23 '21 at 21:03
  • even deeper https://stackoverflow.com/questions/10286164/function-return-value-in-powershell – dcaz Nov 23 '21 at 21:15
  • I don't understand this fully and I will leave this unanswered until tomorrow in hopes someone can explain or do better. The comments won't let me post the final code snip that works. I used ```|out-null'' and a variable with the Dir create. All to save the final Variable. https://stackoverflow.com/questions/16906170/create-directory-if-it-does-not-exist And https://stackoverflow.com/questions/10286164/function-return-value-in-powershell – dcaz Nov 23 '21 at 21:40
  • 1
    This seems to be a bug as Mathias pointed out, the Error Variable should be populated and it is not. You should default to `try` `catch` which will work 100%. – Santiago Squarzon Nov 24 '21 at 00:43

1 Answers1

0

In the comments there is chat about a bug and using try catch. Additionally, we have talked about this problem on stack before with Create directory if it does not exist and Function return value in PowerShell

What I did for now that works.

 IF (!(test-path $DirectoryName)) {
            $Dircreate = New-Item -ItemType Directory -Force -Path $($DirectoryName.Substring(0, 3)) -Name $($DirectoryName.Substring(3)) -ErrorAction SilentlyContinue -ErrorVariable ProcessError | Out-Null

            If ($ProcessError) {
                Write-Warning "Problem trying to create $DirectoryName."
                [bool]$Created = $False
            }
            Else {
                [bool]$Created = $true
            }
        }

        If ($Created) {
            If ($FullPath -as [string]) {
                Return [string]$FullPath
            }
            Else {
                Return [string]$FullPath[1]
            }
        }

This above doesn't use a try catch but I will give that a shot later. For some bug reason creating the directory was changing $fullpath from a string to an array and that is why the test above works using the -as [string]

dcaz
  • 847
  • 6
  • 15