More powershell help ...

Hi I'm looking for some more powershell help.

I have got the attached script and i would like to get the output into a csv file and then email that file.

Can someone give me some pointers on how to go about it as so far I am pretty stumped.
#region Variables and Arguments
$users = "youremail@yourcompany.com" # List of users to email your report to (separate by comma)
$fromemail = "youremail@yourcompany.com"
$server = "yourmailserver.yourcompany.com" #enter your own SMTP server DNS name / IP address here
$list = $args[0] #This accepts the argument you add to your scheduled task for the list of servers. i.e. list.txt
$computers = get-content $list #grab the names of the servers/computers to check from the list.txt file.
[int]$EventNum = 3
[int]$ProccessNumToFetch = 10
foreach ($computer in $computers) {

  $DiskInfo= Get-WMIObject -ComputerName $computer Win32_LogicalDisk | Where-Object{$_.DriveType -eq 3} | Where-Object{ ($_.freespace/$_.Size)*100 -lt $thresholdspace} `
  | Select-Object SystemName, DriveType, VolumeName, Name, @{n='Size (GB)';e={"{0:n2}" -f ($_.size/1gb)}}, @{n='FreeSpace (GB)';e={"{0:n2}" -f ($_.freespace/1gb)}}, @{n='Used (GB)';e={"{0:n2}" -f (($_.size/1gb)-($_.freespace/1GB))}}
  }
  #region System Info
  $OS = (Get-WmiObject Win32_OperatingSystem -computername $computer).caption
  $SystemInfo = Get-WmiObject -Class Win32_OperatingSystem -computername $computer | Select-Object Name, TotalVisibleMemorySize, FreePhysicalMemory, ServicePackMajorVersion
  $SystemCPU = gwmi win32_processor -computername $computer | Measure-Object -property LoadPercentage -Average | Select Average
  $TotalRAM = $SystemInfo.TotalVisibleMemorySize/1MB
  $FreeRAM = $SystemInfo.FreePhysicalMemory/1MB
  $ServicePack = $SystemInfo.ServicePackMajorVersion
  $UsedRAM = $TotalRAM - $FreeRAM
  $RAMPercentFree = ($FreeRAM / $TotalRAM) * 100
  $RAMPercentUsed = ($UsedRAM / $TotalRAM) * 100
  $TotalRAM = [Math]::Round($TotalRAM, 2)
  $FreeRAM = [Math]::Round($FreeRAM, 2)
  $UsedRAM = [Math]::Round($UsedRAM, 2)
  $RAMPercentFree = [Math]::Round($RAMPercentFree, 2)
  $RAMPercentUsed = [Math]::Round($RAMPercentUsed, 2)
  #endregion
  
  #region Event Logs Report
  $SystemEventsReport = @()
  $SystemEvents = Get-EventLog -ComputerName $computer -LogName System -EntryType Error,Warning -Newest $EventNum
  foreach ($event in $SystemEvents) {
    $row = New-Object -Type PSObject -Property @{
      TimeGenerated = $event.TimeGenerated
      EntryType = $event.EntryType
      Source = $event.Source
      Message = $event.Message
    }
    $SystemEventsReport += $row
  }
      
  $SystemEventsReport = $SystemEventsReport
  
  $ApplicationEventsReport = @()
  $ApplicationEvents = Get-EventLog -ComputerName $computer -LogName Application -EntryType Error,Warning -Newest $EventNum
  foreach ($event in $ApplicationEvents) {
    $row = New-Object -Type PSObject -Property @{
      TimeGenerated = $event.TimeGenerated
      EntryType = $event.EntryType
      Source = $event.Source
      Message = $event.Message
    }
    $ApplicationEventsReport += $row
  }
  
  $ApplicationEventsReport = $ApplicationEventsReport
  #endregion

Open in new window

LVL 1
ISD-PLCAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Use this starting and instead of from line 58+:
$ApplicationEventsReport | convertto-csv -NoType c:\temp\ApplicationEventReport.csv
Send-MailMessage -SMTPServer smtp.here.com -From me@here.com -To you@there.com -Subject 'Application Event Report' -Attachments c:\temp\ApplicationEventReport.csv
#endregion

Open in new window

0
ISD-PLCAuthor Commented:
Ok so I added the above ( Thanks Qlemo by the way) however I now get a error -

Missing closing '}' in statement block.
At C:\Scripts\DailyReport_CSV\DailyReport-CSV.ps1:68 char:2
+      <<<<
    + CategoryInfo          : ParserError: (CloseBraceToken:TokenId) [], ParseException
    + FullyQualifiedErrorId : MissingEndCurlyBrace

Heres the current code -

# PowerShell Systems Report
# Example usage: .DailyReport-CSV.ps1 .\list.txt
# Remember that list.txt is the file containing a list of Server names to run this against

#region Variables and Arguments
$users = "toemail" # List of users to email your report to (separate by comma)
$fromemail = "fromemail"
$server = "server" #enter your own SMTP server DNS name / IP address here
$list = $args[0] #This accepts the argument you add to your scheduled task for the list of servers. i.e. list.txt
$computers = get-content $list #grab the names of the servers/computers to check from the list.txt file.
[int]$EventNum = 3
[int]$ProccessNumToFetch = 10
foreach ($computer in $computers) {

	$DiskInfo= Get-WMIObject -ComputerName $computer Win32_LogicalDisk | Where-Object{$_.DriveType -eq 3} | Where-Object{ ($_.freespace/$_.Size)*100 -lt $thresholdspace} `
	| Select-Object SystemName, DriveType, VolumeName, Name, @{n='Size (GB)';e={"{0:n2}" -f ($_.size/1gb)}}, @{n='FreeSpace (GB)';e={"{0:n2}" -f ($_.freespace/1gb)}}, @{n='Used (GB)';e={"{0:n2}" -f (($_.size/1gb)-($_.freespace/1GB))}}
	
	#region System Info
	$OS = (Get-WmiObject Win32_OperatingSystem -computername $computer).caption
	$SystemInfo = Get-WmiObject -Class Win32_OperatingSystem -computername $computer | Select-Object Name, TotalVisibleMemorySize, FreePhysicalMemory, ServicePackMajorVersion
	$SystemCPU = gwmi win32_processor -computername $computer | Measure-Object -property LoadPercentage -Average | Select Average
	$TotalRAM = $SystemInfo.TotalVisibleMemorySize/1MB
	$FreeRAM = $SystemInfo.FreePhysicalMemory/1MB
	$ServicePack = $SystemInfo.ServicePackMajorVersion
	$UsedRAM = $TotalRAM - $FreeRAM
	$RAMPercentFree = ($FreeRAM / $TotalRAM) * 100
	$RAMPercentUsed = ($UsedRAM / $TotalRAM) * 100
	$TotalRAM = [Math]::Round($TotalRAM, 2)
	$FreeRAM = [Math]::Round($FreeRAM, 2)
	$UsedRAM = [Math]::Round($UsedRAM, 2)
	$RAMPercentFree = [Math]::Round($RAMPercentFree, 2)
	$RAMPercentUsed = [Math]::Round($RAMPercentUsed, 2)
	#endregion
	
	#region Event Logs Report
	$SystemEventsReport = @()
	$SystemEvents = Get-EventLog -ComputerName $computer -LogName System -EntryType Error,Warning -Newest $EventNum
	foreach ($event in $SystemEvents) {
		$row = New-Object -Type PSObject -Property @{
			TimeGenerated = $event.TimeGenerated
			EntryType = $event.EntryType
			Source = $event.Source
			Message = $event.Message
		}
		$SystemEventsReport += $row
	}
			
	$SystemEventsReport = $SystemEventsReport
	
	$ApplicationEventsReport = @()
	$ApplicationEvents = Get-EventLog -ComputerName $computer -LogName Application -EntryType Error,Warning -Newest $EventNum
	foreach ($event in $ApplicationEvents) {
		$row = New-Object -Type PSObject -Property @{
			TimeGenerated = $event.TimeGenerated
			EntryType = $event.EntryType
			Source = $event.Source
			Message = $event.Message
		}
		$ApplicationEventsReport += $row
	}
	
	$ApplicationEventsReport | convertto-csv -NoType c:\temp\DailyReport.csv
	Send-MailMessage -SMTPServer $server -From $fromemail -To $users -Subject 'Daily Report CSV' -Attachments c:\temp\DailyEventReport.csv
	#endregion

Open in new window

0
ISD-PLCAuthor Commented:
Right I have given up trying to get all the above to export it just doesnt seem to work.

So I have gone back to just getting basic system info to try and get that to export... but unsuprisingly am still having trouble.. would anyone like to give me some suggestions.. on getting the below to export to csv and then email the file?

# PowerShell Systems Report
# Example usage: .DailyReport-CSV.ps1 .\list.txt
# Remember that list.txt is the file containing a list of Server names to run this against
	#region Variables and Arguments
$users = "harvey.dowler@idealshoppingdirect.co.uk" # List of users to email your report to (separate by comma)
$fromemail = "infrastructureteam@idealshoppingdirect.co.uk"
$server = "10.100.1.187" #enter your own SMTP server DNS name / IP address here
$list = $args[0] #This accepts the argument you add to your scheduled task for the list of servers. i.e. list.txt
$computers = get-content $list #grab the names of the servers/computers to check from the list.txt file.
foreach ($computer in $computers) {

	$DiskInfo= Get-WMIObject -ComputerName $computer Win32_LogicalDisk | Where-Object{$_.DriveType -eq 3} | Where-Object{ ($_.freespace/$_.Size)*100 -lt $thresholdspace} `
	| Select-Object SystemName, DriveType, VolumeName, Name, @{n='Size (GB)';e={"{0:n2}" -f ($_.size/1gb)}}, @{n='FreeSpace (GB)';e={"{0:n2}" -f ($_.freespace/1gb)}}, @{n='Used (GB)';e={"{0:n2}" -f (($_.size/1gb)-($_.freespace/1GB))}} | ConvertTo-HTML -fragment
	
	#region System Info
	$OS = (Get-WmiObject Win32_OperatingSystem -computername $computer).caption
	$SystemInfo = Get-WmiObject -Class Win32_OperatingSystem -computername $computer | Select-Object Name, TotalVisibleMemorySize, FreePhysicalMemory, ServicePackMajorVersion
	$CPU = gwmi win32_processor -computername $computer | Measure-Object -property LoadPercentage -Average | Select Average 
	$TotalRAM = $SystemInfo.TotalVisibleMemorySize/1MB
	$FreeRAM = $SystemInfo.FreePhysicalMemory/1MB
	$ServicePack = $SystemInfo.ServicePackMajorVersion
	$UsedRAM = $TotalRAM - $FreeRAM
	$RAMPercentFree = ($FreeRAM / $TotalRAM) * 100
	$RAMPercentUsed = ($UsedRAM / $TotalRAM) * 100
	$TotalRAM = [Math]::Round($TotalRAM, 2)
	$FreeRAM = [Math]::Round($FreeRAM, 2)
	$UsedRAM = [Math]::Round($UsedRAM, 2)
	$RAMPercentFree = [Math]::Round($RAMPercentFree, 2)
	$RAMPercentUsed = [Math]::Round($RAMPercentUsed, 2)
	$SystemCPU = [Math]::Round($CPU.Average, 2) 
	#endregion

Open in new window

0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Re: http:#a39564969 - the error message is for a line not contained in the code snippet, but obviously the curly bracket in line 13 (outermost foreach) does not have a matching closing one. There should be one in front of the CSV/email part (line 61 of that code). The #endregion does not fit, but that is relevant only for display in PowerGUI (?), and not important.

This should be your overall code now (I have removed the unnecessary usage of $row, and fixed the regions):
# PowerShell Systems Report
# Example usage: .DailyReport-CSV.ps1 .\list.txt
# Remember that list.txt is the file containing a list of Server names to run this against

#region Variables and Arguments
$users = "toemail" # List of users to email your report to (separate by comma)
$fromemail = "fromemail"
$server = "server" #enter your own SMTP server DNS name / IP address here
$list = $args[0] #This accepts the argument you add to your scheduled task for the list of servers. i.e. list.txt
$computers = get-content $list #grab the names of the servers/computers to check from the list.txt file.
[int]$EventNum = 3
[int]$ProccessNumToFetch = 10
#endregion

foreach ($computer in $computers) {

  $DiskInfo= Get-WMIObject -ComputerName $computer Win32_LogicalDisk | Where-Object{$_.DriveType -eq 3} | Where-Object{ ($_.freespace/$_.Size)*100 -lt $thresholdspace} `
  | Select-Object SystemName, DriveType, VolumeName, Name, @{n='Size (GB)';e={"{0:n2}" -f ($_.size/1gb)}}, @{n='FreeSpace (GB)';e={"{0:n2}" -f ($_.freespace/1gb)}}, @{n='Used (GB)';e={"{0:n2}" -f (($_.size/1gb)-($_.freespace/1GB))}}
  
  #region System Info
  $OS = (Get-WmiObject Win32_OperatingSystem -computername $computer).caption
  $SystemInfo = Get-WmiObject -Class Win32_OperatingSystem -computername $computer | Select-Object Name, TotalVisibleMemorySize, FreePhysicalMemory, ServicePackMajorVersion
  $SystemCPU = gwmi win32_processor -computername $computer | Measure-Object -property LoadPercentage -Average | Select Average
  $TotalRAM = $SystemInfo.TotalVisibleMemorySize/1MB
  $FreeRAM = $SystemInfo.FreePhysicalMemory/1MB
  $ServicePack = $SystemInfo.ServicePackMajorVersion
  $UsedRAM = $TotalRAM - $FreeRAM
  $RAMPercentFree = ($FreeRAM / $TotalRAM) * 100
  $RAMPercentUsed = ($UsedRAM / $TotalRAM) * 100
  $TotalRAM = [Math]::Round($TotalRAM, 2)
  $FreeRAM = [Math]::Round($FreeRAM, 2)
  $UsedRAM = [Math]::Round($UsedRAM, 2)
  $RAMPercentFree = [Math]::Round($RAMPercentFree, 2)
  $RAMPercentUsed = [Math]::Round($RAMPercentUsed, 2)
  #endregion
  
  #region Event Logs Report
  $SystemEventsReport = @()
  $SystemEvents = Get-EventLog -ComputerName $computer -LogName System -EntryType Error,Warning -Newest $EventNum
  foreach ($event in $SystemEvents) {
    $SystemEventsReport += New-Object -Type PSObject -Property @{
      TimeGenerated = $event.TimeGenerated
      EntryType = $event.EntryType
      Source = $event.Source
      Message = $event.Message
    }
  }
     
  $ApplicationEventsReport = @()
  $ApplicationEvents = Get-EventLog -ComputerName $computer -LogName Application -EntryType Error,Warning -Newest $EventNum
  foreach ($event in $ApplicationEvents) {
    $ApplicationEventsReport += New-Object -Type PSObject -Property @{
      TimeGenerated = $event.TimeGenerated
      EntryType = $event.EntryType
      Source = $event.Source
      Message = $event.Message
    }
  }
  #endregion
}

$SystemEventsReport + $ApplicationEventsReport | convertto-csv -NoType c:\temp\DailyReport.csv
Send-MailMessage -SMTPServer $server -From $fromemail -To $users -Subject 'Daily Report CSV' -Attachments c:\temp\DailyEventReport.csv

Open in new window

0
ISD-PLCAuthor Commented:
Thanks Qlemo, do you think it possible to create a single csv with all the data in inc the disk/cpu/ram info?

So far I'm thinking not due to the data differences so I may split that section out. I can do it all via an html report which looks good but doesnt allow me to use the data.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
No, you can't combine data that different. Three different CSV files (disk info, system info, eventlog info) is suitable. The whole script needs some rework for getting that done, in particular we need to have single properties of each info to get a proper CSV.
I've also applied some minor optimization, and reformatted the code.
# PowerShell Systems Report
# Example usage: .DailyReport-CSV.ps1 .\list.txt
# Remember that list.txt is the file containing a list of Server names to run this against

#region Variables and Arguments
$users     = "toemail"	# List of users to email your report to (separate by comma)
$fromemail = "fromemail"
$server    = "server"	#enter your own SMTP server DNS name / IP address here
$list      = $args[0]	#This accepts the argument you add to your scheduled task for the list of servers. i.e. list.txt
[int]$EventNum = 3
# [int]$ProccessNumToFetch = 10  ## not used
#endregion

$DiskInfo     = @()
$SystemInfo   = @()
$SystemEvents = @()
$AppEvents    = @()

foreach ($computer in get-content $list) {

  $DiskInfo += Get-WMIObject -ComputerName $computer Win32_LogicalDisk |
                 Where-Object { $_.DriveType -eq 3} |
                 Where-Object { ($_.freespace/$_.Size)*100 -lt $thresholdspace} |
                 Select-Object @{n='ComputerName'  ; e={$Computer}},
                               DriveType, VolumeName, Name,
                               @{n='Size (GB)'     ; e={"{0:n2}" -f ($_.size/1gb)}},
                               @{n='FreeSpace (GB)'; e={"{0:n2}" -f ($_.freespace/1gb)}},
                               @{n='Used (GB)'     ; e={"{0:n2}" -f (($_.size/1gb)-($_.freespace/1GB))}}
  
  #region System Info
  $SysInfo = Get-WmiObject -Class Win32_OperatingSystem -computername $computer |
               Select-Object Caption, Name, TotalVisibleMemorySize, FreePhysicalMemory, ServicePackMajorVersion
  $SystemInfo += New-Object PsObject -Property @{
     ComputerName   = $Computer   
     OS             = $SysInfo.caption
     ServicePack    = $SysInfo.ServicePackMajorVersion
     AvgCPU         = gwmi win32_processor -computername $computer | Measure-Object -property LoadPercentage -Average | Select Average
     TotalRAM       = [Math]::Round($SysInfo.TotalVisibleMemorySize/1MB, 2)
     FreeRAM        = [Math]::Round($SysInfo.FreePhysicalMemory    /1MB, 2)
     UsedRAM        = [Math]::Round(($SysInfo.TotalVisibleMemorySize - $SysInfo.FreePhysicalMemory)/1MB, 2)
     RAMPercentFree = [Math]::Round((   $SysInfo.FreePhysicalMemory / $SysInfo.TotalVisibleMemorySize) * 100, 2)
     RAMPercentUsed = [Math]::Round((1- $SysInfo.FreePhysicalMemory / $SysInfo.TotalVisibleMemorySize) * 100, 2)
  }
  #endregion
  
  #region Event Logs Report
  foreach ($event in Get-EventLog -ComputerName $computer -LogName System -EntryType Error,Warning -Newest $EventNum) {
    $SystemEvents += New-Object -Type PSObject -Property @{
      ComputerName  = $Computer
      TimeGenerated = $event.TimeGenerated
      EntryType     = $event.EntryType
      Source        = $event.Source
      Message       = $event.Message
    }
  }
     
  $ApplicationEvents = 
  foreach ($event in Get-EventLog -ComputerName $computer -LogName Application -EntryType Error,Warning -Newest $EventNum) {
    $AppEvents += New-Object -Type PSObject -Property @{
      ComputerName  = $Computer
      TimeGenerated = $event.TimeGenerated
      EntryType     = $event.EntryType
      Source        = $event.Source
      Message       = $event.Message
    }
  }
  #endregion
}

$DiskInfo                  | convertto-csv -NoType c:\temp\DailyReport-DiskInfo.csv
$SysInfo                   | convertto-csv -NoType c:\temp\DailyReport-SystemInfo.csv
$SystemEvents + $AppEvents | convertto-csv -NoType c:\temp\DailyReport-Events.csv

Send-MailMessage -SMTPServer $server -From $fromemail -To $users -Subject 'Daily Report CSVs' `
  -Attachments c:\temp\DailyReport-DiskInfo.csv, c:\temp\DailyReport-SystemInfo.csv, c:\temp\DailyReport-Events.csv

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ISD-PLCAuthor Commented:
Blimey thanks Qlemo wasn't expecting you to do all that! I'm very grateful.

It doesnt seem to like the convertto-csv as its throwing the follwing errors, i'll try using export-csv instead.

convertto-csv : 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:\Scripts\DailyReport_CSV\DailyReport-CSV.ps1:71 char:30
+ $SysInfo                   | convertto-csv -NoType c:\temp\DailyReport-SystemInf ...
+                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (@{Caption=Micro...MajorVersion=2}:PSObject) [ConvertTo-Csv], Parameter
   BindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.ConvertToCsvCommand

convertto-csv : 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:\Scripts\DailyReport_CSV\DailyReport-CSV.ps1:72 char:30
+ $SystemEvents + $AppEvents | convertto-csv -NoType c:\temp\DailyReport-Events.cs ...
+                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (@{Message=An an...ntryType=Error}:PSObject) [ConvertTo-Csv], Parameter
   BindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.ConvertToCsvCommand

convertto-csv : 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:\Scripts\DailyReport_CSV\DailyReport-CSV.ps1:72 char:30
+ $SystemEvents + $AppEvents | convertto-csv -NoType c:\temp\DailyReport-Events.cs ...
+                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (@{Message=An an...ntryType=Error}:PSObject) [ConvertTo-Csv], Parameter
   BindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.ConvertToCsvCommand

convertto-csv : 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:\Scripts\DailyReport_CSV\DailyReport-CSV.ps1:72 char:30
+ $SystemEvents + $AppEvents | convertto-csv -NoType c:\temp\DailyReport-Events.cs ...
+                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (@{Message=A Pat...ryType=Warning}:PSObject) [ConvertTo-Csv], Parameter
   BindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.ConvertToCsvCommand

convertto-csv : 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:\Scripts\DailyReport_CSV\DailyReport-CSV.ps1:72 char:30
+ $SystemEvents + $AppEvents | convertto-csv -NoType c:\temp\DailyReport-Events.cs ...
+                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (@{Message=Strin...ntryType=Error}:PSObject) [ConvertTo-Csv], Parameter
   BindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.ConvertToCsvCommand

convertto-csv : 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:\Scripts\DailyReport_CSV\DailyReport-CSV.ps1:72 char:30
+ $SystemEvents + $AppEvents | convertto-csv -NoType c:\temp\DailyReport-Events.cs ...
+                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (@{Message=Strin...ntryType=Error}:PSObject) [ConvertTo-Csv], Parameter
   BindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.ConvertToCsvCommand

convertto-csv : 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:\Scripts\DailyReport_CSV\DailyReport-CSV.ps1:72 char:30
+ $SystemEvents + $AppEvents | convertto-csv -NoType c:\temp\DailyReport-Events.cs ...
+                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (@{Message=Strin...ntryType=Error}:PSObject) [ConvertTo-Csv], Parameter
   BindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.ConvertToCsvCommand

Send-MailMessage : The specified string is not in the form required for an e-mail address.
At C:\Scripts\DailyReport_CSV\DailyReport-CSV.ps1:74 char:1
+ Send-MailMessage -SMTPServer $server -From $fromemail -To $users -Subject 'Daily ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidType: (fromemail:String) [Send-MailMessage], FormatException
    + FullyQualifiedErrorId : FormatException,Microsoft.PowerShell.Commands.SendMailMessage

Open in new window

0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
My fault. Yes, use export-csv instead of convertto-csv. And you'll need to define $thresholdspace in the "Variables and Arguments" region.
0
ISD-PLCAuthor Commented:
Excellent all working now ---- except the CPU avg is showing up as @{Average=14} which I fixed in the htm version using the below to jsut pull out the AVG as numeric .

foreach ($computer in $computers) {

	$DiskInfo= Get-WMIObject -ComputerName $computer Win32_LogicalDisk | Where-Object{$_.DriveType -eq 3} | Where-Object{ ($_.freespace/$_.Size)*100 -lt $thresholdspace} `
	| Select-Object SystemName, DriveType, VolumeName, Name, @{n='Size (GB)';e={"{0:n2}" -f ($_.size/1gb)}}, @{n='FreeSpace (GB)';e={"{0:n2}" -f ($_.freespace/1gb)}}, @{n='Used (GB)';e={"{0:n2}" -f (($_.size/1gb)-($_.freespace/1GB))}} | ConvertTo-HTML -fragment
	
	#region System Info
	$OS = (Get-WmiObject Win32_OperatingSystem -computername $computer).caption
	$SystemInfo = Get-WmiObject -Class Win32_OperatingSystem -computername $computer | Select-Object Name, TotalVisibleMemorySize, FreePhysicalMemory, ServicePackMajorVersion
	$CPU = gwmi win32_processor -computername $computer | Measure-Object -property LoadPercentage -Average | Select Average 
	$TotalRAM = $SystemInfo.TotalVisibleMemorySize/1MB
	$FreeRAM = $SystemInfo.FreePhysicalMemory/1MB
	$ServicePack = $SystemInfo.ServicePackMajorVersion
	$UsedRAM = $TotalRAM - $FreeRAM
	$RAMPercentFree = ($FreeRAM / $TotalRAM) * 100
	$RAMPercentUsed = ($UsedRAM / $TotalRAM) * 100
	$TotalRAM = [Math]::Round($TotalRAM, 2)
	$FreeRAM = [Math]::Round($FreeRAM, 2)
	$UsedRAM = [Math]::Round($UsedRAM, 2)
	$RAMPercentFree = [Math]::Round($RAMPercentFree, 2)
	$RAMPercentUsed = [Math]::Round($RAMPercentUsed, 2)
	$SystemCPU = [Math]::Round($CPU.Average, 2) 
	#endregion

Open in new window

0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Use
    AvgCPU         = (gwmi win32_processor -computername $computer | Measure-Object -property LoadPercentage -Average).Average

Open in new window

0
ISD-PLCAuthor Commented:
Cheers for that that got me sorted plus a bit of rounding job done :)

AvgCPU         = [Math]::Round((gwmi win32_processor -computername $computer | Measure-Object -property LoadPercentage -Average).Average,0)

Open in new window

0
ISD-PLCAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for ISD-PLC's comment #a39570292

for the following reason:

Great assistance from Qlemo
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
You selected your own comment http:#a39570292 as answer, which isn't correct.
I recommend to accept
http:#a39567942
http:#a39570610
and either http:#a39571017 or http:#a39571164  (for AvgCPU)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.