Invoking a deployed SCCM package through powershell - powershell

I've been struggling to figure out a way to invoke a package that's already deployed out to a client in SCCM, using Powershell.
I'm able to run a Application using WMI, but it only works with applications, not packages.
$Application = (Get-CimInstance -ClassName CCM_Application -Namespace "root\ccm\clientSDK" -ComputerName $Computername | Where-Object {$_.Name -like $AppName})
$Args = #{EnforcePreference = [UINT32] 0
Id = "$($Application.id)"
IsMachineTarget = $Application.IsMachineTarget
IsRebootIfNeeded = $False
Priority = 'High'
Revision = "$($Application.Revision)" }
Invoke-CimMethod -Namespace "root\ccm\clientSDK" -ClassName CCM_Application -ComputerName $Computername -MethodName $Method -Arguments $Args
Anyways, using the CCM_Application class, i'm able to list out all of the applications deployed to that workstation, and I can install/uninstall, it just doesn't show any packages. I found the CCM_Program / CCM_ProgramManager class, but it doesn't return any packages/task sequences. Has anyone else been able to achieve this? or is it not possible through Powershell? I found some .dll that someone wrote in C# which seems to work with applications/packages, but I'd like to not load any dependencies if possible.

Related

Powershell to query specific Service in enterprise?

