-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRemote_NetworkBackup.ps1
More file actions
56 lines (51 loc) · 3.05 KB
/
Remote_NetworkBackup.ps1
File metadata and controls
56 lines (51 loc) · 3.05 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
#Execute
###############################################################################################
# Backup a machine remotely using environment_settings.xml
#
# WinRM must be enabled on the remote machine
###############################################################################################
### Read in environment_settings.xml to get Domain and Admin info
Set-Location $PSScriptRoot
$localDir = $pwd.Path
$settingsXMLFile = $localDir + '\' + 'environment_settings.xml'
$xml = [xml](Get-Content $settingsXMLFile) # Read XML file
$adminName = $xml.environment.domain + '\' + $xml.environment.domainadmin
# Allow authentication
if ($cred) {} else { $cred = Get-Credential $adminName }
$cryptKey = $xml.environment.backupusersalt # get encryption key from XML settings file
$cryptedPass = $xml.environment.backupuserpass # get encrypted password from XML settings file
$rmtShare = '\\' + $xml.environment.backupserver + '\' + $xml.environment.backupshare
$bakUser = $xml.environment.backupuser
$backupBlock = [ScriptBlock]::Create({
function backupToServer {
[CmdletBinding()] Param(
[Parameter(Position = 0, Mandatory = $true)]
[String]$cryptedPass,
[Parameter(Position = 1, Mandatory = $true)]
[String]$cryptKey,
[Parameter(Position = 2, Mandatory = $true)]
[String]$backupUser,
[Parameter(Position = 3, Mandatory = $true)]
[String]$backupLocation
)
$backupTgt = (Get-WmiObject Win32_OperatingSystem).SystemDrive # Local OS drive
function stringToBytes ($keybitstring){
$bitsplits = $keybitstring.Split(',') # Convert string back into list
$bitsplitn = @() # List to hold integers
$bitsplits | ForEach-Object { $bitsplitn += [Int32]$_ } # Convert strings into Int32
[Byte[]]$key = $bitsplitn
return [Byte[]]$key
}
[Byte[]]$key = stringToBytes -keybitstring $cryptKey
$encryptPass1 = [String]$cryptedPass | ConvertTo-SecureString -Key $key # Decrypt with key
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($encryptPass1) # rotate into store
$backupPass = [String]([System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)) # get string value
## Verify local NetLogon service is running and start if it is not
if ((Get-Service -Name NetLogon).Status -ne "Running"){ Start-Service NetLogon }
## Backup to network
WBADMIN START BACKUP -backupTarget:$backupLocation -user:$backupUser -password:$backupPass -include:$backupTgt -allCritical -quiet -noInheritAcl
}
})
$backupBlock2 = [ScriptBlock]::Create($backupBlock.ToString() + "backupToServer -cryptedPass `"" + $cryptedPass + "`" -cryptKey `"" + $cryptKey + "`" -backupUser `"" + $bakUser + "`" -backupLocation `"" + $rmtShare + "`"")
$pcname = Read-Host "Computer to back up"
Invoke-Command -ComputerName $pcname -Credential $cred -ScriptBlock $backupBlock2 -AsJob -JobName ($pcname + '_backup')