Looking to find service references and database connections in web.config files on a server or a set of servers.
I started with this guys script - http://www.markrainey.me/2013/03/finding-urls-in-text-files.html but he was just interested in unique url’s so I hacked it around a bit and also added in another regex for the connection strings.
This is not perfect by any means but is a start for anyone else (or me in the future)
########################################################### # AUTHOR : Mark Rainey # Ammended - Stuart McLean 2016-05-12 # DATE : 2013-03-13 # COMMENT : Reads in a list of servers and searches # the E:\Live folder for config files. When it finds # them it searches for anything starting with 3 or more # letters (tcp or http) followed by a colon and \\ # now also outputs connection string data and # looks for </value in urls ########################################################### #ERROR REPORTING ALL Set-StrictMode -Version latest $scriptpath = Split-Path -parent $MyInvocation.MyCommand.Definition # A friend at work helped me get this to output to Excel $outputFile = $scriptpath + "\urls.csv" $connectionFile = $scriptpath + "\connections.csv" # This is a file with a FQDN of each server on a new line $serverList = $scriptpath + "\serverlist.txt" # Load server list $servers = Get-Content $serverList $URLS = New-Object System.Collections.ArrayList $ConnectionStrings = New-Object System.Collections.ArrayList $credential = Get-Credential # Find the string and save it to a file Function getStringMatch { Try { # Loop through all servers Foreach ($server In $servers) { # Set UNC Path to files for this server $drive = "\\" + $server + "\d$" New-PSDrive -Root $drive -PSProvider FileSystem -Name z -Credential $credential #The path can be anywhere on your servers you want to search $path = "z:\AppWebSites" #Get list of files $files = Get-Childitem $path web.config -Recurse | Where-Object { !($_.psiscontainer) } # Loop through the server and search all config files under E:\Live #$matches = New-Object System.Collections.ArrayList #$matches.clear() Foreach ($file In $files) { $fullFileName = $file.FullName # regular expression for a URL format #$regex = '([a-zA-Z]{3,})://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)*?' # find any urls $regex = '([a-zA-Z]{3,})(:\/\/.*)(?=<)' # Find all matches in current file and add the Value for each one to an array select-string -Path $fullFileName -Pattern $regex -AllMatches | % { $_.Matches } | % { $found = New-Object psobject -Property @{ server = $server; urlRef = $_.Value; file = $file.FullName } $URLS.add($found) } $connectionStringRegex = '(?<=connectionString=")(.*)(?=")' select-string -Path $fullFileName -Pattern $connectionStringRegex -AllMatches | % { $_.Matches } | % { try{ $connectionString = $_.Value; $source = ($connectionString | select-string -Pattern '(?<=data source=)(.*?)(?=;)').Matches[0].Value $catalog = ($connectionString | select-string -Pattern '(?<=Initial Catalog=)(.*?)(?=;)').Matches[0].Value $sqlUser = ($connectionString | select-string -Pattern '(?<=User ID=|uid=)(.*?)(?=;)').Matches[0].Value $found = New-Object psobject -Property @{ server = $server; source = $source; catalog = $catalog; sqluser = $sqlUser; file = $file.FullName } $connectionStrings.add($found) } Catch { Write-host "could not parse connection string $_" + $connectionString } } } Remove-PSDrive z } } Catch { Write-host "Something failed $_" } Finally { "Finished" } } getStringMatch $URLS.ToArray() | Export-Csv -Path $outputFile -NoTypeInformation $connectionStrings.ToArray() | Export-Csv -Path $connectionFile -NoTypeInformation
No comments:
Post a Comment