Author Posts

July 29, 2014 at 3:14 pm

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.

July 29, 2014 at 3:57 pm

try this:

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

July 30, 2014 at 4:13 am

You can also try WMI:

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

July 30, 2014 at 11:35 am

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?

July 31, 2014 at 8:57 am

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:

[i]get-service -name *TSM* -ComputerName $ComputerName -credential $Credential[/i]

July 31, 2014 at 12:03 pm

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

August 1, 2014 at 2:00 pm

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?

August 2, 2014 at 4:52 am

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.

August 2, 2014 at 9:04 am

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

August 2, 2014 at 11:06 am

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.

August 2, 2014 at 1:53 pm

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

August 2, 2014 at 1:59 pm

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

August 2, 2014 at 4:52 pm

Istvan Szarka:

The -contains [u]operator[/u] is case insensitive. However the OP was using the .contains() [u]method[/u]. 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.

August 2, 2014 at 9:35 pm

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

August 3, 2014 at 3:44 pm

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

August 3, 2014 at 4:32 pm

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.

August 3, 2014 at 5:47 pm

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 🙂