3535# -Server [protocol:]server[instance_name][,port] (Required to save data to the database)
3636# -Database [database_name] (Required to save data to the database)
3737# -Username [user_name] (Required to save data to the database)
38- # -Password [password] (Required to save data to the database)
38+ # -Password [password] (Required to save data to the database, must be passed as secure string )
3939# -FilePath [csv_file_name] (Required to save data in a .csv format. Ignored if database parameters are specified)
4040#
4141
42- param ([string ] $SubId , [string ] $Server , [string ] $Username , [string ] $Password , [string ] $Database , [string ] $FilePath , [switch ] $UseInRunbook , [switch ] $UseEC )
42+ param (
43+ [string ] $SubId ,
44+ [string ] $Server ,
45+ [string ] $Username ,
46+ [SecureString ] $Password ,
47+ [string ] $Database ,
48+ [string ] $FilePath ,
49+ [switch ] $UseInRunbook ,
50+ [switch ] $IncludeEC
51+ )
4352
4453# The following block is required for runbooks only
4554if ($UseInRunbook ){
@@ -75,14 +84,16 @@ if ($SubId -like "*.csv") {
7584 $subscriptions = Get-AzSubscription
7685}
7786
78- [Boolean ] $useDatabase = $PSBoundParameters.ContainsKey (" ServerInstance " ) -and $PSBoundParameters.ContainsKey (" Username" ) -and $PSBoundParameters.ContainsKey (" Password" ) -and $PSBoundParameters.ContainsKey (" Database" )
87+ [Boolean ] $useDatabase = $PSBoundParameters.ContainsKey (" Server " ) -and $PSBoundParameters.ContainsKey (" Username" ) -and $PSBoundParameters.ContainsKey (" Password" ) -and $PSBoundParameters.ContainsKey (" Database" )
7988
8089# Initialize tables and arrays
8190
8291if ($useDatabase ){
8392
8493 # Database setup
8594
95+ $cred = New-Object System.Management.Automation.PSCredential($Username , $Password )
96+
8697 [String ] $tableName = " Usage-per-subscription"
8798 [String ] $testSQL = " SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES
8899 WHERE TABLE_SCHEMA = 'dbo'
@@ -122,11 +133,11 @@ if ($useDatabase){
122133 $propertiesToSplat = @ {
123134 Database = $Database
124135 ServerInstance = $Server
125- User = $Username
126- Password = $Password
136+ User = $cred . Username
137+ Password = $cred .GetNetworkCredential (). Password
127138 Query = $testSQL
128139 }
129-
140+
130141 # Create table if does not exist
131142 if ((Invoke-SQLCmd @propertiesToSplat ).Column1 -eq 0 ) {
132143 $propertiesToSplat.Query = $createSQL
@@ -370,7 +381,7 @@ foreach ($sub in $subscriptions){
370381
371382 $Date = Get-Date - Format " yyy-MM-dd"
372383 $Time = Get-Date - Format " HH:mm:ss"
373- if ($includeEC -eq $null ){
384+ if ($IncludeEC -eq $null ){
374385 $ahb_ec = 0
375386 $payg_ec = 0
376387 }else {
@@ -385,14 +396,6 @@ foreach ($sub in $subscriptions){
385396 }
386397}
387398
388- # Add the total numbers to the usage array
389-
390- # if ($includeEC -eq $null){
391- # $usageTable += ,(@($Date, $Time, "Total", $null, $total.ahb_std, $total.ahb_ent, $total.payg_std, $total.payg_ent, $total.hadr_std, $total.hadr_ent, $total.developer, $total.express))
392- # }else{
393- # $usageTable += ,(@(($Date, $Time$total, "Total", $null, ($total.ahb_std + $total.ahb_ent*4), ($total.payg_std + $total.payg_ent*4), $total.ahb_std, $total.ahb_ent, $total.payg_std, $total.payg_ent, $total.hadr_std, $total.hadr_ent, $total.developer, $total.express))
394- # }
395-
396399if ($useDatabase ){
397400 Write-Host ([Environment ]::NewLine + " -- Added the usage data to $tableName table --" )
398401}else {
0 commit comments