-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQL - Query in PS.ps1
99 lines (76 loc) · 2.66 KB
/
SQL - Query in PS.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<#
.NOTES
===========================================================================
Created on: 2022-04-27
Created by: Brian Thorp
===========================================================================
.Description
Demo code from somewhere online and my awful SQL code to list computers with specific applications
#>
$Query = @"
SELECT
UserData.Full_User_Name0 as 'Full User Name',
UserData.Unique_User_Name0 as 'UserName',
[CM_CM1].[dbo].[v_R_System].Netbios_Name0 as 'Computer Name',
Publisher0,
ARPDisplayName0,
ProductVersion0,
LocalPackage0,
UninstallString0,
PackageCode0,
v_GS_INSTALLED_SOFTWARE.ResourceID
FROM [CM_CM1].[dbo].[v_GS_INSTALLED_SOFTWARE]
INNER JOIN [CM_CM1].[dbo].[v_R_System]
ON v_GS_INSTALLED_SOFTWARE.ResourceID = v_R_System.ResourceID
/* Need to put in differnt user lookup for primary user */
Outer APPLY
(
SELECT top 1
v_R_User.Full_User_Name0,
v_R_User.Unique_User_Name0,
v_R_System_Valid.User_Name0,
v_R_System_Valid.Netbios_Name0
From [CM_CM1].[dbo].[v_R_System_Valid]
INNER JOIN [CM_CM1].[dbo].[v_UserMachineRelationship]
ON v_R_System_Valid.ResourceID = v_GS_INSTALLED_SOFTWARE.ResourceID
INNER JOIN [CM_CM1].[dbo].[v_R_User]
ON v_R_User.User_Name0 = v_R_System_Valid.User_Name0
/*
Select Top 1
MachineResourceName,
MachineResourceID,
UniqueUserName
From [CM_CM1].[dbo].[v_UserMachineRelationship]
Where MachineResourceID = v_GS_INSTALLED_SOFTWARE.ResourceID
*/
) As UserData
WHERE ARPDisplayName0 LIKE 'Microsoft Office 365 ProPlus%' and Publisher0 LIKE '%' and ProductVersion0 LIKE '%'
"@
$ServerName = "sccm.contoso.com"
$DatabaseName = "CM_CM1"
#Timeout parameters
$QueryTimeout = 120
$ConnectionTimeout = 30
#Action of connecting to the Database and executing the query and returning results if there were any.
$conn=New-Object System.Data.SqlClient.SQLConnection
$ConnectionString = "Server={0};Database={1};Integrated Security=True;Connect Timeout={2}" -f $ServerName,$DatabaseName,$ConnectionTimeout
$conn.ConnectionString=$ConnectionString
$conn.Open()
$cmd=New-Object system.Data.SqlClient.SqlCommand($Query,$conn)
$cmd.CommandTimeout=$QueryTimeout
$ds=New-Object system.Data.DataSet
$da=New-Object system.Data.SqlClient.SqlDataAdapter($cmd)
[void]$da.fill($ds)
$conn.Close()
$Results = $ds.Tables
$Affected = Import-CSV -Path "C:\ContosoTemp\onenote_issues.csv"
foreach ($Entry in $Results)
{
$Computer = $Entry.'Computer Name'
$Version = $Entry.'ProductVersion0'
if ($($Affected.Computer) -contains $Computer)
{
$Computer
$Version
}
}