?
Solved

Error calling credentials in Powershell Script

Posted on 2011-09-12
2
Medium Priority
?
507 Views
Last Modified: 2012-08-13
I'm trying to create a Powershell script that retrieves a server name from a text file and then performs the get-service command on the server name and outputs the results to a file. The script that I am using is as follows

$computernames = Source location of the computer names
$credentials = get-credentials "domain account name"

get-service -computername $computernames $credential | where-object {$_.Status -eq "Running"} | export-csv C:\Users\Output.csv.

When I run this command I get the following error:-
 
Get-Service : Cannot find any service with service name 'System.Management.Auto
mation.PSCredential'.
At line:1 char:12
+ get-service <<<<  -computername $computernames $credential | where-object {$_
.Status -eq "Running"} | export-csv C:\Users\Output.csv

    + CategoryInfo          : ObjectNotFound: (System.Manageme...on.PSCredenti
   al:String) [Get-Service], ServiceCommandException
    + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.
   Commands.GetServiceCommand

Does anyone know where I am going wrong with this?



0
Comment
Question by:MartynLawson
2 Comments
 
LVL 71

Accepted Solution

by:
Qlemo earned 2000 total points
ID: 36524671
First, you won't know which machine the output is from.
Second, get-service does not allow for providing credentials. You can do so by using WinRM, however that requires that PowerShell 2 is running remotely, and WinRM is configured to allow connections.
$Credentials = get-credentials "domain account name"
get-content servers.txt  | % {invoke-command -computername $_  -credentials $Credentials {get-service}}

Open in new window

0
 
LVL 1

Author Closing Comment

by:MartynLawson
ID: 36579264
Thanks for that! I didn't realise that I couldn't specify credentials with certain commands.
0

Featured Post

 The Evil-ution of Network Security Threats

What are the hacks that forever changed the security industry? To answer that question, we created an exciting new eBook that takes you on a trip through hacking history. It explores the top hacks from the 80s to 2010s, why they mattered, and how the security industry responded.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Exchange administrators are always vigilant about Exchange crashes and disasters that are possible any time. It is quite essential to identify the symptoms of a possible Exchange issue and be prepared with a proper recovery plan. There are multiple…
This video discusses moving either the default database or any database to a new volume.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question