-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathremoteModifyRedirect.ps1
84 lines (56 loc) · 3.05 KB
/
remoteModifyRedirect.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
# TO MODIFY REDIRECT IN THE NEW PLATFORM
# (ALL ENVIRONMENTS (QA and PROD) IF IT DOES NOT EXIST IN ONE SERVER IT JUST DOES NOT GET MODIFIED)
# ENTER "alias,http://www.newdestination.com" in the file C:\alias.csv (one alias,destination pair per line)
# RUN SCRIPT.
# IF THE REDIRECT SITS IN OTHER SERVERS JUST ADD THEM TO THE $servers ARRAY.
# IF MODIFICATION IS RIGHT ONLY GREEN TEXT IS SHOWN
# IF ALIAS DOES NOT EXIST IN SERVER ITEMNORFOUNDEXCEPTION IS SHOWN (AND TRIES IN NEXT SERVER)
$servers = "","","","","";
Write-Host ""
Write-Host ".: SCRIPT BEGIN :."
Write-Host ""
Write-Host "Reading alias,destination from file C:\aliasToModify.csv ... ... ..."
Write-Host ""
try{ #try to import
Import-CSV C:\aliasToModify.csv -Header alias,destination | Foreach-Object{
$alias = $_.alias
$destination = $_.destination
if ([string]::IsNullOrEmpty($alias) -or [string]::IsNullOrEmpty($destination)){
Write-Host ""
Write-Host "Either alias or destination is empty or format is wrong, please enter alias,destination pairs one per line" -ForegroundColor Red
Exit
}
Foreach ($server in $servers){
&{
Write-Host "----------------------------------------------------------------------------------------------------------------------"
Write-Host ""
Write-Host "Trying to modify -> " $alias " <- destination to -> " $destination " <- in server -> " -NoNewline
Write-Host $server -ForegroundColor Blue -NoNewline
Write-Host " <- "
Write-Host ""
Invoke-Command -ComputerName $server -ArgumentList $alias,$destination,$server -ScriptBlock {
param($alias,$destination,$server)
try{
Import-Module WebAdministration
& Set-WebConfiguration system.webServer/httpRedirect "IIS:\Sites\redirect_$alias" -Value @{enabled="true";destination=$destination;exactDestination="true";httpResponseStatus="Permanent";childOnly="true"}
Write-Host "SUCCESS! "$alias "was modified in" $server " and now points to URL: " $destination -ForegroundColor Green
Write-Host ""
}catch{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
Write-Host "WARNING! " $alias "does not exist on server " $server " "$ErrorMessage -ForegroundColor Red
Write-Host ""
}#closes catch
}#&
} #ScriptBlock
}#Foreach Server
}#Import-CSV
}catch{
#If the file is not readable, empty or wrong formated.
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
Write-Host "Does file exist? " $ErrorMessage -ForegroundColor Red
}
Write-Host ""
Write-Host ".: SCRIPT END :."
Write-Host ""