Monday, March 13, 2017

Powershellist - My backup script


Not really SCCM related, but still useful.  :)

This Powershell script is what I wrote awhile back to backup the entire C: drive to a specified network share.  It prompts to perform a disk check (reboot required) before backing up to ensure there are no bad sectors.  It creates folders based on make, model, and hostname of the computer and outputs the general backup information (hostname, backup date, network location, make, model) to a CSV file on the network share.

Clear-Host
Write-Host "This performs a full system backup using Windows Backup.  Please verify you are logged in as an account that has write access to \\server\share before continuing."
$diskchfile = "C:\windows\temp\disk_check.tag"
if (-not (Test-Path $diskchfile)) { 
$diskch = read-host "Disk check has not been run yet.  Do you want to run a Disk Check now? [y/n] "
IF($diskch -eq "y") {
$text = 'y'
$text | Out-File 'C:\windows\temp\disk_check.tag'
Write-Host "NOTE: Please run this script again to continue backup after the Disk Check."
$text | chkdsk C: /r
Write-Host "Rebooting to run Disk check..."
shutdown /r /t 5
cmd /c pause | out-null
exit
} else {
write-host "Disk check aborted."
}
} else {
Write-Host "Disk check has been performed."
}
$machine = (Get-WmiObject -Class:Win32_ComputerSystem).Model
$make,$model = $machine -Split(" "),2

write-host "Make: $make"
write-host "Model: $model"

$backuppath = "\\server\share\$make\$model\$env:computername"
if ((Test-Path $backuppath)) { 
write-host "WARNING: This machine may have previously been backed up.  Please check the open Explorer window to verify VHD file exists."
Start-Process -filepath explorer.exe -argumentlist "\\server\share\$make\$model\$env:computername"
cmd /c pause | out-null
}

$modelpath = "\\server\share\$make\$model"
if (-not (Test-Path $modelpath)) { 
New-Item -ItemType directory -Path "\\server\share\$make\$model"
}

write-host "Running DontSleep to keep machine awake during backup operation..."
Start-Process -filepath DontSleep.exe

New-Item -ItemType directory -Path "\\server\share\$make\$model\$env:computername"
$date = Get-Date
"$env:computername,$date,\\server\share\$make\$model\$env:computername" | Out-File -Append "\\server\share\Recent backups.csv"
wbadmin start backup "-backupTarget:\\server\share\$make\$model\$env:computername" -include:c: -vssFull
cmd /c pause | out-null
exit

No comments:

Post a Comment