VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Cannot bind argument to parameter 'InputObject' because it is null.

Hi,

I am unable to get the Date information from VM from the below script as I am getting the below error.

Please help

Script

$incsv = Import-Csv .\$($folder)_OS_Info_$date.csv

$report = @()

$reportNotFound = @()

Foreach($vm in $incsv.Name)

{

    $getvm = Get-VM -Name $vm -ErrorAction SilentlyContinue

    if($getvm){

        $report += Invoke-Command -ComputerName $vm -Credential $Creds -ScriptBlock {$DT = Get-WmiObject -Class Win32_LocalTime

        $Times = New-Object PSObject -Property @{

      ServerName = $DT.__Server

      $DateTime = (Get-Date -Day $DT.Day -Month $DT.Month -Year $DT.Year -Minute $DT.Minute -Hour $DT.Hour -Second $DT.Second)

   }

       }

        "$($vm) information has been fetched"

           }

           else{

        $reportNotFound += $vm

        "$($vm) not found"

        }

}

$row | Add-Member -MemberType NoteProperty -Name 'DateTime' -Value $DateTime -PassThru

$report | Export-Csv -Path .\$($folder)_Date_Info_$date.csv -NoTypeInformation -UseCulture

$reportNotFound | Export-Csv -Path .\$($folder)_No_VMs_Date_Info_$date.csv -NoTypeInformation -UseCulture

Output

Add-Member : Cannot bind argument to parameter 'InputObject' because it is null.

At D:\myreports\Get_Date\get_date1.ps1:41 char:8

+ $row | Add-Member -MemberType NoteProperty -Name 'DateTime' -Value $D ...

+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidData: (:) [Add-Member], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddMemberCommand

A null key is not allowed in a hash literal.

    + CategoryInfo          : InvalidOperation: (System.Collections.Hashtable:Hashtable) [], RuntimeException

    + FullyQualifiedErrorId : InvalidNullKey

    + PSComputerName        : app01

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You mean like this?

$incsv = Import-Csv .\$($folder)_OS_Info_$date.csv

$report = @()

$reportNotFound = @()


Foreach ($row in $incsv) {

    $getvm = Get-VM -Name $row.Name -ErrorAction SilentlyContinue

    if ($getvm) {

        $vmDT = Invoke-Command -ComputerName $row.Name -Credential $Creds -ScriptBlock {

            $DT = Get-WmiObject -Class Win32_LocalTime

            Get-Date -Day $DT.Day -Month $DT.Month -Year $DT.Year -Minute $DT.Minute -Hour $DT.Hour -Second $DT.Second

        }

        "$($row.Name) information has been fetched"

    } else {

        $vmDT = ''

        $reportNotFound += $row.Name

        "$($row.Name) not found"

    }

    $row | Add-Member -MemberType NoteProperty -Name 'DateTime' -Value $vmDT

    $report += $row

}


$report | Export-Csv -Path .\$($folder)_Date_Info_$date.csv -NoTypeInformation -UseCulture

$reportNotFound | Export-Csv -Path .\$($folder)_No_VMs_Date_Info_$date.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

There are a couple of issues.
- the ScriptBlock is not returning anything
- there is no $row variable used

Try with these changes

$incsv = Import-Csv .\$($folder)_OS_Info_$date.csv

$report = @()

$reportNotFound = @()

Foreach ($vm in $incsv.Name) {

    $getvm = Get-VM -Name $vm -ErrorAction SilentlyContinue

    if ($getvm) {

        $report += Invoke-Command -ComputerName $vm -Credential $Creds -ScriptBlock {

            $DT = Get-WmiObject -Class Win32_LocalTime

            New-Object PSObject -Property @{

                ServerName = $DT.__Server

                DateTime  = (Get-Date -Day $DT.Day -Month $DT.Month -Year $DT.Year -Minute $DT.Minute -Hour $DT.Hour -Second $DT.Second)

            }

        }

        "$($vm) information has been fetched"

    } else {

        $reportNotFound += $vm

        "$($vm) not found"

    }

}


$report | Export-Csv -Path .\$($folder)_Date_Info_$date.csv -NoTypeInformation -UseCulture

$reportNotFound | Export-Csv -Path .\$($folder)_No_VMs_Date_Info_$date.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Hi LucD,

This is generating a new file, but I need DateTime to appended to same import file, how can I do that..

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Not sure what you are trying to do.
You want to append the DateTime to the filename.
Does that mean a separate file for each VM?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

I am creating a input file as below

Get-Folder $folder | get-vm | Where{$_.'PowerState' -eq 'PoweredOn' -and $_.Guest.OSFullName -match 'windows'} | select Folder, Name, @{N="IP_Address";E={@($_.guest.IPAddress[0])}}, @{N="OS"; E={@($_.guest.OSFullName)}} | Export-Csv -Path .\$($folder)_OS_Info_$date.csv -NoTypeInformation -UseCulture

in the same file, I would like to add a row with DateTime and output should be captured and appended in the same file.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You mean like this?

$incsv = Import-Csv .\$($folder)_OS_Info_$date.csv

$report = @()

$reportNotFound = @()


Foreach ($row in $incsv) {

    $getvm = Get-VM -Name $row.Name -ErrorAction SilentlyContinue

    if ($getvm) {

        $vmDT = Invoke-Command -ComputerName $row.Name -Credential $Creds -ScriptBlock {

            $DT = Get-WmiObject -Class Win32_LocalTime

            Get-Date -Day $DT.Day -Month $DT.Month -Year $DT.Year -Minute $DT.Minute -Hour $DT.Hour -Second $DT.Second

        }

        "$($row.Name) information has been fetched"

    } else {

        $vmDT = ''

        $reportNotFound += $row.Name

        "$($row.Name) not found"

    }

    $row | Add-Member -MemberType NoteProperty -Name 'DateTime' -Value $vmDT

    $report += $row

}


$report | Export-Csv -Path .\$($folder)_Date_Info_$date.csv -NoTypeInformation -UseCulture

$reportNotFound | Export-Csv -Path .\$($folder)_No_VMs_Date_Info_$date.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Thank you very LucD. That worked perfectly Smiley Happy

Reply
0 Kudos