Quantcast
Channel: psappdeploytoolkit Discussions Rss Feed
Viewing all articles
Browse latest Browse all 1769

New Post: Function Execute-Process in 3.2.0 Beta

$
0
0
Yup, got the same result as you when using that path. Same issue as before when we were actually passing a valid path. It is trying to Test-Path after joining two fully qualified paths so the path is malformed. I agree, the code has gotten quite long, but it has some solid error trapping and expands files to the fully qualified path if they are in a directory from the Path environment variable (this will also ensure that we have a WorkingDirectory when we execute such executables).

Also, I don't think that "Test-Path" with -ErrorAction Stop is the problem. The fact that we pass a path to the cmdlet which has two fully qualified paths makes it fail. For example, we are passing a path such as this to it when it fails: "C:\scriptFolder\Files\C:\Windows\System32\nonsense.exe".

Anyways, I think the below code should now resolve all of the various scenarios you've been breaking this function with :).
        ## Validate and find the fully qualified path for the $FilePath variable.
        ## If the file is in the Files subdirectory of the script directory, set the full path to the Files subdirectory.
        If (Test-Path -Path $FilePath -ErrorAction 'Stop')
        {
            Write-Log "`$FilePath contains a valid path [$FilePath], continue"
        }
        Else
        {
            # The first directory to search will be the 'Files' subdirectory of the script directory
            [array]$PathFolders     = $dirFiles
            # Add the current location of the console (Windows always searches this location first)
            [array]$PathFolders     = $PathFolders + (Get-Location).Path
            # Then search all of the paths in the Path environment variable; make sure we only have unique folder locations
            [array]$PathFolders    += $($env:PATH).Trim(';')           |
                                      ForEach-Object { $_.Split(';') } |
                                      ForEach-Object { $_.Trim() }     |
                                      ForEach-Object { $_.Trim('"') }  |
                                      Select-Object -Unique
            # File extensions, in the order that Windows uses to search for files, for when the file extension is not specified
            [array]$PathExtensions  = $($env:PATHEXT).Trim(';')        |
                                      ForEach-Object { $_.Split(';') } |
                                      ForEach-Object { $_.Trim() }     |
                                      ForEach-Object { $_.Trim('"') }  |
                                      ForEach-Object { $_.ToLower() }  |
                                      Select-Object -Unique
            # Initialize variable
            [string]$FullyQualifiedPath
            
            # If the $FilePath variable contains a file extension
            If ($FilePath.Contains('.') -and (-not $FilePath.Contains(':')))
            {
                ForEach ($Folder in $PathFolders)
                {
                    If (-not [string]::IsNullOrEmpty($Folder))
                    {
                        If (Test-Path -Path (Join-Path -Path $Folder -ChildPath $FilePath -ErrorAction 'Stop') -ErrorAction 'Stop')
                        {
                            $FullyQualifiedPath = Join-Path -Path $Folder -ChildPath $FilePath -ErrorAction 'Stop'
                            Break
                        }
                    }
                }
            }
            # If the $FilePath variable does not contain a file extension
            ElseIf (-not $FilePath.Contains(':'))
            {
                ForEach ($Folder in $PathFolders)
                {
                    If (-not [string]::IsNullOrEmpty($Folder))
                    {
                        ForEach ($Extension in $PathExtensions)
                        {
                            If (-not [string]::IsNullOrEmpty($Extension))
                            {
                                If (Test-Path -Path (Join-Path -Path $Folder -ChildPath $($FilePath + $Extension) -ErrorAction 'Stop') -ErrorAction 'Stop')
                                {
                                    $FullyQualifiedPath = Join-Path -Path $Folder -ChildPath $($FilePath + $Extension) -ErrorAction 'Stop'
                                    Break
                                }
                            }
                        }
                    }
                }
            }
            
            If (-not [string]::IsNullOrEmpty($FullyQualifiedPath))
            {
                Write-Log "`$FilePath with value [$FilePath] successfully resolved to fully qualified path [$FullyQualifiedPath]"
                $FilePath = $FullyQualifiedPath
            }
            Else
            {
                Write-Log "`$FilePath contains an invalid path [$FilePath]"
                Throw "`$FilePath contains an invalid path [$FilePath]"
            }
        }

Viewing all articles
Browse latest Browse all 1769

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>