Skip to content

Commit afd8a0d

Browse files
some changes
1 parent da85fb0 commit afd8a0d

12 files changed

+2967
-836
lines changed

Diff for: notebooks/NotDotNet/Agent/AGENT - Getting Run Time Webpage.ipynb

+7-5
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,23 @@
7272
},
7373
"outputs": [
7474
{
75+
"output_type": "stream",
7576
"name": "stdout",
76-
"text": "The PowerShell Gallery is trusted I will continue\nWe have the dbatools module already\n",
77-
"output_type": "stream"
77+
"text": "The PowerShell Gallery is trusted I will continue\nWe have the dbatools module already\n"
7878
}
7979
],
8080
"execution_count": 1
8181
},
8282
{
8383
"cell_type": "code",
8484
"source": [
85-
"$Instance = 'SQL2017n5' # put the instance in the single quotes\r\n",
85+
"$Instance = 'localhost,15592' # put the instance in the single quotes\r\n",
8686
"$daysbehind = 1 # Choose how many days back you need the information - It will show a 24 hour period\r\n",
8787
"$StartDate = (Get-Date).AddDays(- $daysbehind)\r\n",
8888
"$DoW = $StartDate.DayOfWeek\r\n",
89-
"\r\n",
89+
"$PSDefaultParameterValues = @{\r\n",
90+
" \"*Dba*:SQLCredential\" = New-Object System.Management.Automation.PSCredential ((Get-Secret -Name container-demo-sql-user -AsPlainText), (Get-Secret -Name container-demo-sql-pwd))\r\n",
91+
"}\r\n",
9092
"$FileName = 'C:\\temp\\' + $instance + '_' + $DoW + '_' + $StartDate.tostring(\"MM-dd-yyyy\") + '.html'\r\n",
9193
"$getDbaAgentJobHistorySplat = @{\r\n",
9294
" SqlInstance = $Instance\r\n",
@@ -107,7 +109,7 @@
107109
"output_type": "stream"
108110
}
109111
],
110-
"execution_count": 2
112+
"execution_count": 1
111113
},
112114
{
113115
"cell_type": "markdown",

Diff for: notebooks/NotDotNet/Audit/AUDIT - Instance Permissions to Excel.ipynb

+14-13
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@
6666
},
6767
"outputs": [
6868
{
69-
"output_type": "stream",
7069
"name": "stdout",
71-
"text": "The PowerShell Gallery is trusted I will continue\nWe have the dbatools module already\nWe have the importExcel module already\n. {\n"
70+
"text": "The PowerShell Gallery is trusted I will continue\nWe have the dbatools module already\nWe have the importExcel module already\n",
71+
"output_type": "stream"
7272
}
7373
],
74-
"execution_count": 2
74+
"execution_count": 4
7575
},
7676
{
7777
"cell_type": "markdown",
@@ -102,12 +102,12 @@
102102
},
103103
"outputs": [
104104
{
105-
"output_type": "stream",
106105
"name": "stdout",
107-
"text": ""
106+
"text": "",
107+
"output_type": "stream"
108108
}
109109
],
110-
"execution_count": 5
110+
"execution_count": 1
111111
},
112112
{
113113
"cell_type": "markdown",
@@ -124,7 +124,10 @@
124124
"cell_type": "code",
125125
"source": [
126126
"$ExcelDirectory = 'c:\\temp\\' # Alter this to the directory you want the file created\r\n",
127-
"$SQlinstance = 'SQL2017N5' # Alter this for the SQL Instance you want to get permissions for\r\n",
127+
"$SQlinstance = 'localhost,15592' # Alter this for the SQL Instance you want to get permissions for\r\n",
128+
"$PSDefaultParameterValues = @{\r\n",
129+
" \"*Dba*:SQLCredential\" = New-Object System.Management.Automation.PSCredential ((Get-Secret -Name container-demo-sql-user -AsPlainText), (Get-Secret -Name container-demo-sql-pwd))\r\n",
130+
"}\r\n",
128131
"Write-Output \"Processing $sqlinstance\"\r\n",
129132
"\r\n",
130133
"$InstanceName = $SQlinstance.Split('\\').Split('.').Split('\\').Split(',') -join '_'\r\n",
@@ -195,18 +198,16 @@
195198
],
196199
"metadata": {
197200
"azdata_cell_guid": "9fdad48b-965e-45ed-81a4-d999699dfa88",
198-
"tags": [
199-
"hide_input"
200-
]
201+
"tags": []
201202
},
202203
"outputs": [
203204
{
204-
"output_type": "stream",
205205
"name": "stdout",
206-
"text": "Processing SQL2017N5\n FileName is c:\\temp\\\\SQL2017N5_Permssions_OneTab_2020-05-30.xlsx\n\nFinished Processing SQL2017N5\n ############### \n FileName is c:\\temp\\\\SQL2017N5_Permssions_OneTab_2020-05-30.xlsx \n ############### \n\n"
206+
"text": "Processing localhost,15592\n FileName is c:\\temp\\\\localhost_15592_Permssions_OneTab_2020-09-06.xlsx\n\nFinished Processing localhost,15592\n ############### \n FileName is c:\\temp\\\\localhost_15592_Permssions_OneTab_2020-09-06.xlsx \n ############### \n\n",
207+
"output_type": "stream"
207208
}
208209
],
209-
"execution_count": 6
210+
"execution_count": 15
210211
},
211212
{
212213
"cell_type": "markdown",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
{
2+
"metadata": {
3+
"kernelspec": {
4+
"name": ".net-powershell",
5+
"display_name": ".NET (PowerShell)"
6+
},
7+
"language_info": {
8+
"name": "PowerShell",
9+
"version": "7.0",
10+
"mimetype": "text/x-powershell",
11+
"file_extension": ".ps1",
12+
"pygments_lexer": "powershell"
13+
}
14+
},
15+
"nbformat_minor": 2,
16+
"nbformat": 4,
17+
"cells": [
18+
{
19+
"cell_type": "markdown",
20+
"source": [
21+
"# Logging scripts execution"
22+
],
23+
"metadata": {
24+
"azdata_cell_guid": "931f67fe-ac17-4d9f-9bf7-03db4feefc55"
25+
}
26+
},
27+
{
28+
"cell_type": "code",
29+
"source": [
30+
"cd C:\\Users\\mrrob\\OneDrive\\Documents\\GitHub\\JupyterNotebooks\\notebooks\\dotNETNotebooks\\Assistance\\ForM\r\n",
31+
"if(Test-Path C:\\temp\\script-Execution.log -ErrorAction SilentlyContinue){\r\n",
32+
" rm C:\\temp\\script-Execution.log -Force\r\n",
33+
"}"
34+
],
35+
"metadata": {
36+
"azdata_cell_guid": "b31a612f-e0db-4fb3-8556-8489c72c6337",
37+
"tags": []
38+
},
39+
"outputs": [],
40+
"execution_count": 29
41+
},
42+
{
43+
"cell_type": "code",
44+
"source": [
45+
"ipmo .\\scripts\\beardfunction.psm1\r\n",
46+
"& .\\scripts\\CallingScript.ps1\r\n",
47+
"\r\n",
48+
"gc C:\\temp\\script-Execution.log"
49+
],
50+
"metadata": {
51+
"azdata_cell_guid": "6e6dc8da-2412-414e-be21-4be140bb2ee1",
52+
"tags": []
53+
},
54+
"outputs": [],
55+
"execution_count": 31
56+
},
57+
{
58+
"cell_type": "code",
59+
"source": [
60+
"$PSScriptRoot"
61+
],
62+
"metadata": {
63+
"azdata_cell_guid": "057cb2bf-c8f8-463f-973a-24633fc84ae8"
64+
},
65+
"outputs": [
66+
{
67+
"output_type": "stream",
68+
"name": "stdout",
69+
"text": "\r\n"
70+
}
71+
],
72+
"execution_count": 14
73+
}
74+
]
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
try {
2+
3+
[string]$CallingScriptName = "CallingScript" #Myinvocation doesnt work in notebooks
4+
[string]$CallingScriptPath = 'pathto script'
5+
Write-Information "Begin script execution of $CallingScriptName at $CallingScriptPath." -InformationVariable 'LogMessage' | Write-TheLog -ScriptName $CallingScriptName -LogMessage $LogMessage
6+
Write-TheExecutionInfo -ErrorAction 'Stop'
7+
}
8+
catch {
9+
Throw
10+
}
11+
$ActivityLogScriptsToProcess = @('.\scripts\Script1.ps1','.\scripts\Script2.ps1')
12+
[string]$TypeOfJob = 'Log'
13+
14+
try {
15+
Write-Information "Start processing $TypeOfJob scripts." -IV 'LogMessage' | Write-TheLog
16+
17+
$CountOfScripts = $ActivityLogScriptsToProcess.Count
18+
19+
if ($CountOfScripts -gt 0) {
20+
ForEach ($Script in $ActivityLogScriptsToProcess) {
21+
Write-Information "Start execution of $Script." -IV 'LogMessage' | Write-TheLog
22+
& $Script
23+
Write-Information "End execution of $Script." -IV 'LogMessage' | Write-TheLog
24+
}
25+
}
26+
}
27+
catch {
28+
Throw
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
try {
2+
[string]$CallingScriptName = $MyInvocation.MyCommand.Name #Detect current script name for logging
3+
[string]$CallingScriptPath = $PSScriptRoot #Detect current script path for logging
4+
Write-Information "Begin script execution of $CallingScriptName at $CallingScriptPath." -IV 'LogMessage' | Write-theLog
5+
Write-TheExecutionInfo -ErrorAction 'Stop'
6+
}
7+
catch {
8+
Throw
9+
}
10+
11+
if (Test-Path -Path .\scripts\Script2.ps1) {
12+
13+
Write-Information "File Script2 detected." -IV 'LogMessage' | Write-TheLog
14+
}
15+
else{
16+
Write-Information "File Script2 not detected." -IV 'LogMessage' | Write-TheLog
17+
}
18+
if (Test-Path -Path .\scripts\Script3.ps1) {
19+
20+
Write-Information "File Script3 detected." -IV 'LogMessage' | Write-TheLog
21+
}
22+
else{
23+
Write-Information "File Script3 not detected." -IV 'LogMessage' | Write-TheLog
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
try {
2+
[string]$CallingScriptName = $MyInvocation.MyCommand.Name #Detect current script name for logging
3+
[string]$CallingScriptPath = $PSScriptRoot #Detect current script path for logging
4+
Write-Information "Begin script execution of $CallingScriptName at $CallingScriptPath." -IV 'LogMessage' | Write-theLog
5+
Write-TheExecutionInfo -ErrorAction 'Stop'
6+
}
7+
catch {
8+
Throw
9+
}
10+
11+
if (Test-Path -Path .\scripts\Script1.ps1) {
12+
13+
Write-Information "File Script1 detected." -IV 'LogMessage' | Write-TheLog
14+
}
15+
else{
16+
Write-Information "File Script1 not detected." -IV 'LogMessage' | Write-TheLog
17+
}
18+
if (Test-Path -Path .\scripts\Script3.ps1) {
19+
20+
Write-Information "File Script3 detected." -IV 'LogMessage' | Write-TheLog
21+
}
22+
else{
23+
Write-Information "File Script3 not detected." -IV 'LogMessage' | Write-TheLog
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
function Write-TheLog {
2+
3+
[CmdletBinding()]
4+
param (
5+
[Parameter()]
6+
[ValidateNotNullOrEmpty()]
7+
[string]$ScriptName = $CallingScriptName, #ScriptName from calling script
8+
9+
[Parameter()]
10+
[ValidateNotNullOrEmpty()]
11+
[ValidateSet("Information", "Error", "Warning")]
12+
[string]$LogLevel = 'Information',
13+
14+
[Parameter()]
15+
[ValidateNotNullOrEmpty()]
16+
[string]$LogFileLocation = 'C:\temp\script-Execution.log' ,
17+
18+
[Parameter()]
19+
[ValidateNotNullOrEmpty()]
20+
[string[]]$LogMessage
21+
)
22+
[PSCustomObject]@{
23+
ScriptName = $ScriptName
24+
LogDateTimeUTC = ([datetime]::Now.ToUniversalTime()).ToString("MM/dd/yyyy HH:mm")
25+
LogLevel = $LogLevel
26+
LogMessage = $LogMessage[0] #Appends one message at a time based on messages in the calling script
27+
} | Export-Csv -Path $LogFileLocation -Append -NoTypeInformation
28+
}
29+
30+
function Write-TheExecutionInfo {
31+
32+
[CmdletBinding()]
33+
param ()
34+
[PSCustomObject]$LogData = Read-TheExecutionInfo
35+
[string]$BeginLogMsg = 'User: ' + $LogData.ExecutionUser + '. Machine: ' + $LogData.ExecutionComputer + '. Host: ' + $LogData.ExecutionHost + '. Process ID: ' + $LogData.ProcessID + '.'
36+
Write-Information "$BeginLogMsg" -InformationVariable 'LogMessage' | Write-TheLog -ScriptName $CallingScriptName -LogMessage $LogMessage
37+
}
38+
39+
Function Read-TheExecutionInfo {
40+
41+
[CmdletBinding()]
42+
param()
43+
[PSCustomObject]@{
44+
RuntimeUTC = ([datetime]::Now.ToUniversalTime())
45+
ExecutionUser = "$($env:USERDOMAIN)\$($env:USERNAME)"
46+
ExecutionComputer = "$env:COMPUTERNAME"
47+
ExecutionHost = "$($host.Name)"
48+
ProcessID = "$pid"
49+
PowerShellVersion = "$($PSVersionTable.PSVersion)"
50+
}
51+
}

0 commit comments

Comments
 (0)