Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Error calling credentials in Powershell Script

Posted on 2011-09-12
2
Medium Priority
?
505 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

The core idea of this article is to make you acquainted with the best way in which you can export Exchange mailbox to PST format.
The main intent of this article is to make you aware of ‘Exchange fail to mount’ error, its effects, causes, and solution.
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 …
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…

618 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