Not really sure how it went this way but all other write-host was able to show output as i expected but not from these lines
#Create report
#Write-Host "Creating Report" -ForegroundColor Red
Export-Csv -Path $reportName -NoTypeInformation -UseCulture
#Send results via email
#Write-Host "email the report" -ForegroundColor Cyan
Send-MailMessage -SmtpServer $smtpServer -Subject "$smtpSubject $(Get-Date -f dd_MM_yyyy)" -To $to -From $from -Attachments $reportName
#delete the old snapshot report
#Write-Host "delete report in the folder" -ForegroundColor Yellow
Get-ChildItem -Path $att -Filter .csv* | Remove-Item
Disconnect-VIServer -Server * -Confirm:$false -Force
i have to bracket it with # since it doesnt work like it should,
but here are errors i received
Write-Host : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not
match any of the parameters that take pipeline input.
At C:\Batch\Scripts\snapshots\VM_snapshot.ps1:82 char:1
+ Write-Host "Creating Report" -ForegroundColor Red
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (@{Name=SYMC-INC..._Service_IEDC1}:PSObject) [Write-Host], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.WriteHostCommand
Write-Host : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not
match any of the parameters that take pipeline input.
At C:\Batch\Scripts\snapshots\VM_snapshot.ps1:82 char:1
+ Write-Host "Creating Report" -ForegroundColor Red
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (@{Name=SYMC-FUL..._Service_IEDC1}:PSObject) [Write-Host], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.WriteHostCommand
Write-Host : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not
match any of the parameters that take pipeline input.
At C:\Batch\Scripts\snapshots\VM_snapshot.ps1:82 char:1
+ Write-Host "Creating Report" -ForegroundColor Red
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (@{Name=ASAUpgra...eGB=2.3; User=}:PSObject) [Write-Host], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.WriteHostCommand
Write-Host : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not
match any of the parameters that take pipeline input.
At C:\Batch\Scripts\snapshots\VM_snapshot.ps1:82 char:1
+ Write-Host "Creating Report" -ForegroundColor Red
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (@{Name=ASAUpgra...eGB=2.3; User=}:PSObject) [Write-Host], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.WriteHostCommand
Write-Host : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not
match any of the parameters that take pipeline input.
At C:\Batch\Scripts\snapshots\VM_snapshot.ps1:82 char:1
+ Write-Host "Creating Report" -ForegroundColor Red
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (@{Name=pre-upgr...eGB=7.4; User=}:PSObject) [Write-Host], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.WriteHostCommand
Write-Host : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not
match any of the parameters that take pipeline input.
At C:\Batch\Scripts\snapshots\VM_snapshot.ps1:82 char:1
+ Write-Host "Creating Report" -ForegroundColor Red
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (@{Name=PFSense2...AU\~RKrawiecki}:PSObject) [Write-Host], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.WriteHostCommand
Write-Host : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not
match any of the parameters that take pipeline input.
At C:\Batch\Scripts\snapshots\VM_snapshot.ps1:82 char:1
+ Write-Host "Creating Report" -ForegroundColor Red
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (@{Name=PFSense2...AU\~RKrawiecki}:PSObject) [Write-Host], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.WriteHostCommand
Write-Host : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not
match any of the parameters that take pipeline input.
At C:\Batch\Scripts\snapshots\VM_snapshot.ps1:82 char:1
+ Write-Host "Creating Report" -ForegroundColor Red
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (@{Name=about to...U\~KSutherland}:PSObject) [Write-Host], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.WriteHostCommand
Write-Host : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not
match any of the parameters that take pipeline input.
At C:\Batch\Scripts\snapshots\VM_snapshot.ps1:82 char:1
+ Write-Host "Creating Report" -ForegroundColor Red
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (@{Name=pre cert...GB=10.7; User=}:PSObject) [Write-Host], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.WriteHostCommand
From the error message I can only conclude that the line with the Write-Host was not commented out at the time you ran it.
I only use the piece of code that matters.
Get-VIEvent -Start $_.Created.AddMinutes(-1) -Finish $_.Created.AddMinutes(1) |
where{$_ -is [VMware.Vim.TaskEvent] -and $_.Info.DescriptionId -eq 'VirtualMachine.createSnapshot'} |
select -Last 1 -ExpandProperty UserName
}} | # This is the pipeline I suspected was there
#Create report
Write-Host "Creating Report" -ForegroundColor Red # <== This line gave the error!
Export-Csv -Path $reportName -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The line on itself doesn't do anything that can cause the error.
Verify that by running the line as-is
Write-Host "Creating Report" -ForegroundColor Red
I suspect that a previous line in the script places something on the pipeline, and that this is causing the Write-Host error.
Is there a line ending with a pipeline symbol ('|') at the end before the Write-Host line?
I would need to see your complete script to further analyse.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Here it is LucD, this is the script i havent yet completed its construction due to i wanted to make it neat now.
Add-PSSnapin VMware.VimAutomation.Core -ErrorAction 'SilentlyContinue'
#Parameters
$vcenter = Get-Content -Path C:\Batch\Scripts\snapshots\vcenters.txt
$smtpSubject= 'VMware Snapshots on'
$smtpServer = 'a.abc.local'
$to = 'a@abc.com'
$from = 'VMt@abc.com'
$att = 'C:\Batch\Scripts\snapshots\'
$reportName = $att + "snapshot-report_" + $(Get-Date -f dd_MM_yyyy) + ".csv"
#delete the old snapshot report
Get-ChildItem -Path $att -Filter .csv* | Remove-Item
#Connect to the vCenter server defined above. Ignore certificate errors
Write-Host "Connecting to vCenter"
Connect-VIServer -Server $vcenter -wa 0
Write-Host "Connected"
#the script body
Write-Host "Getting Snapshot" -ForegroundColor Yellow
#remove any .csv exists
Get-ChildItem -Path $att -Recurse -Filter *.csv | Remove-Item
Get-VM | Get-Snapshot |
select Name,
Created,
@{N='VM';E={$_.VM.Name}},
@{N='VMHost';E={$_.VM.VMHost.Name}},
@{N='Cluster';E={$_.VM.VMHost.Parent.Name}},
@{N='Datacenter';E={
$p = Get-View -Id $_.VM.VMHost.Parent.ExtensionData.Parent -Property Name,Parent
while($p -and $p -isnot [VMware.Vim.Datacenter]){
$p = Get-View -Id $p.Parent -Property Name,Parent
}
if($p){
$p.Name
}
}},
@{N='SizeMB';E={[math]::Round($_.SizeMB,1)}},
@{N='SizeGB';E={[math]::Round($_.SizeGB,1)}},
@{N='User';E={
Get-VIEvent -Start $_.Created.AddMinutes(-1) -Finish $_.Created.AddMinutes(1) |
where{$_ -is [VMware.Vim.TaskEvent] -and $_.Info.DescriptionId -eq 'VirtualMachine.createSnapshot'} |
select -Last 1 -ExpandProperty UserName
}} |
#Create report
#Write-Host "Creating Report" -ForegroundColor Red
Export-Csv -Path $reportName -NoTypeInformation -UseCulture
#Send results via email
#Write-Host "email the report" -ForegroundColor Cyan
Send-MailMessage -SmtpServer $smtpServer -Subject "$smtpSubject $(Get-Date -f dd_MM_yyyy)" -To $to -From $from -Attachments $reportName
#delete the old snapshot report
#Write-Host "delete report in the folder" -ForegroundColor Yellow
Get-ChildItem -Path $att -Filter .csv* | Remove-Item
Disconnect-VIServer -Server * -Confirm:$false -Force
From the error message I can only conclude that the line with the Write-Host was not commented out at the time you ran it.
I only use the piece of code that matters.
Get-VIEvent -Start $_.Created.AddMinutes(-1) -Finish $_.Created.AddMinutes(1) |
where{$_ -is [VMware.Vim.TaskEvent] -and $_.Info.DescriptionId -eq 'VirtualMachine.createSnapshot'} |
select -Last 1 -ExpandProperty UserName
}} | # This is the pipeline I suspected was there
#Create report
Write-Host "Creating Report" -ForegroundColor Red # <== This line gave the error!
Export-Csv -Path $reportName -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
okay i suspected the same too due to cmdlet write-host is pretty straightforward and i dont see anything wrong either in the previouses line.
in that case,i shall drop the write-host of those lines as i thought it could depicted to me where the powershell progressing while it runs.
LucD,
is there anyway i could use one liner syntax of csv report i made to convert it to html by using convert-html and send it over as the body of email instead of attachment in send-mailmessage cmdlet?
sorry keep asking you, but very much appreciated with tons of your help!
There is no need in that case to use a CSV (provided you don't need it for other reasons).
The following, simplified snippet, shows one way of doing that
$sMail = @{
To = 'me@domain.com'
From = 'Report.PowerCLI@domain.com'
Subject = 'Snapshot Report'
Body = $report | ConvertTo-Html | Out-String
BodyAsHtml = $true
SmtpServer = 'mail.domain.com'
}
Send-MailMessage @sMail
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference