First/End of Month Date calculations
$dNow = get-date $dNowMonth = $dNow.Month $dNowYear = $dNow.Year #Use for beginning of month datetime search, first day at 00 am $firstDayOfMonth = get-date "$dNowMonth/1/$dNowYear" #Use for end of month datetime search, first day of next month at 00 am $lastDayOfMonth = $firstDayOfMonth.AddMonths(1) #Use this end of month search when you do not need the time value $ReallastDayOfMonth = $lastDayOfMonth.AddDays(-1) write-host "FirstDay = $firstDayOfMonth LastDay = $lastDayOfMonth RealLastDayOfMonth = $ReallastDayOfMonth"
Create alias if does not exist
 
if ( -not (get-alias zip -ErrorAction SilentlyContinue)) 
    {new-item -path alias:zip -value "C:\Program Files\7-Zip\7z.exe"} 
Map the network drive if it does not exist( with “filesystem” fix for Test-Path on a network drive).
 
$drive = "H:" 
$remotedir = "\\myserver\SYSBACKUPS" 
$localdir = "F:\SYSBACKUPS\" 
$filesel = "sql_db_" + (get-Date -uformat "%Y%m%d") + ".7z" 
if (-not(Test-Path ($localdir+$filesel))) 
    {
    #We do not have the backup file so lets see if network drive is mapped 
    if (-not (Test-Path -path "filesystem::H:\")) 
        { 
        $net = new-object -ComObject WScript.Network
        $net.MapNetworkDrive($drive, $remotedir, $false, "username", "password")
        }
#... Insert rest of the code here
}
	