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

Need this data written to SQL table without having to create an actuall CSV file.

$
0
0

Hello all I am needing to write what is in the DATA section to an SQL table. I have included the Powershell code that I am using to get the objects shown in POWERSHELL DATA GATHER section. Then I want to write that data directly to the SQL Table using something similar to what I have in theWRITE_DATATABLE section. I simply need to find a way to take the data from the POWERSHELL DATA GATHER SECTION and set it up so that the data can be read as a variable without having to create an actual CSV file. Can anyone assist 

POWERSHELL DATA GATHER (this section gathers the data from a txt file and formats it into the objects in the DATA section

$Logfile = "C:\temp\ps3\vpnlogs\testlog.txt"

$LogSample = Get-Content $Logfile -TotalCount 2 |
  ConvertFrom-Csv -Delimiter ' '

$Props = $LogSample.psobject.properties |
 select -ExpandProperty Name |
 Where {$LogSample.$_ -eq ' '}

Import-csv $Logfile -Delimiter ' ' |
Select $Props  | Export-csv "C:\temp\ps3\vpnlogs\logfiledata.csv"

Data

this needs to be written to SQL table (approx 20000 rows)

 TypeName: Selected.System.Management.Automation.PSCustomObject

Name                MemberType   Definition                                                                                      
----                ----------   ----------                                                                                      
Equals              Method       bool Equals(System.Object obj)                                                                  
GetHashCode         Method       int GetHashCode()                                                                               
GetType             Method       type GetType()                                                                                  
ToString            Method       string ToString()                                                                               
Action              NoteProperty System.String Action=Log In                                                                     
Current Rule Number NoteProperty System.String Current Rule Number=                                                              
Date                NoteProperty System.String Date=4Dec2013                                                                     
Destination         NoteProperty System.String Destination=******                                                         
Information         NoteProperty System.String Information=VPN internal source IP: *.*.*.*; reason: disconnected from gateway
Interface           NoteProperty System.String Interface=                                                                        
Number              NoteProperty System.String Number=473                                                                        
Origin              NoteProperty System.String Origin=********                                                                 
Product             NoteProperty System.String Product=Security Gateway/Management                                               
Protocol            NoteProperty System.String Protocol=                                                                         
Rule                NoteProperty System.String Rule=                                                                             
Rule Name           NoteProperty System.String Rule Name=                                                                        
Service             NoteProperty System.String Service=                                                                          
Source              NoteProperty System.String Source=ip*-*-*-*.ok.ok.cox.net                                               
Source Machine Name NoteProperty System.String Source Machine Name=                                                              
Source Port         NoteProperty System.String Source Port=                                                                      
Source User Name    NoteProperty System.String Source User Name=                                                                 
Time                NoteProperty System.String Time=23:59:01                                                                     
Type                NoteProperty System.String Type=Log                                                                          
User                NoteProperty System.String User=******* 

WRITE_DATATABLE  This is basically what I need to use to write directly from a computer or server to the SQL Table.. I do not want to have to create a file rather just take the output and send it directly to the SQL table.

dt = Import-Csv -Path "C:\temp\ps3\vpnlogs\logfiledata.csv" | Out-DataTable
Write-DataTable -ServerInstance "my server" -Database "my database" -TableName "dbo.VPNLOGS" -Data $dt

whole script :

$Logfile = "C:\temp\ps3\vpnlogs\testlog.txt"

$LogSample = Get-Content $Logfile -TotalCount 2 |
  ConvertFrom-Csv -Delimiter ' '

$Props = $LogSample.psobject.properties |
select -ExpandProperty Name |
Where {$LogSample.$_ -eq ' '}

Import-csv $Logfile -Delimiter ' ' |
Select $Props  | Export-csv "C:\temp\ps3\vpnlogs\logfiledata.csv"

dt = Import-Csv -Path "C:\temp\ps3\vpnlogs\logfiledata.csv" | Out-DataTable
Write-DataTable -ServerInstance "my server" -Database "my database" -TableName "dbo.VPNLOGS" -Data $dt


Viewing all articles
Browse latest Browse all 21975

Trending Articles



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