-
Notifications
You must be signed in to change notification settings - Fork 0
/
Migrar-FTP.ps1
96 lines (88 loc) · 3.73 KB
/
Migrar-FTP.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
# Script para migrar servico de FTP
# Maquina de destino: VM1
$ErrorActionPreference = "Stop"
$FileCSV = "C:\MIGRA\Planilha_Migracao.csv"
$CodFilial = $((Import-Csv $FileCSV -Delimiter ";").CODIGO)
$ServidorAntigo = $((Import-Csv $FileCSV -Delimiter ";").HOSTNAMEATUAL) + "_OLD"
$Destino = $((Import-Csv $FileCSV -Delimiter ";").HOSTNAMEVM1)
$PastaFTP = "C:\migra\ftp"
$ArquivoFTP = $PastaFTP+"\DefaultFTPSite.xml"
$OrigemFTP = "\\"+$ServidorAntigo+"\c$\migra\ftp\DefaultFTPSite.xml"
$DestinoFTP = "\\"+$Destino+"\c$\migra\ftp\DefaultFTPSite.xml"
$FileLocal = "C:\MIGRA\SecureLocal.txt"
$UserLocal = "administrator"
$PassLocal = Cat $FileLocal | ConvertTo-SecureString
$FileCliente = "C:\MIGRA\SecureCliente.txt"
$UserCliente = "dominio\usuario"
$PassCliente = Cat $FileCliente | ConvertTo-SecureString
#
# Obtencao das Credenciais locais e do Cliente
$CredLocal = New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $UserLocal, $PassLocal
$CredCliente = New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $UserCliente, $PassCliente
# Limpando cache do DNS
& ipconfig /flushdns
# Processo de exportacao na maquina de origem ($ServidorAntigo)
Try {
Invoke-Command -ComputerName $ServidorAntigo -Credential $CredCliente -ScriptBlock {
If (-not (Test-Path $using:PastaFTP)) {
mkdir $using:PastaFTP
}
elseif (Test-Path $using:ArquivoFTP) {
Remove-Item $using:ArquivoFTP
}
cmd.exe /c cscript %WINDIR%\system32\iiscnfg.vbs /export /f $using:ArquivoFTP /sp "/LM/MSFTPSVC/1" /inherited /children
if (Test-Path $using:ArquivoFTP) {
Write-Host; Write-Host "Arquivo gerado com sucesso."
}
else {
Write-Host
Write-Host -ForegroundColor Red "Erro ao gerar arquivo de migracao do FTP!"
Write-Host -ForegroundColor Red "Verifique diretamente no servidor antigo:" $using:ServidorAntigo
Write-Host
Exit
}
}
}
Catch {
Write-Host -ForegroundColor Red "Falha ao executar comando de geracao de arquivo de migracao!"
Write-Host -ForegroundColor Red "Mensagem de Erro: " $_.Exception.Message
Write-Host -ForegroundColor Red "Item: " $_.Exception.ItemName
Exit
}
# Copia do arquivo de exportacao da origem para o destino
Try {
Invoke-Command -ComputerName $Destino -Credential $CredCliente -ScriptBlock {
if (-not (Test-Path $using:PastaFTP)) {
mkdir $using:PastaFTP
}
}
Copy-Item $OrigemFTP $DestinoFTP
}
Catch {
Write-Host -ForegroundColor Red "Falha ao criar diretorio de migracao ou ao copiar arquivo do FTP!"
Write-Host -ForegroundColor Red "Mensagem de Erro: " $_.Exception.Message
Write-Host -ForegroundColor Red "Item: " $_.Exception.ItemName
Exit
}
# Processo de importacao na maquina de destino ($Destino)
Try {
Invoke-Command -ComputerName $Destino -Credential $CredCliente -ScriptBlock {
cmd.exe /c cscript %WINDIR%\system32\iiscnfg.vbs /import /sp "/LM/MSFTPSVC/1" /dp "/LM/MSFTPSVC/1" /f $using:ArquivoFTP /inherited /children /merge
if ($? -ne $true) {
Write-Host
Write-Host -ForegroundColor Red "Erro ao importar arquivo de migracao do FTP!"
Write-Host -ForegroundColor Red "Verifique diretamente no servidor destino:" $using:Destino
Write-Host
}
else {
Write-Host; Write-Host "Migracao do FTP realizada com sucesso."; Write-Host
}
}
}
Catch {
Write-Host -ForegroundColor Red "Falha ao executar comando de importacao do FTP!"
Write-Host -ForegroundColor Red "Mensagem de Erro: " $_.Exception.Message
Write-Host -ForegroundColor Red "Item: " $_.Exception.ItemName
}