Solved

Error calling credentials in Powershell Script

Posted on 2011-09-12
2
502 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 70

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

In-place Upgrading Dirsync to Azure AD Connect
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…
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…

707 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