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
You mean like this?
$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
There are a couple of issues.
- the ScriptBlock is not returning anything
- there is no $row variable used
Try with these changes
$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
Hi LucD,
This is generating a new file, but I need DateTime to appended to same import file, how can I do that..
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
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.
You mean like this?
$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
Thank you very LucD. That worked perfectly