Checking services remotely question

I’m trying to make a script that checks all of the TSM services on a server. One thing that’s confusing me is how it allows me to check a service if I use the specific service name but it won’t if I use a wildcard:

[i]PS U:\Scripts\TSM> Get-Service -name ‘TSM Client Acceptor’ -ComputerName Computer01

Status Name DisplayName


Running TSM Client Acce… TSM Client Acceptor
[/i]

As you can see, the above command works. The command below does not. I’m wondering why the command below would require elevated privileges when it’s essentially the same thing

[i]PS U:\Scripts\TSM> Get-Service -name ‘TSM’ -ComputerName Computer01

Get-Service : Cannot open Service Control Manager on computer ‘Computer01’.
This operation might require other privileges.
At line:1 char:1

  • Get-Service -name ‘TSM’ -ComputerName Computer01
  •   + CategoryInfo          : NotSpecified: (:) [Get-Service], InvalidOperationE
     xception
      + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerSh
     ell.Commands.GetServiceCommand
    

[/i]

Note:The account that I’m checking the service with has access to view/start/stop the service on every server in the domain.

try this:

Get-Service -ComputerName Computer01 | Where-Object { $_.Name.contains('TSM') }

You can also try WMI:

Get-CIMInstance Win32_Service -Filter "Name Like '*tsm*'" -Computername Server1

Watch out when using .contains it is case sensitive so if you was to put in ‘tsm’ instead of ‘TSM’ it would come back negative.

Like is probably best because then you can do wildcard on TSM

With regards to the actual question - if you did just get-service -computername Computer01

does that return anything or require more permissions also?

I made a mistake. I have AD 2 accounts. A regular LAN account and a domain admin account. I was running it under the regular LAN account which has access to view the TSM client acceptor service, but not to view all of them. Thank you to Adnan for leading me down that path.

I already have the admin credentials that the script prompts for stored in a variable from earlier in the script (I have a space checking portion to the script as well).

So my question becomes this: Can I do something like this:

get-service -name TSM -ComputerName $ComputerName -credential $Credential

Have you tried it? The help on the command indicates it accepts wild cards and as long as you have the correct permissions

[b]-Name [/b]
    Specifies the service names of services to be retrieved. Wildcards are permitted. By default, Get-Service gets all of the services on the computer.

    Required?                    false
    Position?                    1
    Default value                All services
    Accept pipeline input?       true (ByPropertyName, ByValue)
   [b] Accept wildcard characters?  true[/b]
PS C:\> Get-Service *nm*

Status   Name               DisplayName                           
------   ----               -----------                           
Running  LanmanServer       Server                                
Running  LanmanWorkstation  Workstation                           
Stopped  SNMPTRAP           SNMP Trap                             
Running  Winmgmt            Windows Management Instrumentation

That’s actually the problem. What I’m trying to do is have the script prompt for creds and use those to check the service. I was already able to get the credentials into a variable to use later, but using it to check the service is proving to be quite the challenge. I don’t think there’s a -credential option for Get-Service is there?

No, there is no -Credential parameter for Get-Service.
A solution would be to leverage Invoke-Command and its -Credential parameter, like so :

 Invoke-Command -ComputerName YouServerName -ScriptBlock {Get-Service -Name *TSM* } -Credential (Get-Credential) 

A window will pop-up to prompt you for a Username and password, then, Invoke-Command will execute whatever is inside the -ScriptBlock on the remote computer(s) specified with the -ComputerName parameter.

As far as I know, -contains is case insensitive. You mean -ccontains I think.

Both Get-WMIObject and Get-CIMInstance have ways to pass credentilals and use the Win32_Service class. Additionally, you could also just open the prompt as (RUNAS) the elevated credentials so you can use Get-Service.

nope I mean ‘contains’

$Test = “Hello”

if ( ($Test.Contains(“hello”)) -eq $true) {
$true
}
else {
$false
}

This will equate to false.

I didn’t realise this myself until I was writing a compliance script and noticed some of my results coming back negative. http://technet.microsoft.com/en-us/library/ee692804.aspx

Although having said that i just checked -contains and yes you are quite right that is case insensitive.

So there seems to be a difference between using dot notation and parameter.

However in the original question it referenced dot notation.

cheers

Istvan Szarka:

The -contains operator is case insensitive. However the OP was using the .contains() method. And it is case sensitive. And there IS a difference in its basic use. The -contains operator verifies an object is a member of an array. The .contains() method verifies a substring is present within a string.

Thanks for the explanation, I was completely unaware of the difference between the operator and the method :slight_smile:

Well, you guys are awesome. I gave it shot using Get-WmiObject with the win32_service class and it worked great.

First I tried this:

Get-WmiObject -Class win32_service -Filter "Name Like '*TSM*'" -ComputerName $Computer -Credential ($Credential)

which simply returned nothing, No errors, just blank.

So then I tried this:

Get-WmiObject -Class win32_service -ComputerName $Computer -Credential($Credential) | Where-Object { $_.Name.contains('TSM') }

and it worked no problem.

I’m learning a lot on these forums and as a beginner powershell user, I’m very appreciative. Thanks guys!

Rob

Oops, WMI filters use WQL and the wildcard character is percent (%), not asterick (*). So, it’s actually:

Get-WmiObject -Class win32_service -Filter "Name Like '%TSM%'" -ComputerName $Computer -Credential ($Credential)

The difference in using the filter in WMI is you are only returning records that meet that search. Doing a Where after is returning all services and then you are filtering on all returned results. To make it even more efficient, you should only return only the properties that you are going to use:

Get-WmiObject -Class win32_service -Filter "Name Like '%TSM%'" -Property Name, State -ComputerName $Computer -Credential ($Credential)

If these servers are 2008 or higher, you should try using Get-CIMInstance which use WSMAN over DCOM protocol and typically have better performance.

Some are still 2003 so once we get them upgraded I’ll probably start using Get-CIMInstance, in the mean time i’ll start using the correct wild card haha. Thanks again :slight_smile: