Saturday, 7 August 2021

Custom WQL query to include or exclude

Use the following example queries and replace the example collection ID XYZ0003F with the ID of the collection you want to include or exclude.


Include:

Select * from SMS_R_System where SMS_R_System.ResourceId in (select ResourceID from SMS_CM_RES_COLL_XYZ0003F)

Exclude:

Select * from SMS_R_System where SMS_R_System.ResourceId not in (select ResourceID from SMS_CM_RES_COLL_XYZ0003F)

Share:

SQL Querrey to monitor collection evaluation

Monitor collection evaluation

SELECT [t2].[CollectionName], [t2].[SiteID], [t2].[value] AS [Seconds], [t2].[LastIncrementalRefreshTime], [t2].[IncrementalMemberChanges] AS [IncChanges], [t2].[LastMemberChangeTime] AS [MemberChangeTime]
FROM (
    SELECT [t0].[CollectionName], [t0].[SiteID], DATEDIFF(Millisecond, [t1].[IncrementalEvaluationStartTime], [t1].[LastIncrementalRefreshTime]) * 0.001 AS [value], [t1].[LastIncrementalRefreshTime], [t1].[IncrementalMemberChanges], [t1].[LastMemberChangeTime], [t1].[IncrementalEvaluationStartTime], v1.[RefreshType]
    FROM [dbo].[Collections_G] AS [t0]
    INNER JOIN [dbo].[Collections_L] AS [t1] ON [t0].[CollectionID] = [t1].[CollectionID]
    inner join v_Collection v1 on [t0].[siteid] = v1.CollectionID
    ) AS [t2]
WHERE ([t2].[IncrementalEvaluationStartTime] IS NOT NULL) AND ([t2].[LastIncrementalRefreshTime] IS NOT NULL) and (refreshtype='4' or refreshtype='6')
ORDER BY [t2].[value] DESC
Share:

Powershell Application detection method example

Powershell detection method example
Modify the Powershell script

Modify the below script to suit your needs. The example below will look at strings in 3 different files. You will need to make the following modifications:

  • String variables
  • String<number>Location variables

#Define strings and their location. Also include the filename.

$String1 = ""
$String1Location = ""

$String2 = ""
$String2Location = ""

$String3 = ""
$String3Location = ""

# Detect presence of String1 in String1 Location
try {
    $String1Exists = Get-Content $String1Location -ErrorAction Stop
}
catch {
}

# Detect presence of String2 in String2 Location
try {
    $String2Exists = Get-Content $String2Location -ErrorAction Stop
}
catch {
}

# Detect presence of String3 in String3 Location
try {
    $String3Exists = Get-Content $String3Location -ErrorAction Stop
}
catch {
}

if (($String1Exists -match $String1) -and ($String2Exists -match $String2) -and ($String3Exists -match $String3)) {
    Write-Host "Installed"
}
else {
}
Share:

Monday, 26 July 2021

Using PowerShell script to extract the status messages for SMS provider, Site and client in Configuration Manager




<#
.SYNOPSIS
	This script generate the status messages for sms provider, site server and client.
Author: Eswar Koneti
Date:15-Nov-2019
#>

<#param( 
    [Parameter(Mandatory=$True)] 
    [string]$stringPathToDLL, 
    [Parameter(Mandatory=$True)] 
    [string]$stringOutputCSV 
) #>

$scriptpath = $MyInvocation.MyCommand.Path
$dir = Split-Path $scriptpath
$date = (get-date -f dd-MM-yyyy-HHmmss)

