[HELP] Export-Csv : Cannot bind argument to parameter InputObject

I don’t know what happened with my previous post. I did edit it and all of a sudden it’s gone.

The script is running and the export to csv is correct. But I would like to polish it up without an error.

I am getting the following error when I run the PS script:

Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null. At C:\scripts\MPermissions.ps1:105 char:21
[CmdletBinding()]</code>
<code>Param(</code>
<code>[string]$MailboxId = '*',</code>
<code>[string]$CsvFileName = 'MailboxPermissions.csv'</code>

<code>)</code>

<code>$ScriptDir = Split-Path -Path $script:MyInvocation.MyCommand.Path</code>
<code>$ScriptName = $MyInvocation.MyCommand.Name</code>

<code># build CSV full path to store CSV in script directory</code>
<code>$OutputFile = Join-Path -Path $ScriptDir -ChildPath $CsvFileName</code>

<code>Write-Verbose -Message $OutputFile</code>

<code># Fetch mailboxes of type UserMailbox only</code>
<code>$Mailboxes = Get-Mailbox -RecipientTypeDetails 'UserMailbox' -Identity $MailboxId -ResultSize Unlimited | Sort-Object</code>

<code>$result = @()</code>

<code># counter for progress bar</code>
<code>$MailboxCount = ($Mailboxes | Measure-Object).Count</code>
<code>$count = 1</code>

<code>ForEach ($Mailbox in $Mailboxes) {</code>

<code>$Alias = '' + $Mailbox.Alias # Use Alias property instead of name to ensure 'uniqueness' passed on to Get-MailboxFolderStatistics</code>

<code>$DisplayName = ('{0} ({1})' -f $Mailbox.DisplayName, $Mailbox.Name)</code>

<code>$activity = ('Working... [{0}/{1}]' -f $count, $mailboxCount)</code>
<code>$status = ('Getting folders for mailbox: {0}' -f $DisplayName)</code>
<code>Write-Progress -Status $status -Activity $activity -PercentComplete (($count/$MailboxCount)*100)</code>

<code># Fetch folders</code>
<code>$Folders = Get-MailboxFolderStatistics $Alias | ForEach-Object {$_.folderpath} | ForEach-Object{$_.replace('/','\')}</code>

<code>ForEach ($Folder in $Folders) {</code>

<code># build folder key to fetch mailbox folder permissions</code>
<code>$FolderKey = $Alias + ':' + $Folder</code>

<code># fetch mailbox folder permissions</code>
<code>$Permissions = Get-MailboxFolderPermission -Identity $FolderKey -ErrorAction SilentlyContinue</code>

<code># store results in variable</code>
<code>$result += $Permissions | Where-Object {$_.User -notlike 'Default' -and $_.User -notlike 'Anonymous' -and $_.AccessRights -notlike 'None' -and $_.AccessRights -notlike 'Owner' } | Select-Object -Property @{name='Mailbox';expression={$DisplayName}}, FolderName, @{name='User';expression={$_.User -join ','}}, @{name='AccessRights';expression={$_.AccessRights -join ','}}</code>
<code>}</code>

<code># Increment counter</code>
<code>$count++</code>
<code>}</code>

<code># Export to CSV</code>
<code>$result | Export-Csv -Path $OutputFile -NoTypeInformation -Encoding UTF8 -Delimiter ';' -Force

@Rob Simmers I tried your edit, but that didn’t work. I am still getting the error.

I don’t know what happened with my previous post. I did edit it and all of a sudden it’s gone.
The edit was caught by the auto-moderator system and marked as spam. If your topic disappears please don't submit it again. Give the moderators a chance to deal with it. Submitting your topic multiple times creates a mess that we have to clean up.

I have similar issue. When I run the script in debug mode (F8) it works. But when I execute it directly via PS console as completed PS1 it gives me error “Export-Csv : Cannot bind argument to parameter ‘InputObject’ because it is null.”

# Find all DHCP Server in Domain
$DhcpServers = Get-DhcpServerInDC
#
foreach ($DHCPServer in $DhcpServers.DnsName){
if (Test-Connection -BufferSize 32 -Count 1 -ComputerName $dhcpserver -Quiet){
$ErrorActionPreference = "SilentlyContinue"
$Scopes = Get-DhcpServerv4Scope -ComputerName $DHCPServer
#For all scopes in the DHCP server, get the scope options and add them to $LIstofSCopesandTheirOptions
foreach ($Scope in $Scopes){
$LIstofSCopesandTheirOptions += Get-DHCPServerv4OptionValue -ComputerName $DHCPServer -ScopeID $Scope.ScopeId | Select-Object @{label="DHCPServer"; Expression= {$DHCPServer}},@{label="ScopeID"; Expression= {$Scope.ScopeId}},@{label="ScopeName"; Expression= {$Scope.Name}},@{Name=’Value’;Expression={[string]::join(";", ($_.Value))}},*
}
$LIstofSCopesandTheirOptions += Get-DHCPServerv4OptionValue -ComputerName $DHCPServer | Select-Object @{label="DHCPServer"; Expression= {$DHCPServer}},@{Name=’Value’;Expression={[string]::join(";", ($_.Value))}},*
$ErrorActionPreference = "Continue"
}
}
#Now we have them all, output them
$LIstofSCopesandTheirOptions | Export-Csv -Path D:\EAADM\atrubajda\DhcpOptionsReport.csv -Force
#$ListofScopesandTheirOptions | Out-GridView

I “solved” this issue by switching to PowerShell version 7.1

sindbad97, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

When you post code, error messages, sample data or console output format it as code, please.
In the “Text” view you can use the code tags “PRE“, in the “Visual” view you can use the format template “Preformatted“. You can go back edit your post and fix the formatting – you don’t have to create a new one.
Thanks in advance.

The line is question appears to be here:

$result | Export-Csv -Path $OutputFile -NoTypeInformation -Encoding UTF8 -Delimiter ';' -Force

If the $result variable is null, then you could get an error like you posted. Basically, you should be checking to see if there is anything in $result before you attempt an export, something like this:

if ( $result ) {
   $result | Export-Csv -Path $OutputFile -NoTypeInformation -Encoding UTF8 -Delimiter ';' -Force 
}
else {
   'No results to export.'
}