[CmdletBinding()] param( [Parameter(Mandatory=$false)] [string]$Email = "test.com" ) Begin { $modules = @( "secretserver" "automation" ) # Load AH modules if not present if (-not (get-module $modules)) { Import-AHModule $modules -ErrorAction Stop } Import-Module "SqlServer","dbatools","NetScaler" -Verbose:$false -ErrorAction Stop # Name of SQL Instance where database is located $SqlInstance = "dbserv" # Name of the database $DbName = "test" # Name of the schema in which the above database is in $Schema = "dbo" # Table to load $Table = "NetScaler_stat" # connect to NetScalers using the if ($env:Winusername) { Write-verbose "$(Get-date): Creating Secret Server Login" -Verbose $SecurePassword = ConvertTo-SecureString $env:WinPassword -AsPlainText -Force -ErrorAction Stop $SSCred = New-Object System.Management.Automation.PSCredential ($env:WinUserName, $SecurePassword) -ErrorAction Stop } $NScred = Get-SecretServerCredential -SamAccountName "nses" -OperatorCredentials $SSCred -ErrorAction Stop -Verbose:$false } Process { # Get Primary NetScalers $Netscalers = Get-Primary | select-object -Property DNSName -ExpandProperty DNSName # Connect to each netscaler and insert stats to the database table $NSStats = foreach ($NetScaler in $NetScalers) { Connect-NetScaler -Hostname $NetScaler -Credential $NScred $NSStat = Get-NSStat -Type lbvserver $NSStat | Select-Object name, primaryipaddress, type, state | where-object { $_.name -notlike "*test*" } } $NSStats | Write-SqlTableData -ServerInstance $SqlInstance -DatabaseName $DbName -SchemaName $Schema -TableName $Table -Force }