-3
foreach ( $newfile in $file ) 
{ 
   $b =  Get-CMDeploymentStatus -PackageId $newfile -StatusType Any | select PackageID
 Write-Output $b | Export-Csv -Path "C:\Users\PSM-6A1A000000000000\Documents\list.csv"
}

I am giving input to this with an input file which has number of package names listed and then I want to process it in such a way that the output comes one after the other right now I am getting an error as

Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null. At line:16 char:20 + Write-Output $b | Export-Csv -Path "C:\Users\PSM-6A1A000000000000\Documents\lis ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Export-Csv], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCommand

Itchydon
  • 2,572
  • 6
  • 19
  • 33
  • 1
    It is really hard to read your question and it looks like your problem is not reproducible, because we are missing `$file`. Please format every code snippet as code and provide a minimal working example that will produce your error. [Read more about how to ask good questions](https://stackoverflow.com/help/how-to-ask). – stackprotector Apr 29 '20 at 13:21

2 Answers2

0

Your code is assuming that you will have a result coming back from $b, if it does not though, you'll get an error because you're piping $b, which is null, into Export-CSV.

$null |export-csv c:\temp\1.csv
Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null.
At line:1 char:8
+ $null |export-csv c:\temp\1.csv
+        ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Export-Csv], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCs

You should add a 'Guard Clause' before you try to export.

if ($null -ne $b){
   Export-csv -Append -InputObject $b
}

At least this will continue executing. Now your next problem is to determine why $b would be empty...from my experience with CM, I bet you need to specify which property in your $file you need. Maybe that line should read:

$b = Get-CMDeploymentStatus -PackageId $newfile.PackageId -StatusType Any | select PackageID

FoxDeploy
  • 12,569
  • 2
  • 33
  • 48
0

Since you say "I am giving input to this with an input file which has number of package names listed", but your code uses PackageId..

It looks to me that this file contains a packageId, each on a single line.

Anyway, I don't see the code ever reading this file..

If my assumption about the text file is correct, try:

# read the content of the text file and loop through the lines
# collect the output from Get-CMDeploymentStatus in variable $result
$result = Get-Content -Path 'X:\TheFileWithPackageIds.txt' | ForEach-Object {
    # inside the ForEach-Object, the $_ automatic variable represents a single line from the text file
    Get-CMDeploymentStatus -PackageId $_ -StatusType Any | select PackageID
}

# output on screen
$result

# write to new CSV file
$result | Export-Csv -Path "C:\Users\PSM-6A1A000000000000\Documents\list.csv" -NoTypeInformation
Theo
  • 57,719
  • 8
  • 24
  • 41