Wednesday, 22 June 2011

BackConnectionHostNames PowerShell Script

- useful for SharePoint environments where you are accessing the site from the server itself:


You receive error 401.1 when you browse a Web site that uses Integrated Authentication and is hosted on IIS 5.1 or a later version




### Script ###

Param([string]$StringAdditionalUrls)
[System.String[]]$StringAdditionalUrlsArray = $StringAdditionalUrls.Split(",")
$ArrayAdditionalUrls = "$StringAdditionalUrlsArray".Split()

[string]$path = "HKLM:\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0\"
Write-Output "path=$path"
[string]$name = "BackConnectionHostNames"
Write-Output "name=$name"

[string]$currentvalue = (Get-Item "$path").GetValue("$name")
[System.String[]]$ArrayExistingValues = "$currentvalue".Split()
Write-Output "Current Values are: -"
ForEach ($currentAlias in $ArrayExistingValues)
                {
                Write-Output "- $currentAlias    "
                }

Write-Output "Additional Values are: -"
ForEach ($addAlias in $ArrayAdditionalUrls)
                {
                Write-Output "- $addAlias           "
                }
               
Write-Output "`nChecking current values..."
ForEach ($currentAlias in $ArrayExistingValues)
                                {
                                Write-Output "Checking $currentAlias"
                                If ($currentAlias)
                                                {             
                                                If ($newAlias)
                                                                {                                                             
                                                                If($ArrayNewUrls -notcontains $currentAlias)
                                                                                {
                                                                                Write-Output " -Adding: $currentAlias"
                                                                                $newAlias = $newAlias+" "+$currentAlias                                                            
                                                                                }
                                                                }
                                                Else
                                                                {
                                                                $newAlias = $currentAlias
                                                                Write-Output " -Adding $currentAlias"
                                                                }
                                                }                             
                                [System.String[]]$ArrayNewUrls = "$newAlias".Split()
                                }
Write-Output "`nComparing new values..."
ForEach ($addAlias in $ArrayAdditionalUrls)
                {
                Write-Output "Checking: $addAlias.."
                If ($ArrayExistingValues -contains $addAlias)
                                {
                                Write-Output " -$addAlias already exists"
                                }                             
                Else
                                {
                                Write-Output " -Adding: $AddAlias"
                                $newAlias = $newAlias+" "+$AddAlias   
                                }
                }
               
Write-Output "`nNow setting registry values..."

$newAlias = $newAlias.Trim()
[System.String[]]$ArrayNewUrls = "$newAlias".Split()



Write-Output "New list of values : -"
#ForEach ($newAlias in $ArrayNewUrls)
#             {
                Write-Output $newAlias
#             }

Set-ItemProperty -Path $path -Name $name -Value ([string[]]$ArrayNewUrls)

Write-Output "`ncompleted adding URLs to BackConnectionHostNames"

Write-Output "Completed Script: $scriptpath"


1 comment: