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
I wrote some code to take care of the concerns I had in the above post where certain paths passed to the $FilePath variable were not being validated before attempting execution. The below code will expand file names to their fully qualified path if they exist in one of the folders in the Path environment variable. The code can also handle instances where a user may not specify the file extension for the file. In that instance, we will search for the file using the file extension in the PathExt environment variable (which is what Windows does as well).

Please use the below code and I think this should take care of this reported issue:
If (Test-Path -Path $FilePath -ErrorAction Stop)
{
    Write-Log "`$FilePath [$FilePath] is a valid path, 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(';')    | %{$_.Split(';')} | %{ $_.Trim() } | %{ $_.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(';') | %{$_.Split(';')} | %{ $_.Trim() } | %{ $_.Trim('"') } | %{ $_.ToLower()} | Select-Object -Unique
    # Initialize variable
    [string]$FullyQualifiedPath
    
    # If the $FilePath variable contains a file extension
    If ($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
    Else
    {
        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 [$FilePath] successfully resolved to fully qualified path [$FullyQualifiedPath]"
        $FilePath = $FullyQualifiedPath
    }
    Else
    {
        Write-Log "`$FilePath [$FilePath] contains an invalid path"
        Throw "`$FilePath [$FilePath] contains an invalid path"
    }
}

Viewing all articles
Browse latest Browse all 1769

Trending Articles



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