-
Notifications
You must be signed in to change notification settings - Fork 63
/
Sync-Billing.ps1
112 lines (98 loc) · 7.41 KB
/
Sync-Billing.ps1
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# Sync-Billing.ps1
# copy billing files from the old doc server(s) to the new doc server
#import default cmdlet bindings
[CmdletBinding()]
param()
#clear the console screen
cls
[array]$Clients = @();
[array]$Clients = Invoke-SQLCMD -ServerInstance XUTILSQL2 -Database DexmaSites -Query "SELECT DISTINCT dv.dv_value AS DocServer
, [ce].[DatabaseName]
, [ce].[DatabaseServer]
FROM [DexmaSites].dbo.[clients] AS c
JOIN dexmaSites.dbo.client_data_xref cdx
ON cdx.[cl_id] = c.[cl_id]
JOIN dexmaSites.dbo.data_values dv
ON cdx.dv_id = dv.dv_id
JOIN dexmaSites.dbo.ClientEnvironment AS ce
ON ce.[ClientID] = cdx.[cl_id]
WHERE dv.di_id = 96
AND [ce].[EnvironmentID] = 13
ORDER BY [ce].[DatabaseName]"
foreach ( $Client in $Clients ) {
Write-Host "Beginning $($Client.DatabaseName)" -ForegroundColor DarkGray
if (($Client.DocServer -eq "PADoc5") -or ($Client.DocServer -eq "PDocDirect10"))
{
Write-Verbose -Message "$($Client.DatabaseName) on $($Client.DocServer) has not been migrated to the new datacenter.";
}
else
{
Write-Verbose -Message "Beginning $($Client.DatabaseName)";
[array]$CopyFrom = @();
if (Test-Path -Path "\\PDocDirect10\$($Client.DatabaseName)")
{
Write-Verbose -Message "`t\\PDocDirect10\$($Client.DatabaseName) found, enumerating files(source).";
$CopyFrom += @(Get-ChildItem -path "\\PDocDirect10\$($Client.DatabaseName)\Billing\*" )
}
if (Test-Path -Path "\\PADoc5\$($Client.DatabaseName)")
{
Write-Verbose -Message "`t\\PADoc5\$($Client.DatabaseName) folder found, enumerating files(source).";
$CopyFrom += @(Get-ChildItem -path "\\PADoc5\$($Client.DatabaseName)\Billing\*");
}
$DestPath = "\\" + $Client.DocServer + "\" + $Client.DatabaseName + "\Billing";
[array]$CopyTo = @();
if (!(Test-Path -Path $DestPath))
{
Write-Verbose -Message "`t$($DestPath) not found! Creating directory...";
New-Item -ItemType directory -Path $DestPath;
if (!(Test-Path -Path $DestPath))
{
Write-Verbose -Message "Failed creating $($DestPath)...";
continue;
}
else
{
Write-Verbose -Message "`t$($DestPath) created, enumerating files(dest).";
$CopyTo += @(Get-ChildItem -path "$DestPath\*");
}
}
else
{
Write-Verbose -Message "`t$($DestPath) found, enumerating files(dest).";
$CopyTo += @(Get-ChildItem -path "$DestPath\*");
}
Write-Verbose -Message "`tComparing directory contents...";
$Files2Copy = Compare-Object -ReferenceObject $CopyFrom -DifferenceObject $CopyTo -Property name, length -PassThru | Where-Object {$_.SideIndicator -eq "<="};
if ($Files2Copy -ne $NULL)
{
foreach ($File in $Files2Copy)
{
Write-Verbose -Message "`tThis will copy File $($File.FullName) to $DestPath\$($File.Name)";
Copy-Item -Path $($File.FullName) -Destination $DestPath\$($File.Name) #-whatif;
}
}
else
{
Write-Verbose -Message "`tNo files to copy for $($Client.DatabaseName)!";
}
if ($Client.DocServer -eq "PDoc1")
{
$DropPath = "\\PDoc2\Relateprod\PrimeAlliance\" + $Client.DatabaseName + "\Billing";
}
else
{
$DropPath = "\\PDoc1\Relateprod\PrimeAlliance\" + $Client.DatabaseName + "\Billing";
}
if (Test-Path -Path $DropPath)
{
Write-Verbose -Message "`tDropping the $($DropPath) folder!";
Remove-Item -Recurse -Path $DropPath #-whatif;
}
else
{
Write-Verbose -Message "`tNo files to delete for $($Client.DatabaseName)!";
}
Write-Verbose -Message "End $($Client.DatabaseName)";
}
Write-Host "End $($Client.DatabaseName)" -ForegroundColor DarkGray
}