PowerCLI

 View Only
  • 1.  Cannot bind argument to parameter 'InputObject' because it is null.

    Posted May 05, 2020 02:26 PM

    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



  • 2.  RE: Cannot bind argument to parameter 'InputObject' because it is null.

    Posted May 05, 2020 02:42 PM

    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



  • 3.  RE: Cannot bind argument to parameter 'InputObject' because it is null.

    Posted May 05, 2020 03:05 PM

    Hi LucD,

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



  • 4.  RE: Cannot bind argument to parameter 'InputObject' because it is null.

    Posted May 05, 2020 03:08 PM

    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?



  • 5.  RE: Cannot bind argument to parameter 'InputObject' because it is null.

    Posted May 05, 2020 03:24 PM

    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.



  • 6.  RE: Cannot bind argument to parameter 'InputObject' because it is null.
    Best Answer

    Posted May 05, 2020 03:44 PM

    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



  • 7.  RE: Cannot bind argument to parameter 'InputObject' because it is null.

    Posted May 06, 2020 01:13 PM

    Thank you very LucD. That worked perfectly :smileyhappy: