Need to append this powershell script, not sure what to do - powershell

I need some help expanding on this script. Basically, I need to add when the $inputFileName = 'Report.txt' is not in its default dir when the script runs every morning the job spits out an e-mail saying no file to process, and then the job stops. What I am seeing is when there is no file in the source dir every now and then, the job runs attaching a blank .xls file that is of no use. I appreciate ANY help in advance!
$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
Start-Transcript -path C:\PATH\Logoutput.txt -append
$emailFilePath = 'C:\PATH\PATH\'
$inputFilePath = 'C:\PATH\PATH\Upload'
$inputFileName = 'Report.txt'
$inputFile = Join-Path $inputFilePath $inputFileName
$outputFileName = 'Report.csv'
$outputFilePath = 'C:\PATH\PATH\Send'
$OutputFile = Join-Path $outputFilePath $outputFileName
$folder = 'C:\PATH\PATH\Upload'
$filter = 'Report.txt'
$destination = $outputFilePath
$dateTime1 = Get-Date -Format s
Write-Host "The file was received: $dateTime1"
Import-CSV $inputFile -Delimiter "`t" -Header "Value 1" , "Value 2" , "Value 3" , "Value 4" , "Value 5" | Tee-Object -Variable Report.txt | Export-CSV $OutputFile -Delimiter "," -NoTypeInformation
$xl = new-object -comobject excel.application
$xl.visible = $false
$Workbook = $xl.workbooks.open("$OutputFile")
$Worksheets = $Workbooks.worksheets
$Workbook.SaveAs("$outputFilePath\Report.xls",1)
$Workbook.Saved = $True
$xl.Quit()
$objExcel = new-object -comobject excel.application
$objExcel.Visible = $false
$objWorkbook = $objExcel.Workbooks.open("$outputFilePath\Report.xls")
$objWorksheet = $objWorkbook.Worksheets.Item(1)
$objRange = $objWorksheet.UsedRange
[void] $objRange.EntireColumn.Autofit()
$objWorkbook.SaveAs("$outputFilePath\Report.xlsx",51)
$objWorkbook.Saved = $True
$objExcel.Quit()
$fromaddress = "user#domain.com"
$toaddress = "user#domain.com"
$bccaddress = ""
$CCaddress = "user#domain.com"
$Subject = "Here Is The Report"
$body = get-content $emailFilePath\content.htm
$attachment = "$outputFilePath\Report.xlsx"
$smtpserver = "smtpdomain"
$message = new-object System.Net.Mail.MailMessage
$message.From = $fromaddress
$message.To.Add($toaddress)
$message.CC.Add($CCaddress)
#$message.Bcc.Add($bccaddress)
$message.IsBodyHtml = $True
$message.Subject = $Subject
$attach = new-object Net.Mail.Attachment($attachment)
$message.Attachments.Add($attach)
$message.body = $body
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$smtp.Send($message)
$dateTime2 = Get-Date -Format s
Write-Host "The file was parsed and emailed at $dateTime2"
Start-Sleep -Seconds 60
$message.Dispose()
Start-Sleep -Seconds 60
kill -processname Excel
Start-Sleep -Seconds 60
Remove-Item "C:\PATH\PATH\Send\*.*"
$filenameFormat = "Report" + "" + (Get-Date -Format "yyyy-MM-dd") + ".txt"
$updatedFile = "C:\PATH\PATH\Upload\" + $filenameFormat
Rename-Item -Path "C:\PATH\PATH\Upload\Report.txt" -NewName $filenameFormat
Move-Item -Path $updatedFile -Destination C:\PATH\PATH\ArchivedReportData
Stop-Transcript
exit

You need to test for the file before processing as follows:
If ( -not (Test-Path -Path "inputFile")) {
#Write your file not found logic/messages here
Exit
}
Import-CSV $inputFile -Delimiter "`t" -Header "Value 1" , "Value 2" , "Value 3" , "Value 4" , "Value 5" | Tee-Object -Variable Report.txt | Export-CSV $OutputFile -Delimiter "," -NoTypeInformation
$dateTime1 = Get-Date -Format s
Write-Host "The file was received: $dateTime1"
...
HTH

Related

Sending a properly email with line breaks

I am attempting to send the log files contents in the body of an email using Send-MailMessage. I tried using `r`n to no effect.
My email in outlook looks like the following....
My desired mail output :
Here is my script :
$SourceDir = "C:\Temp\"
#$GCI_Fiter = '*.txt'
$Include=#("*.log","*.txt")
$FileList = Get-ChildItem -LiteralPath $SourceDir -Include "$Include" -File
$myvar = #()
foreach ($FL_Item in $FileList) {
#$FLI_Content = Get-Content -LiteralPath $FL_Item.FullName
#$ExceptionLines = $FLI_Content | Select-String 'Exception' | ForEach-Object {$_.ToString().Trim()}
#if ($FLI_Content Get-Content -Path $file.FullName | Select-String "Exception" )
#){
$results = Get-Content -Path $FL_Item.FullName | Select-String "Exception"
if ($results) {
Write-Host "$($FL_Item.FullName) Exception found." -BackgroundColor Cyan
#$myvar += $results
$LINE = "$($FL_Item.Name)" + ":"
$EMAILBODY = $LINE + "`r`n"
$myvar += $EMAILBODY + $results + "`r`n"
Write-Output "Exception found"
}
else {
Write-Host "$($FL_Item.FullName) No exception found." -BackgroundColor Green
Write-Output "No exception found"
}
#}
#$ExceptionLines = $FLI_Content | Select-String -SimpleMatch 'Exception olustu'
#$ExceptionLines = $FLI_Content | Select-String -SimpleMatch 'Exception' | ForEach-Object {$_.ToString().Trim()}
#$FLI_Content
}
return $myvar
$MailBody = "Hi,`r
Exception logs. `r
"+$myvar+"
`r
`r
Regards,
"
Send-MailMessage -to $emailto -Subject $subject -SmtpServer $smtp -From $fromaddress -Body $MailBody -Encoding ([System.Text.Encoding]::UTF8) -Credential $creds
Another way is to use $Outlook = New-Object -ComObject Outlook.Application. I have come across a similar case. If you are using Outlook for example use the below code, however you format the email in the $Mail.Body, that is how they will receive the email.
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = "usermail#something.com"
$Mail.Subject = "Powershell"
$Mail.Body ="email text here"
$Date = (Get-Date).tostring("yyyyMMdd")
$File = "C:\Users\user\Desktop\log$Date.csv"
$Mail.Attachments.Add($File)
$Mail.Send()

How to Export-Csv with variables?

I'm trying to write 8 variables into an CSV file with PowerShell, but it just ends up as ,,,,,,, instead of var1,var2,var3,var4,var5,var6,var7,var8
My code is as follows:
$newRow = "{0},{1},{2},{3},{4},{5},{6},{7}" -f $var1,$var2,$var3,$var4,$var5,$var6,$var7,$var8
$newRow = $newRow -Replace "`t|`n|`r",""
$newRow = $newRow -Replace " ;|; ",";"
$newRow += "`n"
$newRow | Export-Csv -Path $file -Append -noType -Force
Without -Force I get the following error message:
Export-Csv : Cannot append CSV content to the following file: C:\result.txt. The
appended object does not have a property that corresponds to the following column:
var1. To continue with mismatched properties, add the -Force parameter, and then
retry the command.
At C:\Test.ps1:72 char:12
+ $newRow | Export-Csv -Path $file -Append -noType
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (var1:String) [Export-Csv], InvalidOperationException
+ FullyQualifiedErrorId : CannotAppendCsvWithMismatchedPropertyNames,Microsoft.PowerShell.Commands.ExportCsvCommand
EDIT:
Script:
$startInfo = New-Object System.Diagnostics.ProcessStartInfo
$startInfo.FileName = "powershell.exe"
$startInfo.Arguments = 'C:\zabbix\script\zabbix_vbr_job.ps1 "Discovery"'
$startInfo.RedirectStandardOutput = $true
$startInfo.UseShellExecute = $false
$startInfo.CreateNoWindow = $false
#$startInfo.Username = "DOMAIN\Username"
#$startInfo.Password = $password
$process = New-Object System.Diagnostics.Process
$process.StartInfo = $startInfo
$process.Start() | Out-Null
$discoveryJson = $process.StandardOutput.ReadToEnd()
$process.WaitForExit()
cls
$discovery = $discoveryJson | ConvertFrom-Json
$file = "C:\zabbix\script\result.txt"
function RunScript ($param, $id)
{
$startInfo = New-Object System.Diagnostics.ProcessStartInfo
$startInfo.FileName = "powershell.exe"
$startInfo.Arguments = "C:\zabbix\script\zabbix_vbr_job.ps1 '$param' '$id'"
$startInfo.RedirectStandardOutput = $true
$startInfo.UseShellExecute = $false
$startInfo.CreateNoWindow = $false
$process = New-Object System.Diagnostics.Process
$process.StartInfo = $startInfo
$process.Start() | Out-Null
$output = $process.StandardOutput.ReadToEnd()
$process.WaitForExit()
return $output
}
$fileContent = Import-csv $file
$NewCSVObject = #()
foreach($obj in $discovery.data)
{
$index = [array]::indexof($discovery.data, $obj)
Write-Host $index "/" $discovery.data.count
#Write-Host (RunScript "Result" $obj.JOBID )
$Result = RunScript "Result" $obj.JOBID
#Write-Host $Result
$RunStatus = RunScript "RunStatus" $obj.JOBID
#Write-Host $RunStatus
$IncludedSize = RunScript "IncludedSize" $obj.JOBID
#Write-Host $IncludedSize
$ExcludedSize = RunScript "ExcludedSize" $obj.JOBID
#Write-Host $ExcludedSize
$VmCount = RunScript "VmCount" $obj.JOBID
#Write-Host $VmCount
$Type = RunScript "Type" $obj.JOBID
#Write-Host $Type
$RunningJob = "RunningJob"#RunScript "RunningJob" $obj.JOBID
#Write-Host $RunningJob
#$newRow = New-Object PsObject -Property #{ JobID = $obj.JOBID ; Result = $Result ; RunStatus = $RunStatus ; IncludedSize = $IncludedSize ; ExcludedSize = $ExcludedSize ; VmCount = $VmCount ; Type = $Type ; RunningJob = $RunningJob }
$newRow = "{0},{1},{2},{3},{4},{5},{6},{7}" -f $obj.JOBID,$Result,$RunStatus,$IncludedSize,$ExcludedSize,$VmCount,$Type,$RunningJob
$newRow = $newRow -Replace "`t|`n|`r",""
$newRow = $newRow -Replace " ;|; ",";"
$newRow += "`n"
#$newRow | Out-File $file
#[io.file]::WriteAllText("C:\zabbix\script\test.txt",$newRow)
Write-Host $newRow
$newRow | Export-Csv -Path $file -Append -noType
break
}
#cls
Write-Host $fileContent
CSV headers:
JobID,Result,RunStatus,IncludedSize,ExcludedSize,VmCount,Type,RunningJob
There is no point in using Export-Csv if you're building the CSV line by hand anyway.
Either change
$newRow | Export-Csv -Path $file -Append -noType -Force
into
$newRow | Add-Content $file
or build $newRow like this:
$newRow = New-Object -Type PSObject -Property #{
'JobID' = $var1
'Result' = $var2
'RunStatus' = $var3
'IncludedSize' = $var4
'ExcludedSize' = $var5
'VmCount' = $var6
'Type' = $var7
'RunningJob' = $var8
}
and the problem will disappear.
The reason for this behavior is that Export-Csv is for transforming objects into a tabular string representation of their properties. Essentially, an object
#{
propertyA: 'foo'
propertyB: 23
}
becomes
propertyA,propertyB
"foo","23"
If you're already building a string, the resulting (string) object has just a single property (Length), which doesn't match any of the properties from your existing CSV. Hence the error you're getting without -Force. Even if you use -Force, the properties written to the CSV are determined from the first item in the existing CSV. Properties that are not present in this set are omitted from the output, and properties from that set that are not present in the object are filled with null values.

Powershell Get-ChildItem

I have a powershell script I wrote to check the contents of a folder and if there is a file with a LastWriteTime older than 20 minutes to notify me. The problem I am having is when I get the results it is including all of the files in the email body. How would I write this to get only the latest filename in the email body?
$src = 'c:\test'
$sendmail = $false
Get-ChildItem -path $src | ForEach-Object {
#write-host $_.fullname
$dtdiff = New-TimeSpan ($_.LastWriteTime) $(Get-Date)
if ($dtdiff.TotalMinutes -gt 20){
$strbody=$strbody +$_.fullname+ " Last File Modified at " +$_.LastWriteTime +"`r`n"
$sendmail=$true
}
}
#$strbody
if($sendmail -eq $true){
# Email components
$strFromAddress = "abc#xyz.net"
$strToAddress = "abc#xyz.net"
$strMessageSubject = "REPORT"
$strMessageBody = $strbody
$strSendingServer = "smtp.com"
$SMTPPort = "587"
$emailSmtpUser = "abc#xyz.net"
$emailSmtpPass = "test123"
# Email objects
$objSMTPMessage = New-Object System.Net.Mail.MailMessage $strFromAddress, $strToAddress, $strMessageSubject, $strMessageBody
$objSMTPClient = New-Object System.Net.Mail.SMTPClient( $strSendingServer, $SMTPPort )
$objSMTPClient.EnableSsl = $true
$objSMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser , $emailSmtpPass );
$objSMTPClient.Send($objSMTPMessage)
}
To get only the most actual file: Edited to remove flaw
Get-ChildItem -path $src |
Sort LastWriteTime |
Select -last 1 |
ForEach-Object {
#write-host $_.fullname
$dtdiff = New-TimeSpan ($_.LastWriteTime) $(Get-Date)
if ($dtdiff.TotalMinutes -gt 20){
$strbody=$strbody +$_.fullname+ " Last File Modified at " +$_.LastWriteTime +"`r`n"
$sendmail=$true
}
}
You're appending each filename and its associated timestamp to $strbody in that loop. It's doing exactly what you specified.
If you want only the latest file that was created in the last 20 minutes, change your get-childitem/foreach loop to this:
$mostrecentfile = get-childitem -path $src |
where-object {$_.lastwritetime -gt (get-date).addminutes(-20)} |
sort-object -property lastwritetime -descending | select-object -first 1
}
if ($mostrecentfile -ne $null) {
$strbody=$_.fullname+ " Last File Modified at " +$_.LastWriteTime;
$sendmail = $true;
}
get-childitem "c:\temp" -file | where LastWriteTime -le (Get-Date).AddMinutes(-20) |
Sort lastwritetime -descending |
% {
$strFromAddress = "abc#xyz.net"
$strToAddress = "abc#xyz.net"
$strMessageSubject = "REPORT"
$strMessageBody = "Last file modifed '{0}' at {1}" -f $_.fullname, $_.LastWriteTime
$strSendingServer = "smtp.com"
$SMTPPort = "587"
$emailSmtpUser = "abc#xyz.net"
$emailSmtpPass = "test123"
# Email objects
$objSMTPMessage = New-Object System.Net.Mail.MailMessage $strFromAddress, $strToAddress, $strMessageSubject, $strMessageBody
$objSMTPClient = New-Object System.Net.Mail.SMTPClient($strSendingServer, $SMTPPort )
$objSMTPClient.EnableSsl = $true
$objSMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser , $emailSmtpPass );
$objSMTPClient.Send($objSMTPMessage)
$objSMTPClient.Dispose()
break
}

