Sunday, December 13, 2020

BACKUP YOUR FILES WITH SIMPLE POWERSHELL FUNCTION

 

BACKUP YOUR FILES WITH SIMPLE POWERSHELL FUNCTION

Following PowerShell will help you to create backup of any desired folder and its content. When running this simple PowerShell function, you will need to provide source and destination variables as shown below.

 

  # Add VARS

 $BackSource = "C:\Users\12024\OneDrive - wiretecpro\Documents\S_Root\"

 $DestSource = "C:\Users\12024\OneDrive - wiretecpro\Documents\Backup\"

 

 

  function Function-Backup{

   

    [CmdletBinding()]

    param(

    [Parameter(Position=0,mandatory=$true)]

    [string] $BackSource,

    [string] $DestSource

    )

 

Try{

 

# Add VARS , start with backup Folder Name

$folderName = 'MyBackup\'

 

# Consantrate Vars

$now = (get-Date -format 'dd-MMM-yyyy-HH-mm-ss-tt')

$BackupFolder  = $folderName+$now

$DestSourceFol =  $DestSource+$BackupFolder

 

# Check DESTINATION Folder, create one if does not exist

if(!(Test-Path  $DestSourceFol)){

 

New-Item $DestSourceFol -ItemType Directory | Out-Null

 

}

 

# Check SOURCE Folder, Stop script if source folder path is not valid

if(!(Test-Path $BackSource)){

Write-Warning 'CANNOT Locate Source'

Write-Warning 'Script will stop'

Start-Sleep -Seconds 3

break;

 

}

 

# Collect all files

$files = (Get-ChildItem $BackSource -recurse)

Write-host 'Backup in progress' -ForegroundColor Green

 

# Start Item by Item Copy

foreach ($file in $files)

{

    if ($file.LastWriteTime -lt ($(Get-Date).AddDays(-1)))

    {  

     Copy-Item $file.FullName -Destination $DestSourceFol -Recurse

 

    }

}

 

# Complete Backup

Write-Host "Completed backup"

 

}Catch{

  $errormessage = $($PSItem.ToString())

  Write-Warning 'Error has occoured'

  Write-host 'Problem FOUND:' $errormessage -ForegroundColor Red -BackgroundColor White

      }

  }

 

 

 

 

  # Add VARS

 $BackSource = "C:\Users\12024\OneDrive - wiretecpro\Documents\S_Root\"

 $DestSource = "C:\Users\12024\OneDrive - wiretecpro\Documents\Backup\"

 

 # Run the function

 Function-Backup -BackSource $BackSource -DestSource $DestSource 

 

 

Casey, Dedeal

Principal Systems Engineer

https://simplepowershell.blogspot.com/  (blog)

https://smtp25.blogspot.com/ (blog)

https://telnet25.wordpress.com/ (blog)

https://twitter.com/Message_Talk (Twitter)

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


Setting up ISE Profile with Options

  Here is updated ISE profile. You can use it to built your own and make few adjustments.       # STARTING ADDS-ON MENU # ...