File Path Parameter

This topic contains 7 replies, has 5 voices, and was last updated by Profile photo of Curtis Smith Curtis Smith 3 weeks, 2 days ago.

  • Author
    Posts
  • #74096
    Profile photo of Brian Clanton
    Brian Clanton
    Participant

    I have composed a script that will create a spreadsheet based on Active Directory User Groups but I need to prompt the user for a Path so I know where to save the Excel sheet report it generates.

    I can test the path name individually and then create the file if the path validates; however, I want to give the user the option of NOT putting in a path statement and just declaring a file name, then the script would just save it to the current working directory.

    The only thing I can think of is to test to see if ANY back slashes are in the users input string, but I was wondering if anyone has used a different method.

    Here is some 'test' commands I have been working with to see what I can accomplish:

    Param
        (
            [Parameter(Mandatory=$true)]
            [string]
            $filePath
            
        )
    
    Function TestPath($filpath){
        
        $ParentFolders = Split-Path -Path $filepath
        $File = Split-Path $filePath -Leaf
         if (Test-Path $ParentFolders){
            write-host "Create file $file or save full path to variable"
            New-Item $filepath
          }
          else
          {
            write-host "Invalid Path"
            Exit
          }
    }
    
    TestPath $filePath
    Remove-Item $filePath
    $ParentFolder = $null
    $file = $null
  • #74098
    Profile photo of Ben Camareno
    Ben Camareno
    Participant

    You should have 2 params:
    1 filepath
    2 filename

    The default of filepath $pwd which is the current working directory. So if the user doesn't enters a path, the file will be saved in $pwd

    Then just join filepath and filename.

    At The end, write-output the full path where the file was saved.

  • #74101
    Profile photo of random commandline
    random commandline
    Participant

    You can export Active Directory objects to csv via Export-csv.

    Get-ADUser | Select-Object Property1,Property2 | 
    Export-Csv -NoTypeInformation -Path .\AD.csv # Or \\path\AD.csv
    
    • #74105
      Profile photo of Brian Clanton
      Brian Clanton
      Participant

      There is a lot of special formatting involved in the creation of the spreadsheet. We will submit these reports to our clients so they can use the form to fill out additional fields. Plus the script needs to have implicit emoting to domain controller so other technicians can run it.

  • #74108
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant

    Did you think about using the standard select folder dialog box of windows?

    http://www.powershellmagazine.com/2013/06/28/pstip-using-the-system-windows-forms-folderbrowserdialog-class/

    This way you could be sure that the path is valid.

  • #74113
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    I really don't completely understand the challenge here. You can use Test-Path which can accept an absolute path or a relative path.

    PS C:\Temp> Test-Path "file.csv"
    Test-Path "c:\temp\file.csv"
    True
    True

    So the user can specify either a full path, a relative path, or just a file name and it should be able to test it fine. Then, if for some reason you need the full path to the file after it is created, you can user resolve-path, or if you need to get the current directory location you can user get-location. So I would need to understand more about the challenge you are facing with not being able to specify a filename only.

  • #74167
    Profile photo of Brian Clanton
    Brian Clanton
    Participant

    To keep the code at a minimum I have the 'ValidateScript as part of my parameter list and a separate function to ensure that the technician enters a filename that has a correct file extension.

    Param
        (
            [ValidateScript({Test-Path $_})] 
            [string]
            $filePath = $pwd,
            [Parameter(Mandatory=$true)]
            [string]
            $Client,
            [Parameter(Mandatory=$true)]
            $filename
                
        )
    
    #Tests for Proper File extension
    Function TestfileInput ($filename) {
        $extn = [IO.Path]::GetExtension($filename)
        if ($extn -eq ".xls" -or $extn -eq ".xlsx"){
            Write-Output "Valid Input"
        }
        else{
            Write-Warning "Filename must end in .xlsx extension Try again"
            Exit
        }
    }
  • #74300
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    So if I'm understanding correctly, you are not wanting to check the existence of the file, but rather the parent folder where the file would be created. How about something like this?

        Param(
            [ValidateScript({Test-Path -Path (Split-Path -Path ([io.path]::GetFullPath($_)) -Parent)})]
            [string]
            $filePath,
            [Parameter(Mandatory=$true)]
            [string]
            $Client
        )
        if ([IO.Path]::GetExtension($filePath) -match ".xls|.xlsx") {
            Write-Output "Valid Input"
        } Else {
            Write-Warning "Filename must end in .xlsx extension Try again"
        }

You must be logged in to reply to this topic.