Unwanted spaces in filepath concatenation inside a function

This topic contains 7 replies, has 2 voices, and was last updated by  Olaf Soyk 3 months, 1 week ago.

  • Author
    Posts
  • #93367

    Jack Falvey IV
    Participant
    [string]$FileDS = Get-Date -Format "MMddyyyy"
    [string]$outFile = $outpath+"\"+$computername+"_"+$FileDS+".csv"
    New-Item -ItemType file $outfile -Force
    $object | export-csv -path $outfile

    This is a section of a function I'm using for a software inventory I'm running against remote machines. As it stands, the output adds unnecessary spaces between each value returned in these variables, and throwing the following error:

    New-Item : Could not find a part of the path 'filepath \ prod-adcweb1 _ 20180209 .csv'.
    At filepath\GetInstalledApplication.ps1:181 char:1
    + New-Item -ItemType file $outfile -Force
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : WriteError: (C:\users\username_ 20180209 .csv:String) [New-Item], DirectoryNotFoundException
        + FullyQualifiedErrorId : NewItemIOError,Microsoft.PowerShell.Commands.NewItemCommand
    
    export-csv : Could not find a part of the path 'filepath \ prod-adcweb1 _ 20180209 .csv'.
    At filepath\GetInstalledApplication.ps1:182 char:11
    + $object | export-csv -path $outfile
    +           ~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : OpenError: (:) [Export-Csv], DirectoryNotFoundException
        + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ExportCsvCommand

    I've been staring at this too long, wondering if someone else has an answer as to how I can remove these spaces. Thank you in advance.

  • #93373

    Olaf Soyk
    Participant

    Did you consider using Join-Path? Give this a try:

    [string]$FileDS = Get-Date -Format "_yyyyMMdd"
    [string]$outFile = = Join-Path -Path $outpath -ChildPath ($computername + $FileDS + '.csv')
    • #93376

      Jack Falvey IV
      Participant

      I'm getting the exact same error despite specifically calling for no spacing, seen here:

      [string]$FileDS = Get-Date -Format "_yyyyMMdd"
      [string]$outFile = Join-Path -Path $outpath -ChildPath ($computername + $FileDS + '.csv')
      New-Item -ItemType file $outfile -Force
      $object | export-csv -path $outfile

      Error:

      New-Item : Could not find a part of the path 'C:\filepath \ prod-adcweb1 _ 20180209 .csv'.
      At C:\filepath\GetInstalledApplication.ps1:181 char:1
      + New-Item -ItemType file $outfile -Force
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : WriteError: (C:\Users\username..._ 20180209 .csv:String) [New-Item], DirectoryNotFoundException
          + FullyQualifiedErrorId : NewItemIOError,Microsoft.PowerShell.Commands.NewItemCommand
      
      export-csv : Could not find a part of the path 'C:\filepath \ prod-adcweb1 _ 20180209 .csv'.
      At C:\filepath\GetInstalledApplication.ps1:182 char:11
      + $object | export-csv -path $outfile
      +           ~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : OpenError: (:) [Export-Csv], DirectoryNotFoundException
          + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ExportCsvCommand
    • #93379

      Olaf Soyk
      Participant

      So it seems to me that the cause for this error is in your $outpath. Where/how do you get this?

    • #93381

      Jack Falvey IV
      Participant

      The function is Get-InstalledApplication, borrowed from elsewherenon the web. $outpath is a parameter of the cmdlet.

    • #93387

      Olaf Soyk
      Participant

      ?? I meant how you fill in the variable $Outpath? You have to have an assignment somewhere in your code.

    • #93391

      Jack Falvey IV
      Participant

      Sorry, stepped away for a few.

      For reference, this is the code I'm modifying to suit my needs:

      https://social.technet.microsoft.com/wiki/contents/articles/34637.powershell-list-and-export-installed-programs-local-or-remote.aspx#Code

      The only place outpath is filled is when I run the cmdlet, seen here:

      Get-InstalledApplication -computername computername -outputtype CSV -outpath C:\filepath\filename.csv

    • #93397

      Olaf Soyk
      Participant

      OK. Even if I think that there's a lot of room for improvemnt in this code – if you change the initial param block to the following it works at least:

      Param(
      [Parameter(Mandatory=$true)]
      [string[]]$Computername,
      [String]$OutputType,
      [string]$outpath
      )

You must be logged in to reply to this topic.