PowerShell: Connecting to Azure!

Here comes the Azure PowerShell module!
To automate the provisioning and management of Window Azure, you’ll need the Azure PowerShell module. The Azure team makes this module available as a free download, and this module provides approximately 180 Windows PowerShell cmdlets for automating Azure storage, virtual machines, virtual networks, and other cloud services.

To get started, you’ll need the following prerequisite items:

An Azure subscription – If you don’t currently have a paid subscription, you can sign-up for a FREE trial subscription on the Azure account site.
Azure PowerShell module – Download and install the Azure PowerShell module on a PC running Windows PowerShell 2.0 or Windows PowerShell 3.0.
After you satisfy these prerequisites, you’ll be prepared to follow along with the rest of this post.

Connect PowerShell to your Azure subscription.
After downloading and installing the Azure PowerShell module, you need to perform the following initial steps from Windows PowerShell to configure connection information for your Azure subscription. After you perform these steps the first time, the configuration is saved locally, and you do not need to perform these steps again.

1. Set the Windows PowerShell script execution policy to RemoteSigned:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

2. Import the Azure PowerShell module:

Import-Module Azure

After you complete the steps in this section, Windows PowerShell is configured to connect to your Azure subscription. To test this connection, enter the following cmdlet to connect to Azure and enumerate your subscription information:

Get-AzureSubscription

#Install AzureRM Module
Install-Module AzureRM
http://aka.ms/webpi-azps

# To log in to Azure Resource Manager
$cred = Get-Credential -Credential Domain\User
Login-AzureRmAccount -Credential $cred

# To view all subscriptions for your account
Get-AzureRmSubscription | Select-AzureRmSubscription

# To select a default subscription for your current session.
$subscription = "NAME OF YOUR SUBSCRIPTION GOES HERE"
Get-AzureRmSubscription -SubscriptionName $subscription | Select-AzureRmSubscription
Advertisements

PowerShell: GUI to manage service of your choice

Today we will be going over how to stop and start a service using GUI with popups. We will be using the Get-Service, Stop-Service, and Start-Service cmdlets. To provide the popups we will be using a wscript.shell.

First we start by grabbing the list of services that are running using the cmdlet Get-Service with the following pipeline expressions where {$_.status -eq 'running'} | Out-GridView -OutputMode Single. Out-Gridview expression with the single outputmode switch will allow the user to only select one Service at a time. This is handy when restarting a service for a program during an upgrade.

$Service=Get-Service | where {$_.status -eq 'running'} | Out-GridView -OutputMode Single

We will also need to setup the popup menu

To add some error handling we will ask the user if the service they picked is correct:

if ($Service.Status -eq "Running"){
$ServiceName=$Service.DisplayName
$Yes_Cancle = $GUI.popup("Do you want to turn stop $ServiceName ?", `
0,"Manage Service",3)
}

If the wrong service was selected we will notify the user that it will not be stopped.

else {
$GUI.popup("You decided to cancel. $ServiceName will not be stopped")
Start-Service $ServiceName
cls
}

How long doe we want to stop it is based on the script, but we will say 5 minutes or 300000 millisecond. We will also notify the user once the service has stopped and started again. I understand that something could have came up during the update or you need more time. The service will not actually start until you it OK!. If the update or anything you are doing requires a restart. Depending on the service setting to manual or auto will bring that service back online, once the machine is rebooted.

If ($Yes_Cancle -eq 6) {
Stop-Service $ServiceName 
$GUI.popup("$ServiceName service has been STOPPED and will START again in 5 minutes, soon as you hit OK")
start-Sleep -s 300000
$GUI.popup("Time is up! $ServiceName service will now start as soon as you hit OK")
Start-Service $ServiceName
cls
}

The complete code is listed here:

Function ManageService{
$Service=Get-Service | where {$_.status -eq 'running'} | Out-GridView -OutputMode Single
$GUI = new-object -comobject wscript.shell
if ($Service.Status -eq "Running"){
$ServiceName=$Service.DisplayName
$Yes_Cancle = $GUI.popup("Do you want to turn stop $ServiceName ?", `
0,"Manage Service",3)
If ($Yes_Cancle -eq 6) {
Stop-Service $ServiceName 
$GUI.popup("$ServiceName service has been STOPPED and will START again in 5 minutes, soon as you hit OK")
start-Sleep -s 300000
$GUI.popup("Time is up! $ServiceName service will now start as soon as you hit OK")
Start-Service $ServiceName
cls
}
else {
$GUI.popup("You decided to cancel. $ServiceName will not be stopped")
Start-Service $ServiceName
cls
}}}
cls
ManageService

Hope you all enjoyed this PowerShell: Managed Service GUI. See you next time.