$Dllfiles=Get-ChildItem -Path "$dir\*.dll" -Recurse
foreach ($file in $Dllfiles)
{

    $dir=$File.Directory.FullName
    $dll=$file.name
    $strOutputCSV =  $dir+"\"+$dll+".csv"
    $stringPathToDLL = $dir+"\"+$dll
 
#Start Invoke Code 
$sigFormatMessage = @' 
[DllImport("kernel32.dll")] 
public static extern uint FormatMessage(uint flags, IntPtr source, uint messageId, uint langId, StringBuilder buffer, uint size, string[] arguments); 
'@ 
 
$sigGetModuleHandle = @' 
[DllImport("kernel32.dll")] 
public static extern IntPtr GetModuleHandle(string lpModuleName);
'@ 
 
$sigLoadLibrary = @' 
[DllImport("kernel32.dll")] 
public static extern IntPtr LoadLibrary(string lpFileName); 
'@ 
 
$Win32FormatMessage = Add-Type -MemberDefinition $sigFormatMessage -name "Win32FormatMessage" -namespace Win32Functions -PassThru -Using System.Text 
$Win32GetModuleHandle = Add-Type -MemberDefinition $sigGetModuleHandle -name "Win32GetModuleHandle" -namespace Win32Functions -PassThru -Using System.Text 
$Win32LoadLibrary = Add-Type -MemberDefinition $sigLoadLibrary -name "Win32LoadLibrary" -namespace Win32Functions -PassThru -Using System.Text 
#End Invoke Code 
 
$sizeOfBuffer = [int]16384 
$stringArrayInput = {"%1","%2","%3","%4","%5", "%6", "%7", "%8", "%9"} 
$flags = 0x00000800 -bor 0x00000200  
$stringOutput = New-Object System.Text.StringBuilder $sizeOfBuffer 
$colMessages = @() 

#Load Status Message Lookup DLL into memory and get pointer to memory 
$ptrFoo = $Win32LoadLibrary::LoadLibrary($stringPathToDLL.ToString()) 
$ptrModule = $Win32GetModuleHandle::GetModuleHandle($stringPathToDLL.ToString()) 
 
#Find Informational Status Messages 
for ($iMessageID = 1; $iMessageID -ile 99999; $iMessageID++) 
{ 
    $result = $Win32FormatMessage::FormatMessage($flags, $ptrModule, 1073741824 -bor $iMessageID, 0, $stringOutput, $sizeOfBuffer, $stringArrayInput) 
     
    if( $result -gt 0) 
    { 
        $objMessage = New-Object System.Object 
        $objMessage | Add-Member -type NoteProperty -name MessageID -value $iMessageID 
        $objMessage | Add-Member -type NoteProperty -name MessageString -value $stringOutput.ToString().Replace("%11","").Replace("%12","").Replace("%3%4%5%6%7%8%9%10","") 
        $objMessage | Add-Member -type NoteProperty -name Severity -value "Informational" 
        $colMessages += $objMessage 
        #$iMessageID 
        #$stringOutput.ToString() 
    } 
     
    #$previousString = $stringOutput.ToString() 
} 
 
#Find Warning Status Messages 
for ($iMessageID = 1; $iMessageID -ile 99999; $iMessageID++) 
{ 
    $result = $Win32FormatMessage::FormatMessage($flags, $ptrModule, 2147483648 -bor $iMessageID, 0, $stringOutput, $sizeOfBuffer, $stringArrayInput) 
     
    if( $result -gt 0) 
    { 
        $objMessage = New-Object System.Object 
        $objMessage | Add-Member -type NoteProperty -name MessageID -value $iMessageID 
        $objMessage | Add-Member -type NoteProperty -name MessageString -value $stringOutput.ToString().Replace("%11","").Replace("%12","").Replace("%3%4%5%6%7%8%9%10","") 
        $objMessage | Add-Member -type NoteProperty -name Severity -value "Warning" 
        $colMessages += $objMessage 
        #$iMessageID 
        #$stringOutput.ToString() 
    } 
 
    #$previousString = $stringOutput.ToString() 
} 
 
#Find Error Status Messages 
for ($iMessageID = 1; $iMessageID -ile 99999; $iMessageID++) 
{ 
    $result = $Win32FormatMessage::FormatMessage($flags, $ptrModule, 3221225472 -bor $iMessageID, 0, $stringOutput, $sizeOfBuffer, $stringArrayInput) 
     
    if( $result -gt 0) 
    { 
        $objMessage = New-Object System.Object 
        $objMessage | Add-Member -type NoteProperty -name MessageID -value $iMessageID 
        $objMessage | Add-Member -type NoteProperty -name MessageString -value $stringOutput.ToString().Replace("%11","").Replace("%12","").Replace("%3%4%5%6%7%8%9%10","") 
        $objMessage | Add-Member -type NoteProperty -name Severity -value "Error" 
        $colMessages += $objMessage 
        #$iMessageID 
        #$stringOutput.ToString() 
    } 
     
    #$previousString = $stringOutput.ToString()
} 
 
$colMessages | Export-CSV -path $strOutputCSV -NoTypeInformation
}

$csvs = Get-ChildItem "$dir\*.csv"
$y=$csvs.Count
Write-Host "Detected the following CSV files: ($y)"
foreach ($csv in $csvs)
{
Write-Host " "$csv.Name
}
$outputfilename = "StatusMessages-$date.xlsx" #creates file name with date/username
Write-Host Creating: $outputfilename
$excelapp = new-object -comobject Excel.Application
$excelapp.sheetsInNewWorkbook = $csvs.Count
$xlsx = $excelapp.Workbooks.Add()
$sheet=1

foreach ($csv in $csvs)
{
$row=1
$column=1
$worksheet = $xlsx.Worksheets.Item($sheet)
$worksheet.Name = $csv.Name
$file = (Get-Content $csv)
foreach($line in $file)
{
$linecontents=$line -split ',(?!\s*\w+")'
foreach($cell in $linecontents)
{
$worksheet.Cells.Item($row,$column) = $cell
$column++
}
$column=1
$row++
}
$sheet++
}
$output = $dir + "\" + $outputfilename
$xlsx.SaveAs($output)
$excelapp.quit()
cd \ #returns to drive root
Remove-Item "$dir\*.csv" -Recurse -Force -ErrorAction SilentlyContinue

http://eskonr.com/2020/11/using-powershell-script-to-extract-the-status-messages-for-sms-provider-site-and-client-in-configuration-manager/
Share:

Using Scripts to trigger software updates remotely from the software center

The following is the PowerShell script which will check for the windows updates (it can be Microsoft or 3rd party), and trigger the installation. Doing this action, will ignore the maintenance window ONLY (if you have any) and follow the reboot schedule as per the assignment.

$MissingUpdates = Get-WmiObject -Class CCM_SoftwareUpdate -Filter ComplianceState=0 -Namespace root\CCM\ClientSDK
$MissingUpdatesReformatted = @($MissingUpdates | ForEach-Object {if($_.ComplianceState -eq 0){[WMI]$_.__PATH}})
if ( $MissingUpdatesReformatted)
{
$InstallReturn = Invoke-WmiMethod -ComputerName $env:computername -Class CCM_SoftwareUpdatesManager -Name InstallUpdates -ArgumentList (,$MissingUpdatesReformatted) -Namespace root\ccm\clientsdk
write-host "Updates found, initiated"
}
else
{
write-host "No updates found"
}
http://eskonr.com/2021/05/using-scripts-to-trigger-software-updates-remotely-from-the-sccm-console/


Share:

VB Script to delete Software update files from SCCM client Cache folder

Create Notepad File and save it as Filename.vbs
http://eskonr.com/2012/05/vb-script-to-delete-software-update-files-from-sccm-client-cache-folder/

On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = WScript.CreateObject("WScript.Shell")
'Delete all software update caches
If objFSO.FolderExists("C:\Windows\System32\ccm\cache") Then
    objShell.Run ("cmd /c dir /b c:\windows\system32\ccm\cache\*-*-*-*-*.1.System>c:\temp\caches.txt")

else

objShell.Run ("cmd /c dir /b c:\windows\syswow64\ccm\cache\*-*-*-*-*.1.System>c:\temp\caches.txt")

End If

If objFSO.FileExists ("C:\temp\caches.txt") Then
    Set objFile = objFSO.OpenTextFile("C:\temp\caches.txt", 1)
    Do Until objFile.AtEndOfStream
        strLine = objFile.ReadLine
'Wscript.Echo strLine
dirPath = "c:\windows\system32\ccm\cache\" & strLine
'Wscript.Echo dirPath
objFSO.DeleteFolder dirPath, True
Wscript.Sleep 10
    Loop
    objFile.Close
End If
Wscript.Quit(0)

Share: