Monday, January 13, 2014

Find the amount of time it takes to complete a script

Lately I've been running nightly backups against my main computer at midnight every night which got me wondering . . . how long does it take to complete the backup?

Below are a few lines of code that can be added to any script to find out how long it takes your script to complete.

## Begin the timer.
$StartTime = (Get-Date)

## Begining of code.
hostname ; Get-Counter -Counter "\Memory\Available MBytes" -SampleInterval 1 -MaxSamples 3
## End of code

## Stop timer.
$EndTime = (Get-Date)

## Calculate amount of seconds your code takes to complete.
"Elapsed Time: $(($EndTime - $StartTime).totalseconds) seconds"


Sunday, December 29, 2013

Make PowerShell startup blazing fast!!

If you use PowerShell...You HAVE to run this script! This will cut the time it takes PowerShell to start in half!

Function Improve-GAC { Set-Alias ngen (Join-Path ([System.Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory()) ngen.exe)
 [AppDomain]::CurrentDomain.GetAssemblies() |
    sort {Split-path $_.location -leaf} |
    %{
        $Name = (Split-Path $_.location -leaf)
        if ([System.Runtime.InteropServices.RuntimeEnvironment]::FromGlobalAccessCache($_))
        {
            Write-Host "Already GACed: $Name"
        }else
        {
            Write-Host -ForegroundColor Yellow "NGENing      : $Name"
            ngen $_.location | %{"`t$_"}
         }
      }
}Improve-GAC


referance: http://blogs.msdn.com/b/powershell/archive/2007/11/08/update-gac-ps1.aspx

Saturday, December 28, 2013

Backup Windows 8.1

Here is a short little PowerShell Function that I use to create a scheduled job that runs every night at midnight that backups my C:\ drive to my E:\ drive.

The code used to backup Windows 8.1:
wbAdmin start backup -backupTarget:E: -include:C: -quiet

Function Create-ScheduledJob {
<#
.Synopsis
   schedules nightly backups.
.FUNCTIONALITY
   PowerShell v3.0+
#>
[CmdletBinding()]
    Param( [Parameter( Mandatory = $True )]
           $JobName,
           [Parameter( Mandatory = $True )]
           $ScriptToRun,
           $VerbosePreference = "Continue" )

Begin{ $Trigger = (New-JobTrigger -Daily -At 12:01AM) }

Process{ Register-ScheduledJob -Name $JobName -Trigger $Trigger -ScriptBlock { "& wbAdmin start backup -backupTarget:E: -include:C: -quiet" } }

End { Write-Verbose "Your new ScheduledJob has been created successfully!" }

}Create-ScheduledJob