PowerShell script to detect Active instances of BizTalk send port - powershell

I have a PowerShell script to detect active instances of send port running in BizTalk. I am struggling with syntax to query the MSBTS_ServiceInstance to find the active instances of this send port.
Can anyone help me as to how to subsitute the name of the send port in the where clause or the filter clause please ?
[ARRAY]$active = get-wmiobject MSBTS_ServiceInstance -namespace 'root\MicrosoftBizTalkServer' -filter '(ServiceStatus = 2) and how to search by name' -ErrorAction SilentlyContinue
Write-Host "Active Instances:" $active.Count

This should do the trick
[ARRAY]$active = get-wmiobject MSBTS_ServiceInstance -namespace 'root\MicrosoftBizTalkServer' -filter {ServiceStatus = 2 and ServiceName = "port name"} -ErrorAction SilentlyContinue
Please see Class definition and PowerShell syntax
However I personaly prefer using Microsoft.BizTalk.Operations.dll with PowerShell to perform this kind of queries.

Related

PowerShell check for service existance on remote machine - reliably?

I'm currently trying to check a bunch of remote machines for the existence of certain services. Currently that process is pretty slow (as in > 1 hour for a few hundred machines) which isn't much of a problem but also unreliable (which is a problem).
Some of those machines might be blocking access, turned off or might in some other way limit access to query for the services.
The current approach is to check with Get-Service and as it has proven to be unreliable to also check with Get-WmiObject as well as Get-CimInstance.
The problem is that they don't support timeouts so if a server is just unresponsive it will take quite a bit of time to get a response and because all three options might work or not depending on the machine there really isn't an option to only run one. As one of them might succeed where the others failed I need to check the results of all three calls.
Would anyone have an idea on how to do a sort of preflight check to figure out which command might work or if there is a different approach to checking for the existence of a service that might be more reliable?
The current approach looks akin to this (simplified):
$error.Clear()
$serviceExists = $false
$server = "localhost"
$serviceQuery = Get-Service -ErrorAction SilentlyContinue -Name WinRM -ComputerName $server
$wmiQuery = Get-WmiObject -ErrorAction SilentlyContinue -Class Win32_Service -Filter "Name = 'WinRM'" -ComputerName $server
$cimQuery = Get-CimInstance -ErrorAction SilentlyContinue -ClassName Win32_Service -Filter "Name = 'WinRM'" -ComputerName $server
if ($error.Count -ge 1) {
Write-Output "An error occurred while check for the service existence on $server"
}
if (
($serviceQuery | Where Name -eq 'WinRM') -ne $null -or
($wmiQuery | Where Name -eq 'WinRM') -ne $null -or
($cimQuery | Where Name -eq 'WinRM') -ne $null
) {
$serviceExists = $true
}
As far as I can tell it might be that Get-Service internally uses (the same approach as) Get-WmiObject and Get-CimInstance but I wasn't able to find documentation on that. The machine currently use PowerShell 5.x, PowerShell remoteing might or might not be configured on the servers that are being queried for the service existence.
Edit: Get-Service is unreliable because it might not indicate that there was an issue and just return $null without indicating that there actually was a problem. Also while it might fail one of the other options might still work.

PowerShell script to identify and stop one specific service

I try to make a PowerShell script to do the following:
I want to identify the antivirus running on my PC.
I use command to do that:
$AntiVirusProduct = Get-WmiObject -Namespace root\SecurityCenter2 -Class AntiVirusProduct
Write-Output $AntiVirusProduct.DisplayName
Here I get the antivirus name but, I don't know how to grep the antivirus name and put it to the next command.
The next command is:
Stop-Service -Force "$Antivirus Name"
Or if there is a better way to to this?
Edit
Get-WmiObject -Namespace root\SecurityCenter2 -Class AntiVirusProduct |
Select DisplayName
DisplayName
-----------
AVG Antivirus
Windows Defender
If your display name output is a list of service names you can remove the Windows Defender entry from the list with something like this:
$svc = $AntiVirusProduct.DisplayName |
Where-Object { $_ -notlike '*Windows Defender*' }
and then stop the service like this:
$svc | Stop-Service -Force

Powershell Script - Search AD and then restart service

I have created the below powershell script which looks at a local active directory, and searches for a particular string within the description field.
Once the script has found the particular computer name from the above search it starts the service in question.
$FullName = "*Username*"
$LookUp = Get-ADComputer -Filter {Description -Like $FullName} -Properties Description | Select DNSHostName
$ComputerName = $LookUp
$ServiceName = "Service Name"
Get-Service -Name $ServiceName -ComputerName $ComputerName | Set-Service -Status Running
The issue I am having is that the Powershell script returns a 'service not found' message when I know (have manually checked) that the service exists?
Any help would be greatly appreciated.
Thanks
Does the service start if you run only that line on the target machine?
ADDITION----------------------
I think you need to do this:
$ComputerName = $LookUp.DNSHostname

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

Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x80070 6BA

I have what should be a simple script that will connect to all the servers in a domain and build a table of all the services running on each server. However, when I try to automate the script to grab all the servers in a foreach loop I get an RPC error. If the $name variable is replaced with the server DNS name everything works as expected. I've checked the firewall and DCOM services on my system (win7) and the servers (2000 - 2008R2) and these are all enabled or disabled appropriately. So, I'm thinking something in the script is broke. I'm still learning powershell, so any tips are appreciated.
Here is the script so far.
$servernames = get-adobject -Filter 'ObjectClass -eq "Computer" ' -Searchbase "OU=Servers,DC=E,DC=BENEFIS,DC=ORG"
foreach ($name in $servernames) {
Get-WMIObject win32_service -computername $name -Property SystemName,Name,StartName,StartMode |
Format-table SystemName, Name, Startname >c:\serverservices.txt }
Each object you get back have a name property so you need to pass its value to the ComputerName parameter. In addition, to get computer object use the Get-ADComputer cmdlet, you also need to specify the Append switch when you export to the file otherwise content will be overwritten and what you'll see finally is the output of the last computer only.
$servernames = Get-ADComputer -SearchBase "OU=Servers,DC=E,DC=BENEFIS,DC=ORG" -Filter *
foreach ($name in $servernames)
{
Get-WMIObject win32_service -computername $name.Name -Property SystemName,Name,StartName,StartMode |
Format-table SystemName, Name, Startname | Out-File c:\serverservices.txt -Append
}