Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Powershell Output Formatting in SQL

$
0
0
I'm trying to retrieve Citrix licencing information and then write that info to a sql database for reporting. Let me start off by saying that i don't know much about power-shell, but that i have been able to get this working from scripts that i've found online. Im only having issues with the format the data in my sql table.  
The script as follows:
#Add Snapins
Add-PSSnapin SqlServerCmdletSnapin100
Add-PSSnapin SqlServerProviderSnapin100
#Region Settings
#Your License Server
$CitrixLicenseServer = "AN1XENLIC01.za.if.atcsg.net"
#Do you want to report on licenses with 0 users?
$ShowUnusedLicenses = $true
#SQL Connection settings
$db_server = "X.X.X.X"
$db = "XXXXX"
$table = "XXXXX"
$username = "XXXXX"
$pwd = "XXXXX"
#Toggle an alert above this percentage of licenses used
$UsageAlertThreshold = 80
#EndRegion Settings
#Region CollectData
#retrieve license information from the license server
$LicenseData = Get-WmiObject -class "Citrix_GT_License_Pool" -namespace "ROOT\CitrixLicensing"  -ComputerName $CitrixLicenseServer
$usageReport = @() 
$LicenseData |  select-object pld -unique | foreach { 
   $CurrentLicenseInfo = "" | Select-Object Date, License, Count, Usage, pctUsed
   $CurrentLicenseInfo.License = $_.pld    
   $CurrentLicenseInfo.Count   = ($LicenseData  | where-object {$_.PLD -eq $CurrentLicenseInfo.License } | measure-object -property Count      -sum).sum 
   $CurrentLicenseInfo.Usage   = ($LicenseData  | where-object {$_.PLD -eq $CurrentLicenseInfo.License } | measure-object -property InUseCount -sum).sum
   $CurrentLicenseInfo.pctUsed = [Math]::Round($CurrentLicenseInfo.Usage / $CurrentLicenseInfo.Count * 100,2)
   $CurrentLicenseInfo.Date = Get-Date
   #$CurrentLicenseInfo.Alert   = ($CurrentLicenseInfo.pctUsed -gt $UsageAlertThreshold)
   if ($ShowUnusedLicenses -and $CurrentLicenseInfo.Usage -eq 0) {
       $usageReport += $CurrentLicenseInfo
   } elseif ($CurrentLicenseInfo.Usage -ne 0) {
       $usageReport += $CurrentLicenseInfo
   }
}
#EndRegion CollectData    
$usageReport | Format-Table
#$usageReport | ConvertTo-HTML | out-file "c:\temp\usagereport.htm" -append
#Write results to SQL LICENSE DB
$sql_query = "INSERT INTO $table (Date, License, Count, Usage, PercentageUsed) VALUES ('$CurrentLicenseInfo.Date', '$CurrentLicenseInfo.License', '$CurrentLicenseInfo.Count', '$CurrentLicenseInfo.Usage','$CurrentLicenseInfo.pctUsed')"
Invoke-Sqlcmd -ServerInstance $db_server -Database $db -Username $username -Password $pwd -Query $sql_query
# If any record raises an alert, send an email.
if (($usagereport | where {$_.alert -eq "True"}) -ne $null) {
    #Send Email Here}

So, this works fine except for the formatting of the data in my SQL table. My table results looks like:
DateLicense CountUsage PercentageUsed
@{Date=07/09/2013 07:35:27; License=MPS_ENT_CCU; Count=373; Usage=97; pctUsed=26.01}.Date          @{Date=07/09/2013 07:35:27; License=MPS_ENT_CCU; Count=373; Usage=97; pctUsed=26.01}.License        @{Date=07/09/2013 07:35:27; License=MPS_ENT_CCU; Count=373; Usage=97; pctUsed=26.01}.Count          @{Date=07/09/2013 07:35:27; License=MPS_ENT_CCU; Count=373; Usage=97; pctUsed=26.01}.Usage          @{Date=07/09/2013 07:35:27; License=MPS_ENT_CCU; Count=373; Usage=97; pctUsed=26.01}.pctUsed

How do i get the data format correct in my table:
Date License Count PercentageUsed
07/09/2013 07:35:27 MPS_ENT_CCU 373 97 26.01          



Viewing all articles
Browse latest Browse all 15028

Trending Articles



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