How to Load the Output of a Powershell Function into a SQL Server table

I am using this PowerShell Function to get the local drive and mount point information from a Server.  I need to use this function to retrieve information from many servers and then dump the output into a SQL Server table. I am not sure how to do this correctly. Below is the function I am using as well as some info I gathered but I am not sure how to put it all together. Any suggestions would be appreciated.

Function Get-DiskDrive
{
 
  Param(
  [string[]]$drive = $env:SystemDrive,
  [string[]]$computername = $env:COMPUTERNAME
 ) #end param
  Get-WmiObject -Class win32_Volume -ComputerName $computername |  
  Select-object DriveLetter, Label, FileSystem, PageFilePresent,  
  @{Name = "ComputerName"; Expression = {$_.__Server} },
  @{Name = "Capacity(GB)"; Expression = {$_.capacity / 1GB} },  
  @{Name = "FreeSpace(GB)"; Expression = {$_.Freespace / 1GB} },
  @{Name = "PercentFree"; Expression = { ($_.FreeSpace / $_.Capacity)*100 } }

 
} #end function get-diskdrive



---------------------------------


$serverlist = (Get-Content -path C:\ServerNames\Serverlist.txt)
foreach ($server in $serverlist)
{
Get-DisksSpace $server

$dataTable = Get-DisksSpace $server  | out-DataTable
$connectionString = "Data Source=<server\instance>; Integrated Security=True;Initial Catalog=<databaseName>;"
$bulkCopy = new-object ("Data.SqlClient.SqlBulkCopy") $connectionString
$bulkCopy.DestinationTableName = "<schema.table>"
$bulkCopy.WriteToServer($dataTable)
VitaminDAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

x-menIT super heroCommented:
assuming you have the script from https://gallery.technet.microsoft.com/scriptcenter/4208a159-a52e-4b99-83d4-8048468d29dd

the code:
$serverlist = (Get-Content -path C:\ServerNames\Serverlist.txt)
 foreach ($server in $serverlist)
 {
 $dataTable = Get-DisksSpace $server  | out-DataTable
 $connectionString = "Data Source=<server\instance>; Integrated Security=True;Initial Catalog=<databaseName>;"
 $bulkCopy = new-object ("Data.SqlClient.SqlBulkCopy") $connectionString
 $bulkCopy.DestinationTableName = "<schema.table>"
 $bulkCopy.WriteToServer($dataTable)
}

looks good
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
All true. And in general, if you don't use the content of a var at least twice, don't use a var. That is
foreach ($server in Get-Content C:\ServerNames\ServerList.txt)

Open in new window

is to be preferred over
$serverlist = (Get-Content -path C:\ServerNames\Serverlist.txt)
foreach ($server in $serverlist)

Open in new window

$connectionString above is an exception, as the string is really long, and so using a var here makes the code more readable.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.