Author Posts

February 9, 2018 at 4:27 pm

[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.

February 9, 2018 at 4:36 pm

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')

February 9, 2018 at 4:42 pm

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

February 9, 2018 at 4:51 pm

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

February 9, 2018 at 4:58 pm

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

February 9, 2018 at 5:06 pm

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

February 9, 2018 at 5:59 pm

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

February 9, 2018 at 6:35 pm

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
)