Forum Discussion
Create Power BI Gateway restart Automation script by power shell
Hi Team, can you please help me how to create an automation script by using powershell so that it will check in every 5minutes if my gateway is online it is fine if offline start it and send an email to our admin group. What are the prerequisite need for it and what are the process and how to integrate it with Azure etc.
Happy to help clarify.
From address => This is just the sender name that appears in the email, it doesn't have to be a real mailbox, but it should look professional, just replace [email protected] with your actual company domain, something like [email protected] you can think of it like a "no-reply" email address.To address => You already have this right, just put your admin group email [email protected] there and everyone in that group will receive the alert.
SMTP & Port => Since you're on Office 365, keep smtp.office365.com and port 587 as they are, these are Microsoft's default settings for sending emails securely, nothing to change here.
Credentials => Use your admin account that has an Office 365 mailbox and to avoid the script asking for your password every time it runs, do this once:
Get-Credential | Export-Clixml -Path "C:\Scripts\gateway-cred.xml"
Then swap
(Get-Credential) in the script with:Import-Clixml -Path "C:\Scripts\gateway-cred.xml"You would not need Azure App Registration at all for this setup.
Did it work? π A kudos would be appreciated
π¨ Mark it as a solution to help spread knowledge π‘
6 Replies
- DataVitalizerSuper User
Hi Manas0396 ,
Here's how to set up the automation, you would need firstly:
- Admin rights
- PowerShell 5.0+
- Email access (Office 365/SMTP)
Then create a script file such check-gateway.ps1 with this code:
$service = Get-Service -Name "PBIEgwService"
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
if ($service.Status -ne "Running") {
Restart-Service -Name "PBIEgwService" -Force
Send-MailMessage -From "[email protected]" `
-To "[email protected]" `
-Subject "ALERT: Gateway Restarted" `
-Body "Restarted at $timestamp" `
-SmtpServer "smtp.office365.com" `
-Port 587 -UseSsl `
-Credential (Get-Credential)
}Later, you will need to schedule running that script:
- Open Task Scheduler of windows
- Create Basic Task
- Set trigger: Repeat every 5 minutes
- Set action: Run the PowerShell script
- Run as admin
Did it work? π A kudos would be appreciated
π¨ Mark it as a solution to help spread knowledge π‘π© Let's connect on LinkedIn
- Manas0396Frequent Visitor
Hi DataVitalizer Thank you for your replay I am admin with have access to powershell in on premises gateway machine I have couple of doubts from above script if you could explain me so I will get easily from my environment and apply this set up.
Send-MailMessage -From "[email protected]" - what is this email and where I can find out
-To "[email protected]" ` - This is our group email like [email protected]
-Subject "ALERT: Gateway Restarted" ` - email subject
-Body "Restarted at $timestamp" ` - email with time
-SmtpServer "smtp.office365.com" ` - what is this smtp.office365.com is it require as it send to [email protected]
-Port 587 -UseSsl `- where I get this port what is this ssl do I need to give these details if yes where find these details
-Credential (Get-Credential)- whom credential I need to provide hereAnother thing do I need to do App registration in Azure or not
- DataVitalizerSuper User
Happy to help clarify.
From address => This is just the sender name that appears in the email, it doesn't have to be a real mailbox, but it should look professional, just replace [email protected] with your actual company domain, something like [email protected] you can think of it like a "no-reply" email address.To address => You already have this right, just put your admin group email [email protected] there and everyone in that group will receive the alert.
SMTP & Port => Since you're on Office 365, keep smtp.office365.com and port 587 as they are, these are Microsoft's default settings for sending emails securely, nothing to change here.
Credentials => Use your admin account that has an Office 365 mailbox and to avoid the script asking for your password every time it runs, do this once:
Get-Credential | Export-Clixml -Path "C:\Scripts\gateway-cred.xml"
Then swap
(Get-Credential) in the script with:Import-Clixml -Path "C:\Scripts\gateway-cred.xml"You would not need Azure App Registration at all for this setup.
Did it work? π A kudos would be appreciated
π¨ Mark it as a solution to help spread knowledge π‘
- v-nmadadi-msftCommunity Support
Hi Manas0396 ,
I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions.
Thank you. - v-nmadadi-msftCommunity Support
Hi Manas0396
May I check if this issue has been resolved? If not, Please feel free to contact us if you have any further questions.
Thank you