Question
powershell

Need a Powershell script that will obtain a path thats leads to multiple comma separated value excel sheets and can send out

For example a link “\\example\temp\chg” goes to a file that contains multiple Excel CSV files such as “chg.csv”

Chg.csv and the multiple other Excel Csv files contain data under Name, Date, Address

If the Excel CSV file only contains the header Name, Date, Address then send an email alert from a powerscript file that the Excel Csv file is empty
0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOME FACTS ABOUT READING CSV FILE USING POWERSHELL SCRIPT

1.HOW TO IMPORT A CSV FILE

Import-Csv [[-Path] <string[]>] -UseCulture [-LiteralPath <string[]>] [-Header <string[]>][-Encoding <Encoding>] [<CommonParameters>]

The Import-Csv cmdlet creates table-like custom objects from the items in CSV files. Each column in the CSV file becomes a property of the custom object and the items in rows become the property values. Import-Csv works on any CSV file, including files that are generated by the Export-Csvcmdlet.

You can use the parameters of the Import-Csv cmdlet to specify the column header row and the item delimiter, or direct Import-Csv to use the list separator for the current culture as the item delimiter.

Import a CSV that is missing a value

This example shows how the Import-Csv cmdlet in PowerShell responds when the header row in a CSV file includes a null or empty value. Import-Csv substitutes a default name for the missing header row that becomes the property name of the object that Import-Csv returns.

PowerShellCopy

Get-Content -Path .\Projects.csv

ProjectID,ProjectName,,Completed
13,Inventory,Redmond,True
440,,FarEast,True
469,Marketing,Europe,False

Import-Csv -Path .\Projects.csv

WARNING: One or more headers were not specified. Default names starting with "H" have been used in place of any missing headers.

ProjectID ProjectName H1      Completed
--------- ----------- --      ---------
13        Inventory   Redmond True
440                   FarEast True
469       Marketing   Europe  False

(Import-Csv -Path .\Projects.csv).H1

WARNING: One or more headers were not specified. Default names starting with "H" have been used in place of any missing headers.
Redmond
FarEast
Europe

To create your Projects.csv file, use the values shown in the example's Get-Content output.

The Get-Content cmdlet displays the Projects.csv file. The header row is missing a value betweenProjectName and Completed. The Import-Csv cmdlet imports the Projects.csv file and displays a warning message because H1 is a default header name. The (Import-Csv -Path .\Projects.csv).H1 command gets the H1 property values and displays a warning.

EMAIL ON EMPTY FILE

Actually, in this part, i was not having proper knowledge about this topic so I also took some help from some of the Powershell scripts queries and understood  some related stuff

Checking if a file exists can be done using the Test-Path cmdlet, date modified and date created and file size (content) are all attributes of the file which can be retrieved using Get-Item or Get-ChildItem for example. Sending an email is done using Send-MailMessage in which you can also specify the content and the smtp server.

# Define hash table and the parameters for the send mail message cmdlet
$FileToCheck = Get-Item -Path C:\Users\my\Desktop\test\Accounts_To_check.txt -ErrorActionSilentlyContinue
$EmailSplat = @{
To = '[email protected]'
CC = '[email protected]'
SmtpServer = 'smtp.server.net'
From = '[email protected]'
Priority = 'High'
}

# Your first condition: 'If the file does not exist, or was not created today, an e-mail should be sent that states "File not created" or similar.'
if ((-not $FileToCheck) -or ($FileToCheck.CreationTime -le (Get-Date).AddDays(-1))) {
$EmailSplat.Subject = 'File not Found or not created today'
$EmailSplat.building = 'This is the email building'
Send-MailMessage @EmailSplat
# Your second condition 'If the file exists and was created today, but has no content, no e-mail should be sent.'
} elseif (($FileToCheck) -and ($FileToCheck.Length -le 2)) {
# Do nothing
# Your third condition and the default condition if it does not match the other conditions
} else {
$EmailSplat.Subject = 'Active Directory Accounts To Check'
$EmailSplat.building = Get-Content -Path 'C:\Users\my\Desktop\test\Accounts_To_Check.txt'
Send-MailMessage @EmailSplat

Specifies the delimiter that separates the property values in the CSV file. The default is a comma (,).

Add a comment
Know the answer?
Add Answer to:
powershell For example a link “\\example\temp\chg” goes to a file that contains multiple Excel CSV files such as “...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Use the csv file on spotify from any date Code from lab2 import java.io.File; import java.io.FileNotFoundException;...

    Use the csv file on spotify from any date Code from lab2 import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; public class SongsReport {    public static void main(String[] args) {               //loading name of file        File file = new File("songs.csv"); //reading data from this file        //scanner to read java file        Scanner reader;        //line to get current line from the file        String line="";       ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT