-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayingWith-CMManagedCode-ManagedCodeSucks.ps1
More file actions
70 lines (60 loc) · 2.67 KB
/
PlayingWith-CMManagedCode-ManagedCodeSucks.ps1
File metadata and controls
70 lines (60 loc) · 2.67 KB
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
# function Load-CMAssemblies {
# [string[]] $files = @("Microsoft.ConfigurationManagement.ManagementProvider.dll","AdminUI.WqlQueryEngine.dll")
# [string] $AdminConsoleDirectory = "F:\Program Files\Microsoft Configuration Manager\AdminConsole\bin"
# [string[]] $filesToLoad = @()
# foreach ($file in $files) {
# [string] $fullPath = join-path $AdminConsoleDirectory $file
# if (Test-Path $fullPath) {
# $filesToLoad += $fullPath
# Add-Type -AssemblyName $fullPath
# Write-Host ("Loaded assembly: $file") -ForegroundColor Green
# } else {
# Write-Host ("File not found: $file") -ForegroundColor Red
# }
# }
# #add-type -ReferencedAssemblies $filesToLoad
# }
function Load-CMAssemblies($adminConsoleDirectory) {
# Loads the CM .NET assemblies into memory.
# Requires: $AdminConsoleDirectory needs to be defined globally or at the script level.
[string[]] $filesToLoad = @("Microsoft.ConfigurationManagement.ManagementProvider.dll","AdminUI.WqlQueryEngine.dll")
Push-Location $AdminConsoleDirectory
[System.IO.Directory]::SetCurrentDirectory($AdminConsoleDirectory)
foreach($fileName in $filesToLoad)
{
$fullAssemblyName = [System.IO.Path]::Combine($AdminConsoleDirectory, $fileName)
if([System.IO.File]::Exists($fullAssemblyName ))
{
$FileLoaded = [Reflection.Assembly]::LoadFrom($fullAssemblyName )
Write-Host ([String]::Format("Loaded assemblies from {0}",$fileName )) -ForegroundColor Green
}
else
{
Write-Host ([String]::Format("File not found {0}",$fileName )) -ForegroundColor Red
}
}
Pop-Location
}
function Execute-WqlQuery($siteServerName, $query) {
#Uses ConfigManager assemblies to perform a CM WQL query.
# Requires: the name of the site server (string format), and the WQL query (string format)
# Returns: The results of the query
$returnValue = $null
$connectionManager = new-object Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine.WqlConnectionManager
if($connectionManager.Connect($siteServerName,))
{
$result = $connectionManager.QueryProcessor.ExecuteQuery($query)
foreach($i in $result.GetEnumerator())
{
$returnValue = $i
break
}
$connectionManager.Dispose()
}
$returnValue
}
[string] $CMAdminDir = "F:\Program Files\Microsoft Configuration Manager\AdminConsole\bin"
Load-CMAssemblies($CMAdminDir)
$CMSiteServer = "."
$query = "select * from SMS_TaskSequencePackage"
$results = Execute-WqlQuery ($CMSiteServer, $query)