Send powershell script output to email

Kumari, Nidhi (Cognizant) 20 Reputation points
2025-01-20T17:39:31.0533333+00:00

I am working on a script to send email the new created server last 30 days ....I have script ready but I want to send output to email accordingly, if there is any new server then it will send email with the new server data but if there is no new server, it should send email body as " no new server created within last month"....please help with the code

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,609 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Marcin Policht 32,660 Reputation points MVP
    2025-01-20T17:43:17.21+00:00

    You can modify your existing PowerShell script to check whether there are any new servers created within the last 30 days, and then adjust the email body accordingly. This should help:

    Steps:

    1. Query the server creation data (assuming you are querying Active Directory or a database where the server information is stored).
    2. Filter the results to get only those created in the last 30 days.
    3. Check if any results exist.
    4. Prepare the email body.
    5. Send the email with the appropriate message.

    An example PowerShell script:

    # Set up the email parameters
    $smtpServer = "smtp.yourserver.com"
    $fromEmail = "your-email@example.com"
    $toEmail = "recipient@example.com"
    $subject = "New Server Creation Report"
    
    # Define the date range (last 30 days)
    $last30Days = (Get-Date).AddDays(-30)
    
    # Query for servers created in the last 30 days (Adjust the query based on your data source)
    # For example, if querying Active Directory:
    $servers = Get-ADComputer -Filter {WhenCreated -ge $last30Days} | Select-Object Name, WhenCreated
    
    # Check if there are any new servers
    if ($servers.Count -gt 0) {
        # Format the output for the email
        $body = "The following new servers were created in the last 30 days:`n"
        foreach ($server in $servers) {
            $body += "Server Name: $($server.Name), Created On: $($server.WhenCreated)`n"
        }
    } else {
        # If no new servers, set the body to indicate this
        $body = "No new servers created within the last 30 days."
    }
    
    # Send the email
    Send-MailMessage -SmtpServer $smtpServer -From $fromEmail -To $toEmail -Subject $subject -Body $body -BodyAsHtml $false
    
    • $last30Days: This variable calculates the date 30 days ago.
    • Get-ADComputer -Filter {WhenCreated -ge $last30Days}: This fetches computers (servers) from Active Directory that were created in the last 30 days. You can replace this with a different query depending on where your server data is stored.
    • $servers.Count -gt 0: This checks if there are any servers in the result.
    • If there are new servers, it loops through each and appends their names and creation dates to the email body.
    • If there are no new servers, the body will simply say "No new servers created within the last 30 days."

    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.