By:Nisarg Upadhyay | Last Updated: 2018-12-04 || Related Tips:More > PowerShell
ProblemIn this tip we will walk through how to create a PowerShell module to get a list of files and subfolders of a windows folder and store this information in a SQL Server table.
SolutionIn this tip, I am going to explain the code of a PowerShell module which gets a list of files that reside in a folder and stores this information in a SQL Server table.
I am going to:
Create a SQL table that has a column to store FQN (Fully qualified name) of any file. Show how to write the PowerShell code. Execute PowerShell function using T-SQL.In this tip, I have used the following PowerShell cmdlets.
PowerShell cmdlet Description Invoke-SQLCmd It is used to execute a SQL command. Reference: Invoke-SQLcmd Get-ChildItem It gets a list of files in a specified location. Reference: Get-ChildItem . Foreach-Object It iterates through the collection of items and performs operations against each item. Reference: Foreach-Object Create the SQL Server TableTo store the directory contents in a table, first I will create a table named " tblFileSummary " in database " DemoDatabase ". The following code creates the SQL table.
USE [DemoDatabase]GO
CREATE TABLE [dbo].[tblFileSummary](
[ID] [int] IDENTITY(1,1) NOT NULL,
[File_name] [varchar](max) NULL,
[Fully_Qualified_FileName] [varchar](max) NULL,
[attributes] [varchar](250) NULL,
[CreationTime] [datetime] NULL,
[LastAccessTime] [datetime] NULL,
[LastWriteTime] [datetime] NULL,
[Length] [numeric](10, 2) NULL,
PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO Create a PowerShell Module
As I mentioned, I want to get the list of files within a specific directory. To do that, we will create a PowerShell cmdlet. It accepts the Drive Letter as a parameter, iterates through the entire drive and populates the following details:
File Name Fully qualified file name File attribute Last access time Last modified time File SizeTo execute this function using a SQL Stored procedure, we must register it as a PowerShell module. Once the PowerShell module is registered, we add this module in the PowerShell profile.
First, let’s understand the PowerShell script.
To understand the PowerShell code, I have split it into multiple sections:
Script Part-1Create a PowerShell function. This function accepts the Drive Letter as a parameter. The following code block creates the function.
function global:getFileSummery{
param(
[Parameter(Position=0,mandatory=$true)]
[string[]] $FileLocation
) Script Part-2
Construct a string variable named "$sqlstatment" that has an "Insert" query statement. The following code creates a string variable.
<a href="/cdn-cgi/l/email-protection" data-cfemail="86a2f5f7eaf5f2e7f2e3ebe3e8f2bbc6">[email protected]</a>'INSERT INTO tblFileSummary(
Fully_Qualified_FileName, File_name, attributes, CreationTime, LastAccessTime, LastWriteTime,
Length
)
VALUES (
'{0}',
'{1}',
'{2}',
'{3}',
'{4}',
'{5}',
'{6}'
)
'@ Script Part-3
Populate list of files command and formats the output according to the insert query stored in the "$sqlstatement" variable. The following is the code block.
Invoke-Sqlcmd -Query "Truncate Table tblFileSummary" -ServerInstance TTI412-VM\SQL2017 -database DemoDatabaseGet-ChildItem -Recurse $FileLocation |
select File_name,Fully_Qualified_FileName,attributes, CreationTime, LastAccessTime, LastWriteTime,@{Label="Length";Expression={$_.Length / 1MB -as [int] }}|
ForEach-Object {
$SQL = $sqlstatement -f $_.FullName, $_.name,$_.attributes, $_.CreationTime, $_.LastAccessTime, $_.LastWriteTime,$_.Length
Invoke-sqlcmd -Query $SQL -ServerInstance TTI412-VM\SQL2017 -database DemoDatabase
}
The above code block performs the following tasks:
Truncates table " tblFileSummary " using " Invoke-SQLCmd " cmdlet. It passes " $FilePath " parameter to " Get-ChildItem -Recurse " cmdlet and populates the list of files. " $FilePath " parameter is drive letter/ directory location (C:\Temp\...). Format the output, generated by " Get-ChildItem -Recurse " in multiple columns. Column sequence of output and the insert statement which is stored in " $sqlstatement " parameter must be the same. The output of "Get-ChildItem -Recurse" Column sequence in the insert query FullName {0} Name {1} Attribute {2} CreationTime {3} LastAccesstime {4} LastWriteTime {5} FileSize {6} It creates a new string variable named " $SQLQuery " by concatenating " $sqlstatement " and the values generated by " Get-ChildItem -Recurse " command. The format of "$SQLQuery " parameter is like a T-SQL insert statement. Using " ForEach-Object " cmdlet, it generates multiple insert statements and stores it in a $ SQLQuery variable. See the following image:
By using " Invoke-SQLCmd " command, it inserts the value of the $SQLQuery variable into the tblFileDetails table. Complete PowerShell Script function global:getFileSummery
{
param(
[Parameter(Position=0,mandatory=$true)]
[string[]] $FileLocation
)
Write-Output "Inserting files"
<a href="/cdn-cgi/l/email-protection" data-cfemail="4a6e393b26393e2b3e2f272f243e770a">[email protected]</a>'
INSERT INTO tblFileSummary(
Fully_Qualified_FileName, File_name, attributes, CreationTime, LastAccessTime, LastWriteTime,
Length
)
VALUES (
'{0}',
'{1}',
'{2}',
'{3}',
'{4}',
'{5}',
'{6}'
)
'@
Invoke-Sqlcmd -Query "Truncate Table tblFileSummary" -ServerInstance TTI412-VM\SQL2017 -database DemoDatabase
Get-ChildItem -Recurse $FileLocation |
select File_name,Fully_Qualified_FileName,attributes, CreationTime, LastAccessTime, LastWriteTime,@{Label="Length";Expression={$_.Length /