Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

join three command to one script

Posted on 2016-07-26
9
39 Views
1 Endorsement
Last Modified: 2016-07-26
I have below three syntax, that i need to put in single script.

my purpose is that, i want the the below exchange server components state will come to email, as i will put this .ps1 file in schedule task.

i am using remote powershell

=========================================
$UserName = 'domain\username'
$Password = 'secret password'
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserName, (ConvertTo-SecureString $Password -asPlaintext -Force)

$session = New-PSSession –ConfigurationName Microsoft.Exchange -ConnectionUri "http://exchangecasarray.contoso.com/powershell" -Credential $Credential -Authentication Kerberos -AllowRedirection
Import-PSSession $session
Set-AdServerSettings -ViewEntireForest $true

===================================================================

second smtp setting

$smtpsettings = @{
    To =  "administrator@exchangeserverpro.net"
    From = "exchangeserver@exchangeserverpro.net"
    Subject = "$reportemailsubject - $now"
    SmtpServer = "smtp.exchangeserverpro.net"
    }

==================================================

output in email

Get-ExchangeServer | Get-ServerComponentState | ft



Please suggest, my purpose is to set this whole joined script in task scheudler and result will come on email.
1
Comment
Question by:Addy Nadia
  • 6
  • 3
9 Comments
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 41729258
It's almost everything there already:
UserName = 'domain\username'
$Password = 'secret password'

$smtpsettings = @{
    To =  "administrator@exchangeserverpro.net"
    From = "exchangeserver@exchangeserverpro.net"
    Subject = "$reportemailsubject - $now"
    SmtpServer = "smtp.exchangeserverpro.net"
    }

# -----------

$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserName, (ConvertTo-SecureString $Password -asPlaintext -Force)

$session = New-PSSession –ConfigurationName Microsoft.Exchange -ConnectionUri "http://exchangecasarray.contoso.com/powershell" -Credential $Credential -Authentication Kerberos -AllowRedirection
Import-PSSession $session
Set-AdServerSettings -ViewEntireForest $true

# Send as plain text mail
Send-MailMessage @smtpsettings -Body (Get-ExchangeServer | Get-ServerComponentState | ft -a Name, Component, State | Out-String)

# or send as HTML mail.
# Send-MailMessage @smtpsettings -BodyAsHTML -Body (Get-ExchangeServer | Get-ServerComponentState |  select Name, Component, State | convertTo-HTML)

Open in new window

I've provided a choice between HTML and plain text body, with the plain text being active.
1
 
LVL 5

Author Comment

by:Addy Nadia
ID: 41729267
so in this script, can i normally test it while running it in powershell?

and also i need to jst change username and password syntax above

You are Really a Champ !!

Thanks,
Andy
1
 
LVL 5

Author Comment

by:Addy Nadia
ID: 41729281
Awsum.. i tried
 it runs perfectly fine.. thank you so much

but outlook comes like below in email, can this be adjusted correctly, as seen Name and component are together

Name Component                  State
---- ---------                  -----
     ServerWideOffline          Active
     HubTransport               Active
     FrontendTransport          Active
     Monitoring                 Active
     RecoveryActionsEnabled     Active
     AutoDiscoverProxy          Active
     ActiveSyncProxy            Active
     EcpProxy                   Active
     EwsProxy                   Active
     ImapProxy                  Active
     OabProxy                   Active
     OwaProxy                   Active
     PopProxy                   Active
     PushNotificationsProxy     Active
     RpsProxy                   Active
     RwsProxy                   Active
     RpcProxy                   Active
     UMCallRouter               Active
     XropProxy                  Active
     HttpProxyAvailabilityGroup Active
     ForwardSyncDaemon          Active
     ProvisioningRps            Active
     MapiProxy                  Active
     EdgeTransport              Active
     HighAvailability           Active
     SharedCache                Active
     ServerWideOffline          Active
     HubTransport               Active
     FrontendTransport          Active
     Monitoring                 Active
     RecoveryActionsEnabled     Active
     AutoDiscoverProxy          Active
     ActiveSyncProxy            Active
     EcpProxy                   Active
     EwsProxy                   Active
     ImapProxy                  Active
     OabProxy                   Active
     OwaProxy                   Active
     PopProxy                   Active
     PushNotificationsProxy     Active
     RpsProxy                   Active
     RwsProxy                   Active
     RpcProxy                   Active
     UMCallRouter               Active
     XropProxy                  Active
     HttpProxyAvailabilityGroup Active
     ForwardSyncDaemon          Active
     ProvisioningRps            Active
     MapiProxy                  Active
     EdgeTransport              Active
     HighAvailability           Active
     SharedCache                Active
     ServerWideOffline          Active
     HubTransport               Active
     FrontendTransport          Active
     Monitoring                 Active
     RecoveryActionsEnabled     Active
     AutoDiscoverProxy          Active
     ActiveSyncProxy            Active
     EcpProxy                   Active
     EwsProxy                   Active
     ImapProxy                  Active
     OabProxy                   Active
     OwaProxy                   Active
     PopProxy                   Active
     PushNotificationsProxy     Active
     RpsProxy                   Active
     RwsProxy                   Active
     RpcProxy                   Active
     UMCallRouter               Active
     XropProxy                  Active
     HttpProxyAvailabilityGroup Active
     ForwardSyncDaemon          Active
     ProvisioningRps            Active
     MapiProxy                  Active
     EdgeTransport              Active
     HighAvailability           Active
     SharedCache                Active
     ServerWideOffline          Active
     HubTransport               Active
     FrontendTransport          Active
     Monitoring                 Active
     RecoveryActionsEnabled     Active
     AutoDiscoverProxy          Active
     ActiveSyncProxy            Active
     EcpProxy                   Active
     EwsProxy                   Active
     ImapProxy                  Active
     OabProxy                   Active
     OwaProxy                   Active
     PopProxy                   Active
     PushNotificationsProxy     Active
     RpsProxy                   Active
     RwsProxy                   Active
     RpcProxy                   Active
     UMCallRouter               Active
     XropProxy                  Active
     HttpProxyAvailabilityGroup Active
     ForwardSyncDaemon          Active
     ProvisioningRps            Active
     MapiProxy                  Active
     EdgeTransport              Active
     HighAvailability           Active
     SharedCache                Active
     ServerWideOffline          Active
     HubTransport               Active
     FrontendTransport          Active
     Monitoring                 Active
     RecoveryActionsEnabled     Active
     AutoDiscoverProxy          Active
     ActiveSyncProxy            Active
     EcpProxy                   Active
     EwsProxy                   Active
     ImapProxy                  Active
     OabProxy                   Active
     OwaProxy                   Active
     PopProxy                   Active
     PushNotificationsProxy     Active
     RpsProxy                   Active
     RwsProxy                   Active
     RpcProxy                   Active
     UMCallRouter               Active
     XropProxy                  Active
     HttpProxyAvailabilityGroup Active
     ForwardSyncDaemon          Active
     ProvisioningRps            Active
     MapiProxy                  Active
     EdgeTransport              Active
     HighAvailability           Active
     SharedCache                Active
     ServerWideOffline          Active
     HubTransport               Active
     FrontendTransport          Active
     Monitoring                 Active
     RecoveryActionsEnabled     Active
     AutoDiscoverProxy          Active
     ActiveSyncProxy            Active
     EcpProxy                   Active
     EwsProxy                   Active
     ImapProxy                  Active
     OabProxy                   Active
     OwaProxy                   Active
     PopProxy                   Active
     PushNotificationsProxy     Active
     RpsProxy                   Active
     RwsProxy                   Active
     RpcProxy                   Active
     UMCallRouter               Active
     XropProxy                  Active
     HttpProxyAvailabilityGroup Active
     ForwardSyncDaemon          Active
     ProvisioningRps            Active
     MapiProxy                  Active
     EdgeTransport              Active
     HighAvailability           Active
     SharedCache                Active
     ServerWideOffline          Active
     HubTransport               Active
     FrontendTransport          Active
     Monitoring                 Active
     RecoveryActionsEnabled     Active
     AutoDiscoverProxy          Active
     ActiveSyncProxy            Active
     EcpProxy                   Active
     EwsProxy                   Active
     ImapProxy                  Active
     OabProxy                   Active
     OwaProxy                   Active
     PopProxy                   Active
     PushNotificationsProxy     Active
     RpsProxy                   Active
     RwsProxy                   Active
     RpcProxy                   Active
     UMCallRouter               Active
     XropProxy                  Active
     HttpProxyAvailabilityGroup Active
     ForwardSyncDaemon          Active
     ProvisioningRps            Active
     MapiProxy                  Active
     EdgeTransport              Active
     HighAvailability           Active
     SharedCache                Active
     ServerWideOffline          Active
     HubTransport               Active
     FrontendTransport          Active
     Monitoring                 Active
     RecoveryActionsEnabled     Active
     AutoDiscoverProxy          Active
     ActiveSyncProxy            Active
     EcpProxy                   Active
     EwsProxy                   Active
     ImapProxy                  Active
     OabProxy                   Active
     OwaProxy                   Active
     PopProxy                   Active
     PushNotificationsProxy     Active
     RpsProxy                   Active
     RwsProxy                   Active
     RpcProxy                   Active
     UMCallRouter               Active
     XropProxy                  Active
     HttpProxyAvailabilityGroup Active
     ForwardSyncDaemon          Active
     ProvisioningRps            Active
     MapiProxy                  Active
     EdgeTransport              Active
     HighAvailability           Active
     SharedCache                Active
1
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 69

Expert Comment

by:Qlemo
ID: 41729289
"Name and component are together" - not correct, the name is empty. You probably have a different Exchange release than I. But for better formatting, try the HTML mail version, and replace "Name" with "Identity":
# Send as plain text mail
# Send-MailMessage @smtpsettings -Body (Get-ExchangeServer | Get-ServerComponentState | ft -a Identity, Component, State | Out-String)

# or send as HTML mail.
Send-MailMessage @smtpsettings -BodyAsHTML -Body (Get-ExchangeServer | Get-ServerComponentState |  select Identity, Component, State | convertTo-HTML)

Open in new window

0
 
LVL 5

Author Comment

by:Addy Nadia
ID: 41729353
i think you are not getting my query, i mean output that coming in email
is not in format

the data going here and dere like below

means if column status the below data not come below to status, it is not in format.
i mean exact like below it need to be

State
Active
Active
Active
0
 
LVL 5

Author Closing Comment

by:Addy Nadia
ID: 41729541
Thank you so much

i am so Thankful to EE to have so brilliant engineers, its worth to have a Subscription on EE
1
 
LVL 69

Expert Comment

by:Qlemo
ID: 41729722
With the latest change you get your email in HTML format. Maybe your mail client does not accept/display HTML mail - if this is the case, there is nothing we can do. For plain text to be properly formatted you need to have a mono-spaced (non-proportional) font in your email client.
0
 
LVL 5

Author Comment

by:Addy Nadia
ID: 41729738
Got it.. Many thanksss
0
 
LVL 5

Author Comment

by:Addy Nadia
ID: 41729739
Got it.. Many thanksss
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
In this video we show how to create a Shared Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Recipients >> Sha…
To show how to create a transport rule in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Mail Flow >> Rules tab.:  To cr…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question