Friday, January 10, 2020

Connect-Azure-PowerShell if there is no existing Connection.



By Using MsolService to connect to Azure PowerShell, could be annoying time to time if unable to validate existing connection. The simple function below will just do that. To make below function work, you will need to adjust “$company” variable to ensure it fits into your environment. Rest of the function is pretty easy to understand.



<#   

.NOTES

#=============================================
# Script      : Connect-Azure-PoWerShell.ps1
# Created     : ISE 3.0
# Author(s)   : Casey.Dedeal
# Date        : 01/10/2020 14:39:23
# Org         : ETC Solutions
# File Name   : Connect-Azure-PoWerShell.ps1
# Comments    :
# Assumptions :
#==============================================


SYNOPSIS           : Connect-Azure-PoWerShell.ps1
DESCRIPTION        : O365 Azure PS connect
Acknowledgements   : Open license
Limitations        : None
Known issues       : None
Credits            : Please visit:
                          https://simplepowershell.blogspot.com
                          https://msazure365.blogspot.com

.EXAMPLE

  .\Connect-Azure-PoWerShell.ps1


  MAP:

  -----------
  #(1)_.Check to see if any return for Valid cmdlet
  #(1.1)_.Connected to O365 Azure PowerShell Module
  #(1.2)_.Add Vars Modify $company Var
  #(2)_.Catch Errors

#>


function Connect-Azure-PoWerShell
{
   

# Connect Azure PowerShell with MsolService if ! Connected
Try{

 #(1)_.Check to see if any return for Valid cmdlet
 $session  = Get-MsolDomain -EA SilentlyContinue
 if ($session) {
    #(1.1)_.Connected to O365 Azure PowerShell Module
    Write-Host 'ALREADY Connected: Azure PowerShell' -f Gray -b DarkGreen

   } else {

    #(1.2)_.Add Vars Modify $company Var
     $company   = '@CloudSec365.onmicrosoft.com'
     $adminUser = "$env:USERNAME"
     $UPN =  $adminUser + $company
     Write-Host 'Connecting: Azure PowerShell' -f Gray -b DarkCyan
     $cred  = Get-Credential -Credential $UPN -ErrorAction Stop
     Connect-MsolService -Credential $cred -ErrorAction Stop | Out-Null
          }
}Catch{

 #(2)_.Catch Errors
 Write-Host "ERROR FOUND:$($PSItem.ToString())"

}
}


Casey, Dedeal
Azure Solutions Architect
AWS Certified Cloud Practitioner

https://simplepowershell.blogspot.com
https://cloudsec365.blogspot.com
https://msazure365.blogspot.com
https://twitter.com/Message_Talk


Sunday, January 5, 2020

Check If Module Exist



Here is simple PowerShell script to check if any given module exists or not. We will be using try and catch block to determine the existence of AzureAD module


# Check If module exist

$module = 'AzureAD'
try {
    Import-Module $module -ErrorAction stop
    Write-Host 'Module exists' -f Green
}
catch {
    Write-Host 'Module does not exist' -f red -b Black
}



There are other ways to accomplish same results. One other way of doing it would be




#(1)_.Assign Vars
$checkModule = 'MsOnline'
$List = (Get-InstalledModule).name

#(2)_.Check if Module Installed
if ($List -contains $checkModule)
{

   Write-host 'Module is installed' -f Green -$checkModule

}

else {

    Write-host 'Module is not installed' -f Red -$checkModule

}




Another way of checking doing same check;

#(3)_.Check if Module Installed
if (!($List -contains $checkModule))
{
   Write-host 'Module is not installed' -f Red -$checkModule
  

}

else {

   Write-host 'Module is installed' -f Green -$checkModule

}



Casey DeDeal
Azure Certified Solutions Architect
AWS Certified Cloud Practitioner

https://msazure365.blogspot.com
https://simplepowershell.blogspot.com
https://twitter.com/Message_Talk




Tuesday, November 19, 2019

Try Catch Errors with PowerShell

TRY AND CATCH BLOCK


With PowerShell when using <TRY> block to define a section of the script, it will store error within $error default variable. In  the example below, we will take advantage of default behavior to capture the error. after intentionally generating it.

# Try Catch Errors


Try {

   $user = "Rambo"
   get-adUser $user -ea stop

}

Catch {

    Write-Host 'Error FOUND:'`n -f white -b blue
    Write-Host "Message: ($($_.Exception.Message))" -f Cyan -b DarkBlue

}


Here is template for same
try
{
      Do Something  
}
Catch
{
      Write-Warning 'Error has occoured'
      Write-host 'Problem FOUND: $($PSItem.ToString())' -f red -b White
      Write-Host $_.Exception.Message -ForegroundColor red
     
}




Casey DeDeal
Azure Certified Solutions Architect
AWS Certified Cloud Practitioner

https://msazure365.blogspot.com
https://simplepowershell.blogspot.com
https://twitter.com/Message_Talk






Connect-Azure-PowerShell if there is no existing Connection.

By Using MsolService to connect to Azure PowerShell, could be annoying time to time if unable to validate existing connection. The simpl...