‎05-21-2019 06:18 PM
# This first query simply joins a couple tables to get me all my subnets by site. This DB is mine and is not a LS DB.
$DataSet = Get-SQLDataSet "centraldb.server.com" "SiteVLANs" "SELECT [LS_Servers].[SiteCode],[LS_Servers].[IPAddress] As LS_IP,[VLANs].[VLAN],[VLANs].[AboveBelow],[VLANs].[Description],[VLANs].[StartingIP],[VLANs].[EndingIP] FROM [Plant_VLANs].[dbo].[VLANs] INNER JOIN [Plant_VLANs].[dbo].[LS_Servers] ON LS_Servers.SiteCode=VLANs.SiteCode WHERE [LS_Servers].[Accessible]=1 ORDER BY LS_Servers.SiteCode"
foreach($row in $Dataset.Tables[0])
{
If ($SubnetSiteCode -ne $row["SiteCode"]) {
$SubnetSiteCode = $row["SiteCode"]
$SubnetLSServer = $row["LS_IP"]
}
$SubnetIPStart = $row["StartingIP"]
$SubnetExIPStart = Get-ExpandedIP $SubnetIPStart
$SubnetIPEnd = $row["EndingIP"]
$SubnetExIPEnd = Get-ExpandedIP $SubnetIPEnd
$SubnetDesc = $row["SiteCode"] + "-" + $row["AboveBelow"] + "-VLAN" + $row["VLAN"] + " - " + $row["Description"]
$SQLText_General = $row["SiteCode"] + "-" + $row["AboveBelow"] + "-VLAN" + $row["VLAN"]
############### Add Subnet to IP Scan Range
# Get server name
$SQLText_ServerName = "Select Servername from [lansweeperdb].[dbo].[tsysASServers]"
$IDS_ServerName = Get-SQLDataSet $SubnetLSServer "lansweeperdb" $SQLText_ServerName
# INSERTs Subnet into IP Scan table
foreach($row in $IDS_ServerName.Tables[0])
{
$SQLText_IPScan = "INSERT INTO [lansweeperdb].[dbo].[tsysIPScanRanges] ([Servername],[Ipstart], [Ipend], [Enabled], [PingTimeout], [IPIgnoreWindows], [DontPing], [Day1], [Day2], [Day3], [Day4], [Day5], [Day6], [Day7], [Day1time], [Day2time], [Day3time], [Day4time], [Day5time], [Day6time], [Day7time], [NoSSH], [Recurring], [Minutes], [Waittime], [SSHport], [SavePingedIP], [IPIgnoreKnownWindows], [Description], [SIPport], [ScanNow]) VALUES ('" + $row["Servername"] + "','" + $SubnetIPStart + "','" + $SubnetIPEnd + "', 0, 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, '1900-01-01 12:00:00.000', '1900-01-01 12:00:00.000', '1900-01-01 12:00:00.000', '1900-01-01 12:00:00.000', '1900-01-01 12:00:00.000', '1900-01-01 12:00:00.000', '1900-01-01 12:00:00.000', 0, 0, 1, 1, 22, 1, 0, '" + $SubnetDesc + "', '5060', 0)"
write-host Writing IP Subnet Scan - $SQLText_General
$IDS_IPScan = Get-SQLDataSet $SubnetLSServer "lansweeperdb" $SQLText_IPScan
}
$row = 0
############### IP Location Stuff
# Query sets the IP Location for each subnet
$SQLText_IPLocation = "INSERT INTO [lansweeperdb].[dbo].[tsysIPLocations] ([StartIP],[EndIP],[IPLocation],[Realstart],[Realend],[PackageShare],[ShareUsername],[SharePassword]) VALUES ('" + $SubnetExIPStart + "','" + $SubnetExIPEnd + "','" + $SubnetDesc + "','" + $SubnetIPStart + "','" + $SubnetIPEnd + "','','','')"
write-host Writing to IP Location - $SQLText_General
$IDS_IPLoc = Get-SQLDataSet $SubnetLSServer "lansweeperdb" $SQLText_IPLocation
############### Group Filters
# Query gets the max value of Location ID to set the next Location ID
$SQLText_AssetGroupID = "Select max(AssetGroupID) + 1 As Next_AssetGroupID from [lansweeperdb].[dbo].[tblAssetGroups]"
$AG_InternalDataSet = Get-SQLDataSet $SubnetLSServer "lansweeperdb" $SQLText_AssetGroupID
$IDS_AssetGroupID = Get-SQLDataSet $SubnetLSServer "lansweeperdb" $SQLText_AssetGroupID
# Query sets the Asset Group for the subnet
$SQLText_AssetGroup_1 = "INSERT INTO [lansweeperdb].[dbo].[tblAssetGroups] ([AssetGroupID],[AssetGroup],[Builtin],[Dynamic]) VALUES ("
$SQLText_AssetGroup_2 = ",'" + $SubnetDesc + "',0,1)"
# Query sets the Asset Group Filter for the subnet
$SQLText_AssetGroupF_1 = "INSERT INTO [lansweeperdb].[dbo].[tsysAssetGroupFilter] ([AssetGroupID],[Compare],[Operator],[Comparevalue],[VersionCompareValue]) VALUES ("
$SQLText_AssetGroupF_2 = ",5,1,'" + $SubnetDesc + "','')"
############### Execute Dynamic Asset Things
foreach($row in $IDS_AssetGroupID.Tables[0])
{
write-host Writing Filter Group $row["Next_AssetGroupID"] - $SQLText_General
$SQLText_AssetGroup = $SQLText_AssetGroup_1 + $row["Next_AssetGroupID"] + $SQLText_AssetGroup_2
$SQLText_AssetGroupF = $SQLText_AssetGroupF_1 + $row["Next_AssetGroupID"] + $SQLText_AssetGroupF_2
$IDS_AssetGroup = Get-SQLDataSet $SubnetLSServer "lansweeperdb" $SQLText_AssetGroup
$IDS_AssetGroupF = Get-SQLDataSet $SubnetLSServer "lansweeperdb" $SQLText_AssetGroupF
}
$row = 0
}
Experience Lansweeper with your own data. Sign up now for a 14-day free trial.
Try Now