Good day good people.
Would someone please help me out I am trying to PS our enterprise servers to find all assets with a particular service on it and having no luck.
I tried
$servicename = "SERVICE_NAME"
$list = get-content "c:\security\comp_list.txt"
foreach ($server in $list) {
if (Get-Service $servicename -computername $server -ErrorAction 'SilentlyContinue'){
Write-Host "$servicename exists on $server
}
Any suggestions would be greatly appreciated. I'm still fairly new to PS.
My accepted answer to why Get-Service -ComputerName fails can be found in the link below and the resolution to the issue.
Powershell Results to Slack via Webhook question - Remote Server results
Summary
Differences between Windows PowerShell 5.1 and PowerShell 7.x
Please Note In Windows 7.2 the Get-Service command made use of DCOM and such functionality like '-ComputerName' is removed.
https://learn.microsoft.com/en-us/powershell/scripting/whats-new/differences-from-windows-powershell?view=powershell-7#remove--computername-from--service-cmdlets-5090
$server = $env:Computername
Invoke-Command -Computername $server -Scriptblock {Get-Service | where status -eq 'started;}
Get-Service cmdlet
The cmdlet gets the members, the properties and methods, of objects
get-service | get-member | sort Name
Names of services that start / contain 'App'
$Results = Get-Service -Name App | Select Name

PowerShell Invoke-Command Speed (win32_product)

I wrote a short script to uninstall a program on multiple computers (from a text doc of hostnames). The script is working as expected but is taking about 2-3 minutes per host. Is there a way to perform this on all the machines simultaneously?
Here's my script.
$computers = Get-Content C:\Computernames.txt
foreach($Computer in $computers){
Invoke-Command -ComputerName $Computer -ScriptBlock{
$application = Get-WmiObject -Class Win32_Product -Filter "Name LIKE '%Appname%'"
#uninstall the app if it exists
if($application){
$application.Uninstall()
Write-Output "Application uninstalled successfully.."
}
else{
Write-Output "Application not found.."
}
}
}
Can I do Invoke-Command -ComputerName $Computers and do all machines simultaneously to avoid looping through?
As suggested, using $Computers worked successfully. I was able to get rid of my loop and speed the script up tremendously.
Here's the updated script - thanks for letting me know it supports arrays.
#list of computers
$computers = Get-Content C:\Computernames.txt
Invoke-Command -ComputerName $computers -ScriptBlock{
$application = Get-WmiObject -Class Win32_Product -Filter "Name LIKE '%appname%'"
if($application){
$application.Uninstall()
Write-Output "Successful uninstall on $env:COMPUTERNAME "
}
else{
Write-Output "Application not found on $env:COMPUTERNAME"
}
}
The win32_product class is notoriously slow, because it verifies every msi whenever it's used. I assume appname is replaced with a specific name. You can use the little known get-package and uninstall-package in powershell 5.1, but only with msi providers:
get-package appname | uninstall-package

How to find the version of Trend from the Registry of a specific PC?

I was wondering if there is a way to find a registry value of a specific computer. The only way I could find is entering a pssession and then exiting.
$Computer = Read-Host "Enter the PC Name: "
$connection=test-connection -ComputerName $Computer -Quiet
if($connection -eq $True) {
Enter-PSSession $Computer
$TrendServer= Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\TrendMicro\PC-cillinNTCorp \CurrentVersion | Select Server
write-output $TrendServer
if($TrendServer -ne $null){
Exit-PSSession
}
} else{Write-Output "Computer is not available. Please check Lan Sweeper "}
If it is installed using Windows Installer, you can use WMI, though this class is known to be quite slow:
Get-CimInstance -Query "SELECT * FROM Win32_Product WHERE Name = 'TrendMicro'" `
-ComputerName $computer
Change the name from 'TrendMicro' to whatever it actually is (I don't have it installed to check), and for older versions of PowerShell, use Get-WmiObject instead of Get-CimInstance.
Get more information here: Working with Software Installations

SCCM Device Collection Query: Find all clients in intranet

I'm trying to create a WMI query for a device collection to find all computers that are currently outside our intranet. I can do this in Powershell by executing:
(Get-WmiObject -namespace root\ccm -query "select InInternet from ClientInfo").InInternet
But I cannot find the appropriate query in SCCM.
In configuration manager on the client you are able to see the "Connection Type" and whether or not it's currently Intranet or Internet.
Does anyone know if this is possible in an SCCM query?
AFAIK SCCM doesn't collect Connection type, probably because it changes too often (or at least can do). The only server-side query I can think of is to check if the last MP was one of the internet-enabled MPs. Ex:
SELECT * FROM SMS_R_System WHERE ResourceID IN ( SELECT ResourceID FROM SMS_G_System_CH_ClientSummary WHERE LastMPServerName IN ('InternetEnabledMP.DMZ.contoso.local','MySecondInternetEnabledMP.DMZ.contoso.local'))"
if (Get-WmiObject -namespace root\ccm -query "select InInternet from ClientInfo").InInternet can return the correct data, you should still be able to get all result from clients one by one by running command on a remote machine using -computername property:
Import-Module 'C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1'
cd cts:
$devices = (Get-CMDevice -CollectionName "All Desktop and Server Clients").name
Foreach ($device in $devices)
{
if(Test-Connection -ComputerName $device -Count 1 -Quiet)
{
$InInternet = (Get-WmiObject -ComputerName $device -Namespace root\ccm -Query 'select InInternet from ClientInfo').InInternet
$properties = #{'name' = $device; 'IsInternet' = $InInternet}
$Object = New-Object -TypeName PSObject -Property $properties
Write-Output $Object
}else{
Write-Warning "Try connection to $device failed!"
}
}
The script is not a complete script because it did not catch exceptions when trying to connect to target machine to get property. But it should be able to tell what I mean here and should be able to work. You may need to run script under admin permission

Restarting an app pool using powershell Exception

I am trying to restart an application pool remotely using powershell.
net use $ToPath $pass /USER:$usr
$appPool = get-wmiobject -computername $ToServerName -namespace "root\MicrosoftIISv2" -class "IIsApplicationPool" | Where-Object {$_.Name -eq "W3SVC/APPPOOLS/$appPoolName"} -Authentication PacketPrivacy
#(Get-WmiObject -Query "SELECT * FROM IIsApplicationPool WHERE Name = 'W3SVC/AppPools/$appPoolName'" -Namespace 'root\MicrosoftIISv2').Recycle()
$appPool.Recycle()
net use $ToPath /delete
I basically use the same command that I use to move files remotely, where I set up a net user. I get a Get-WMI exception
I wanted to make sure that this question was answered for those that come after me. It turns out that I was piping the -Authentication PacketPrivacy parameter to the wrong command