Upload New File
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
$VerbosePreference = "continue"
|
||||
$CurrentUser = $env:USERNAME
|
||||
#email settings
|
||||
|
||||
$password = ConvertTo-SecureString 'BKW1xaHJEAgWKo8nUvRBYlP3vBIPN0m+LrtRAMbmtmu2' -AsPlainText -Force
|
||||
$MailCredential = New-Object System.Management.Automation.PSCredential ("AKIAQTLVYSCHMP7BYC4U", $password)
|
||||
$SMTPServer = "email-smtp.eu-central-1.amazonaws.com"
|
||||
$SMTPPort = "587"
|
||||
$From = "no-reply@roser.prometheusgroup.app"
|
||||
$Subject = "BP usage report"
|
||||
$Body = "<h2>Herewith the BP usage report per sites</h2><br><br>"
|
||||
|
||||
|
||||
|
||||
$databases = @{
|
||||
"bp-srvsql01" = "p_rc_bp_rotterdam","p_rc_bp_gelsenkirchen","p_rc_bp_lingen"
|
||||
"bp-srvsql05" = "p_rc_bp_whiting","p_rc_bp_cherry_point"
|
||||
}
|
||||
|
||||
foreach($server in $databases.keys ){
|
||||
|
||||
write-verbose "####################################################################"
|
||||
Write-Verbose "######################### $server ######################"
|
||||
write-verbose "####################################################################"
|
||||
|
||||
<#
|
||||
$session = New-PSSession -ComputerName $server
|
||||
|
||||
Invoke-Command -Session $session -ScriptBlock {
|
||||
hostname
|
||||
|
||||
$VerbosePreference = "continue"
|
||||
|
||||
$query = "
|
||||
DECLARE @start2023 Datetime = '20230101';
|
||||
DECLARE @end2023 Datetime = '20240101';
|
||||
WITH RankedDays AS (
|
||||
SELECT
|
||||
login_day,
|
||||
column_2023_concurrent_user_count,
|
||||
ROW_NUMBER() OVER (PARTITION BY MONTH(login_day) ORDER BY column_2023_concurrent_user_count DESC) AS rn
|
||||
FROM
|
||||
(
|
||||
select login_day, max(column_2023_concurrent_user_count) column_2023_concurrent_user_count from (
|
||||
SELECT
|
||||
DISTINCT CAST(ul_main.logindate AS DATE) AS login_day,
|
||||
( SELECT
|
||||
COUNT(DISTINCT CHECKSUM(ul_sub.fk_userid, ul_sub.createcomputer))
|
||||
FROM
|
||||
tblUserLogin ul_sub
|
||||
INNER JOIN tblApplication a ON a.pk_applicationid = ul_sub.fk_applicationid
|
||||
WHERE
|
||||
a.enumvalue IN (2, 3, 4, 5, 7, 11, 13, 15, 6, 9, 18, 10) --no rosercatalog!
|
||||
AND ul_sub.logindate <= ul_main.logindate
|
||||
AND (
|
||||
CASE
|
||||
WHEN lastheartbeat < logoutdate
|
||||
THEN lastheartbeat
|
||||
ELSE logoutdate
|
||||
END
|
||||
) >= ul_main.logindate
|
||||
) AS column_2023_concurrent_user_count
|
||||
FROM
|
||||
tblUserLogin ul_main
|
||||
WHERE
|
||||
ul_main.logindate >= @start2023
|
||||
AND ul_main.logindate < @end2023
|
||||
) max_of_each_month group by login_day ) sub_query
|
||||
WHERE login_day LIKE '2023%'
|
||||
)
|
||||
|
||||
SELECT
|
||||
login_day,
|
||||
column_2023_concurrent_user_count
|
||||
FROM
|
||||
RankedDays
|
||||
WHERE
|
||||
rn <= 2
|
||||
ORDER BY
|
||||
MONTH(login_day),
|
||||
column_2023_concurrent_user_count DESC;"
|
||||
|
||||
|
||||
|
||||
|
||||
foreach($db in $args ){
|
||||
|
||||
write-verbose "executing the query on $db"
|
||||
#Invoke-Sqlcmd -Query $query -ServerInstance "localhost" -ConnectionTimeout 0 -Querytimeout 0 -Database $db | Export-CSV "C:\Users\$CurrentUser\Desktop\$db.csv" -NoTypeInformation
|
||||
$Body += "$db<br>"
|
||||
$Body += "hello"
|
||||
|
||||
|
||||
}
|
||||
} -ArgumentList $databases[$server]
|
||||
#>
|
||||
foreach($db in $databases[$server]){
|
||||
|
||||
write-verbose "copy the $db file from $server"
|
||||
#Copy-Item -FromSession $session C:\Users\$CurrentUser\Desktop\$db.csv -Destination C:\Users\$CurrentUser\Desktop\$db.csv
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
$To = "pgeeraert@prometheusgroup.com"
|
||||
$Cc = ""
|
||||
#$Attachment = (get-childitem "C:\Users\$CurrentUser\Desktop\*.csv").FullName
|
||||
|
||||
$databases.values|ForEach-Object{$body += $PSItem;$body += "<br>"; }
|
||||
|
||||
try {
|
||||
#Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -BodyAsHtml -SmtpServer $SMTPServer -Port $SMTPPort -UseSsl -Credential $MailCredential -Attachments $Attachment
|
||||
|
||||
}
|
||||
catch {
|
||||
Write-Warning "The email failed to start"
|
||||
}
|
||||
$body
|
||||
Reference in New Issue
Block a user