Last active
January 28, 2020 14:34
-
-
Save jivoi/e09062a7966cd847ef45fef0a8225d5b to your computer and use it in GitHub Desktop.
SQLServerLinkCrawl-2-Cypher.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://github.com/NetSPI/PowerUpSQL | |
# Get-SQLServerLinkCrawl -verbose -instance "[ip-address]\SQLSERVER2008" -username 'guest' -password 'guest' | export-clixml c:\temp\links.xml | |
$List = Import-CliXml 'C:\temp\links.xml' | |
$Servers = $List | select name,version,path,user,sysadmin -unique | where name -ne 'broken link' | |
$Outnodes = @() | |
$Outpaths = @() | |
foreach($Server in $Servers){ | |
$Outnodes += "$([string][math]::abs($Server.Name.GetHashCode())),$($Server.Name),$($Server.Version)" | |
if($Server.Path.Count -ne 1){ | |
$Parentlink = $Server.Path[-2] | |
foreach($a in $Servers){ | |
if(($a.Path[-1] -eq $Parentlink) -or ($a.Path -eq $Parentlink)){ | |
[string]$Parentname = $a.Name | |
break | |
} | |
} | |
$Outpaths += "$([math]::abs($Parentname.GetHashCode())),$([math]::abs($Server.Name.GetHashCode())),$($Server.User),$($Server.Sysadmin)" | |
} | |
} | |
$Outnodes | select -unique | out-file C:\pathtoneo4j\Neo4j\default.graphdb\Import\nodes.txt | |
$Outpaths | select -unique | out-file C:\pathtoneo4j\default.graphdb\Import\links.txt | |
<# | |
[OPTIONAL] Cypher to clear the neo4j database: | |
MATCH (n) | |
OPTIONAL MATCH (n)-[r]-() | |
DELETE n,r | |
-- | |
Cypher statement to create a neo4j graph - load nodes | |
LOAD CSV FROM "file:///nodes.txt" AS row | |
CREATE (:Server {ServerId: toInt(row[0]), Name:row[1], Version:row[2]}); | |
--- | |
Cypher statement to create a neo4j graph - load links | |
USING PERIODIC COMMIT | |
LOAD CSV FROM "file:///links.txt" AS row | |
MATCH (p1:Server {ServerId: toInt(row[0])}), (p2:Server {ServerId: toInt(row[1])}) | |
CREATE (p1)-[:LINK {User: row[2], Sysadmin: row[3]}]->(p2); | |
--- | |
[OPTIONAL] Cypher statement to create a start node which indicates where the crawl started. This is not automated; first node id must be filled in manually (i.e. replace 12345678 with the first node's id). | |
CREATE (:Start {Id: 1}) | |
[OPTIONAL] Link start node to the first server | |
MATCH (p1:Start {Id: 1}), (p2:Server {ServerId: 12345678}) | |
CREATE (p1)-[:START]->(p2); | |
#> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment