Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Need to format a vpn log text file into a format that can be written directly to a SQL table

$
0
0

I am trying to do two thing inside of powershell

1. Take a vpnlog text file  "C:\temp\ps3\vpnlogs\testlog.txt" formatted as such and be able to directly add the columns to a sql table

"Number" "Date" "Time" "Interface" "Origin" "Type" "Action" "Service" "Source Port" "Source" "Destination" "Protocol" "Rule""Rule Name" "Current Rule Number" "User" "Information" "Product" "Source Machine Name" "Source User Name"
"291" "29Nov2013" "23:59:00" "" "A*****" "Log" "Log In" "" "" "111.*.*.* " "A*****" "" "" "" "" "pet*******""VPN internal source IP: 192.*.*.*; reason: disconnected from gateway" "Security Gateway/Management" "" ""

2. Remove the empty colums in this text file so that I can then find a way to upload only colums that have a column name directly up to the sql table via a powershell script .

SO basically import txt file format it into colums that have data that can then be directly written to a sql table without me having to manually create the columns in the table. if this worked correctly the sql table would be uploaded with the colums and data from sql.

I can do the upload portion with the below listed powershell command however when I import the following file into powershell I have to manually import the testlog.txt into excel then save as a csv to upload... not to mention I had to manually create the columns in the sql table so that it imports correctly.

get-content "C:\temp\ps3\vpnlogs\testlog.txt" I can not get the data to format correctly without manually importing it into the logfiledata.csv.

$dt=Import-Csv-Path"C:\temp\ps3\vpnlogs\logfiledata.csv"|Out-DataTable

Write-DataTable -ServerInstance"myserver"-Database"mydata"-TableName"dbo.VPNLOGS"-Data$dt

I also have tried this to format the text file but it's not working the way I thought it should, but I don't want to have to create the .CSV file if I can avoid it I would rather take the data in powershell formatted and use the colums to

get-content"C:\temp\ps3\vpnlogs\testlog.txt"|Foreach-Object{ ($_ -replace'"',',') } |Set-Content"C:\temp\ps3\vpnlogs\testlog2.csv" | Import-Csv "C:\temp\ps3\vpnlogs\testlog2.csv"|selectNumber,date,time,interface,origin,type,action,service,"sourceport",source,destination,protocol,rule,"rule name","current rule number",user,information,product,"source machine name","source user name"


"Number" "Date" "Time" "Interface" "Origin" "Type" "Action""Service" "Source Port" "Source" "Destination" "Protocol" "Rule" "Rule Na

me" "Current Rule Number" "User" "Information" "Product" "Source Machine Name" "Source User Name"

"473" "4Dec2013" "23:59:01" "" "AAMESXF604" "Log" "Log In" "" "" "ip72-198-30-53.ok.ok.cox.net" "AAMESXF604" "" "" """" "usthompst"



Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>