Error while sending email through outlook in power shell script

I have a powershell script as below. Here I check whether any .err file has been created before 5 minutes and if yes, then I am sending an email for each error file with first 5 lines of the .err file.When I try to run the script I receive an email for the first file, but i get an error for the second file as shown in the snapshot below. I am new to powershell, and I am struggling to find any solution for this error.
$ChkFile = "D:\ErrorLog\*.err"
$ChkFilePath = "D:\ErrorLog"
$o = New-Object -comObject Outlook.Application
$mail = $o.CreateItem(0)
$mail.importance = 2
$mail.subject = “Error Log“
$mail.To = “email#email.com“
$FileExists = Test-Path $ChkFile
$FileCount = Get-ChildItem $ChkFilePath *.err | Measure-Object | %{$_.Count}
If ($FileExists -eq $True) {
If ($FileCount -gt 0)
{
Foreach($file in (Get-ChildItem $ChkFile))
{
Write-Host $file
$createtime = $file.LastWriteTime
$nowtime = get-date
if (($nowtime - $createtime).totalminutes -gt 5)
{
$GetFileContent = Get-Content $file -totalcount 5
$mail.body = $GetFileContent
Write-Host $GetFileContent
$mail.Send()
}
}
}
}
Error generated while executing the script:
After you invoke the $mail.Send() method you will likely need to recreate your mail object. You could do this by placing the object creation in the loop.
$ChkFile = "D:\ErrorLog\*.err"
$ChkFilePath = "D:\ErrorLog"
$o = New-Object -comObject Outlook.Application
$FileExists = Test-Path $ChkFile
$FileCount = Get-ChildItem $ChkFilePath *.err | Measure-Object | %{$_.Count}
If ($FileExists -eq $True) {
If ($FileCount -gt 0) {
Foreach($file in (Get-ChildItem $ChkFile)) {
Write-Host $file
$createtime = $file.LastWriteTime
$nowtime = get-date
if (($nowtime - $createtime).totalminutes -gt 5) {
$mail = $o.CreateItem(0)
$mail.importance = 2
$mail.subject = "Error Log"
$mail.To = "email#email.com"
$mail.importance = 2
$mail.subject = "Error Log"
$mail.To = "email#email.com"
$GetFileContent = Get-Content $file -totalcount 5
$mail.body = $GetFileContent
Write-Host $GetFileContent
$mail.Send()
}
}
}
}

Email Value From Variable & Hyperlink

From within PowerShell, I know how to send a basic email. But with my syntax below, how could I append to the body of the email each $QueryName and each $RowCount and add a hyperlink to the value contained in $FPath\$FormattedDate\so the body of email would look like this:
$QueryName - $RowCount
(or with actual data)
Santa - 14
Mickey - 12
Mars - 2
Here is my current PS script
Function Execute-SQLquery {
param ($QueryName, $QueryString)
$server = "Server"
$database = "DB1"
$FPath = "C:\Testing"
#Setting additional variables
$extension = ".csv"
$date = Get-Date -f 'MM.dd.yy'
$FormattedDate = Get-Date -f 'MM.dd.yy'
$connectionTemplate = "Data Source={0};Integrated Security=SSPI;Initial Catalog={1};"
$connectionString = [string]::Format($connectionTemplate, $server, $database)
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$command = New-Object System.Data.SqlClient.SqlCommand
$command.CommandText = $QueryString
$command.Connection = $connection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $command
$DataSet = New-Object System.Data.DataSet
$rowCount = $SqlAdapter.Fill($DataSet)
if(!(Test-Path -path "$FPath\$FormattedDate\")){New-Item "$FPath\$FormattedDate\" -type directory}
if ($rowCount -gt 0)
{
if ($QueryName -eq "Santa")
{
$extractFile = "C:\Testing\TemplateFiles\Santa.csv"
[System.IO.Directory]::CreateDirectory("$FPath\$FormattedDate\Santa\")
Write-Host $rowCount -fore Red
$dirName = "$FPath\$FormattedDate\Santa\"
$filename = [IO.Path]::GetFileNameWithoutExtension($extractFile) + "_$date" + [IO.Path]::GetExtension($extractFile)
$extractFile = Join-Path $dirname $filename
}
if ($QueryName -eq "Mickey")
{
$extractFile = "C:\Testing\TemplateFiles\Mickey.csv"
[System.IO.Directory]::CreateDirectory("$FPath\$FormattedDate\Mickey\")
Write-Host $rowCount -fore Red
$dirName = "$FPath\$FormattedDate\Mickey\"
$filename = [IO.Path]::GetFileNameWithoutExtension($extractFile) + "_$date" + [IO.Path]::GetExtension($extractFile)
$extractFile = Join-Path $dirname $filename
}
if ($QueryName -eq "Mars")
{
$extractFile = "C:\Testing\TemplateFiles\Mickey\Mars.csv"
[System.IO.Directory]::CreateDirectory("$FPath\$FormattedDate\Mars\")
Write-Host $rowCount -fore Red
$dirName = "$FPath\$FormattedDate\Mars\"
$filename = [IO.Path]::GetFileNameWithoutExtension($extractFile) + "_$date" + [IO.Path]::GetExtension($extractFile)
$extractFile = Join-Path $dirname $filename
}
$DataSet.Tables[0] | Export-Csv $extractFile -NoTypeInformation
}
$connection.Close()
}
First up, since the only thing that changes based on $QueryName are direct references to the value in $QueryName and the $extractFile, you'd be better off not repeating that entire block.
For the mail message, you can use Send-MailMessage.
To add a link to a local file resource, use the file:/// scheme prefix and change all backslashes (\) to forward slashes (/), ie. file:///C:/Document/Path.ext, or in your example "file:///$("$FPath\$FormattedDate" -replace '\','/')":
Function Execute-SQLquery {
param ($QueryName, $QueryString)
# up to this point no change is required
if ($rowCount -gt 0)
{
$extractFile = switch($QueryName){
"Santa" { "C:\Testing\TemplateFiles\Santa.csv" }
"Mickey" { "C:\Testing\TemplateFiles\Mickey.csv" }
"Mars" { "C:\Testing\TemplateFiles\Mars\Mickey.csv" }
default { throw "Illegal QueryName" }
}
[System.IO.Directory]::CreateDirectory("$FPath\$FormattedDate\$QueryName\")
Write-Host $rowCount -fore Red
$dirName = "$FPath\$FormattedDate\$QueryName\"
$filename = [IO.Path]::GetFileNameWithoutExtension($extractFile) + "_$date" + [IO.Path]::GetExtension($extractFile)
$extractFile = Join-Path $dirname $filename
$DataSet.Tables[0] | Export-Csv $extractFile -NoTypeInformation
$EmailBody = #'
Here are the results:
{0} - {1}
Find the documents here
'# -f $QueryName,$rowCount,$("$FPath\$FormattedDate" -replace '\','/')
Send-MailMessage -From "me#company.example" -To "you#company.example" -Body $EmailBody -BodyAsHtml:$true -Subject "Data extracted!" -SmtpServer "your.mail.server.company.example"
}
$connection.Close()
}