-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPrompt_Local_NetworkBackup.ps1
More file actions
66 lines (58 loc) · 2.68 KB
/
Prompt_Local_NetworkBackup.ps1
File metadata and controls
66 lines (58 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#Execute
###############################################################################################
# Backup local machine to file share using user input
# Note: This does not work in a Windows Recovery Environment (untested in WinPE)
###############################################################################################
$rmtShare = Read-Host "UNC Backup Location"
$bakUser = Read-Host "Backup User"
$credential = Read-Host "Password" -AsSecureString
# Determine if running in a WinPE environment
$wpe = $false
if(Test-Path ($env:WINDIR +"\Systm32\wpeutil.exe")){$wpe = $true}
# Set backupTgt
if (!$wpe){
$backupTgt = (Get-WmiObject Win32_OperatingSystem).SystemDrive # Local OS drive
} else {
$backupTgt = 'C:'
}
# Get computername
function Get-ComputerName {
$pcname = $env:COMPUTERNAME
if ($wpe){
$search = (Get-Content -Path ($backupTgt + '\Windows\debug\NetSetup.log') | Select-String "called for computer '")
if (($search.Length -ge 1) -and ($search.Length -lt 70)){
$searchline = $search[($search.Length - 1)].ToString()
$searchlist = $searchline.Split("'")
$pcname = $searchlist[($searchlist.Length - 2)]
}
}
Return $pcname
}
$pcname = Get-ComputerName
function backupToServer {
[CmdletBinding()] Param(
[Parameter(Position = 0, Mandatory = $true)]
[SecureString]$securestring,
[Parameter(Position = 2, Mandatory = $true)]
[String]$backupUser,
[Parameter(Position = 3, Mandatory = $true)]
[String]$backupLocation
)
# Handle SecureString
$encryptedstring = $securestring | ConvertFrom-SecureString
$securepassword = ConvertTo-SecureString $encryptedstring
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($securepassword)
$backupPass = [String]([System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR))
## Backup to network
if ($wpe){
cmd /c "wpeutil.exe InitializeNetwork"
WBADMIN START BACKUP -backupTarget:$backupLocation -user:$backupUser -password:$backupPass -include:$backupTgt -allCritical -quiet -noInheritAcl -noVerify
} else {
if ((Get-Service -Name NetLogon).Status -ne "Running"){ Start-Service NetLogon }
WBADMIN START BACKUP -backupTarget:$backupLocation -user:$backupUser -password:$backupPass -include:$backupTgt -allCritical -quiet -noInheritAcl
}
[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($BSTR)
$backupPass = $null
}
Write-Host "Backing up this PC ($pcname)..." -ForegroundColor Cyan
backupToServer -secureString $credential -backupUser $bakUser -backupLocation $rmtShare