-
Notifications
You must be signed in to change notification settings - Fork 1
/
pscp.ps1
123 lines (93 loc) · 2.97 KB
/
pscp.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
113
114
115
116
117
118
119
120
121
122
123
$Path = "C:\export\files\"
$PostfixUrl = "[email protected]:/home/user/"
$PscpPath = "C:\export\"
$pscp = "pscp.exe"
$PostfixPrivateKey = "user.ppk"
$ScpCopyCmd = '& $PscpPath$pscp -i $PscpPath$PostfixPrivateKey $ScpOptions $PostfixUrl'
$ComputerName = $env:COMPUTERNAME.ToLower()
$EmailFile = "email_$ComputerName.txt"
$EmailFileold = "email_$ComputerName.old"
$EmailHashFile = "email_$ComputerName.hash"
$lockfile = “c:\export\lock.lck"
$lockstatus = 0
While ($lockstatus -ne 1)
{
If (Test-Path $lockfile)
{
echo “Lock file found!”
$pidlist = Get-content $lockfile
If (!$pidlist)
{
$PID | Out-File $lockfile
$lockstatus = 1
}
$currentproclist = Get-Process | ? { $_.id -match $pidlist }
If ($currentproclist)
{
echo “lockfile in use by other process!”
$rndwait = New-Object system.Random
$rndwait= $rndwait.next(1,4)
echo “Sleeping for $rndwait seconds”
Start-Sleep $rndwait
}
Else
{
Remove-Item $lockfile -Force
$PID | Out-File $lockfile
$lockstatus = 1
}
}
Else
{
$PID | Out-File $lockfile
$lockstatus = 1
}
}
## Main Script Part
## ----------------
if (!(Test-Path $Path$EmailFile -PathType Leaf)){
Out-File -FilePath $Path$EmailFile
}
if (!(Test-Path $Path$EmailHashFile -PathType Leaf)){
Out-File -FilePath $Path$EmailHashFile
}
if ((Test-Path $Path$EmailFileold -PathType Leaf)){
del $Path$EmailFileold
}
move $Path$EmailFile $Path$EmailFileold
Out-File -FilePath $Path$EmailFile
### Get data from MS Exchange and export recipients to csv
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn;
$addresses = @()
$recipients = Get-Recipient -ResultSize Unlimited
foreach ($read in $recipients){$addresses += $read.EmailAddresses.SmtpAddress; $addresses += $read.ExternalEmailAddress.SmtpAddress}
$addresses | sort -uniq | select {($_).tolower()} | Export-Csv $Path$EmailFile
Remove-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn;
###Comparing two files and sending new file to smarthost when old and new file is not equal
$EmailFileHash = $(Get-FileHash $Path$EmailFile).Hash
$EmailFileoldHash = $(Get-FileHash $Path$EmailFileold).Hash
Write-Output $EmailFileHash | Out-File -Encoding UTF8 $Path$EmailHashFile
if ($EmailFileHash -ne $EmailFileoldHash) {
Write-Output "Files $EmailFile and $EmailFileold aren't equal"
Start-Sleep -s 2
$ScpOptions = "-ls"
$AAAA = Invoke-Expression $ScpCopyCmd | select-string $EmailFile
if ([string]::IsNullOrWhitespace($AAAA)){
$ScpOptions = "$Path$EmailFile"
Invoke-Expression $ScpCopyCmd
Start-Sleep -s 2
$ScpOptions = "$Path$EmailHashFile"
Invoke-Expression $ScpCopyCmd
}
else {
Write-Output "File still exist on remote host"
Out-File -FilePath $Path$EmailFile
}
}
else {
Write-Output "New file is the same as old"
Start-Sleep -s 2
}
## -----------------
#remove the lockfile
Remove-Item $lockfile –Force