Solved

Error calling credentials in Powershell Script

Posted on 2011-09-12
2
495 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 68

Accepted Solution

by:
Qlemo earned 500 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

We are happy to announce a brand new addition to our line of acclaimed email signature management products – CodeTwo Email Signatures for Office 365.
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
In this video we show how to create a Resource Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: Navigate to the Recipients >> Resources tab.: "Recipients" is our default selection …
In this video we show how to create an email address policy in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Mail Flow…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now