Solved

Need some help with PowerShell script

Posted on 2014-09-17
6
291 Views
Last Modified: 2014-09-17
Hi!

I found a script to read the local accounts from a server. I need to modify the script so it can do what it does on a REMOTE SERVER.

The best would be, that i fill a txt file with the server names. Then the script uses these server names to retreive the local accounts. Can someone help me with that?

Param
(
	[Parameter(Position=0,Mandatory=$false)]
	[ValidateNotNullorEmpty()]
	[Alias('cn')][String[]]$ComputerName=$Env:COMPUTERNAME,
	[Parameter(Position=1,Mandatory=$false)]
	[Alias('un')][String[]]$AccountName,
	[Parameter(Position=2,Mandatory=$false)]
	[Alias('cred')][System.Management.Automation.PsCredential]$Credential
)
	
$Obj = @()

Foreach($Computer in $ComputerName)
{
	If($Credential)
	{
		$AllLocalAccounts = Get-WmiObject -Class Win32_UserAccount -Namespace "root\cimv2" `
		-Filter "LocalAccount='$True'" -ComputerName $Computer -Credential $Credential -ErrorAction Stop
	}
	else
	{
		$AllLocalAccounts = Get-WmiObject -Class Win32_UserAccount -Namespace "root\cimv2" `
		-Filter "LocalAccount='$True'" -ComputerName $Computer -ErrorAction Stop
	}
	
	Foreach($LocalAccount in $AllLocalAccounts)
	{
		$Object = New-Object -TypeName PSObject
		
		$Object|Add-Member -MemberType NoteProperty -Name "Name" -Value $LocalAccount.Name
		$Object|Add-Member -MemberType NoteProperty -Name "Full Name" -Value $LocalAccount.FullName
		$Object|Add-Member -MemberType NoteProperty -Name "Caption" -Value $LocalAccount.Caption
      	$Object|Add-Member -MemberType NoteProperty -Name "Disabled" -Value $LocalAccount.Disabled
      	$Object|Add-Member -MemberType NoteProperty -Name "Status" -Value $LocalAccount.Status
      	$Object|Add-Member -MemberType NoteProperty -Name "LockOut" -Value $LocalAccount.LockOut
		$Object|Add-Member -MemberType NoteProperty -Name "Password Changeable" -Value $LocalAccount.PasswordChangeable
		$Object|Add-Member -MemberType NoteProperty -Name "Password Expires" -Value $LocalAccount.PasswordExpires
		$Object|Add-Member -MemberType NoteProperty -Name "Password Required" -Value $LocalAccount.PasswordRequired
		$Object|Add-Member -MemberType NoteProperty -Name "SID" -Value $LocalAccount.SID
		$Object|Add-Member -MemberType NoteProperty -Name "SID Type" -Value $LocalAccount.SIDType
		$Object|Add-Member -MemberType NoteProperty -Name "Account Type" -Value $LocalAccount.AccountType
		$Object|Add-Member -MemberType NoteProperty -Name "Domain" -Value $LocalAccount.Domain
		$Object|Add-Member -MemberType NoteProperty -Name "Description" -Value $LocalAccount.Description
		
		$Obj+=$Object
	}
	
	If($AccountName)
	{
		Foreach($Account in $AccountName)
		{
			$Obj|Where-Object{$_.Name -like "$Account"}
		}
	}
	else
	{
		$Obj
	}
}

Open in new window

0
Comment
Question by:SvenIA
  • 4
  • 2
6 Comments
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 40327730
Looks like you just need to supply a paremeter for the remote computer(s)

.\ScriptName.ps1 -cn Computer1,Computer2, etc...
0
 
LVL 67

Assisted Solution

by:sirbounty
sirbounty earned 500 total points
ID: 40327732
It 'defaults' to the local computername with this parameter definition:

[Alias('cn')][String[]]$ComputerName=$Env:COMPUTERNAME,

You can change that to whatever as the default:
[Alias('cn')][String[]]$ComputerName='Computer1'
0
 
LVL 7

Author Comment

by:SvenIA
ID: 40327773
Thanks!

And how can i get the output in for example a HTML, TXT, or CSV file?
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 67

Assisted Solution

by:sirbounty
sirbounty earned 500 total points
ID: 40327786
Html would require some further code adjustments, unless you wanted 'boring'.
Your script is producing objects for each server, so you can simply pipe the results to your output...

.\ScriptName.ps1 -cn Computer1,Computer2 | out-file c:\output.txt

or

.\ScriptName.ps1 -cn Computer1,Computer2 | export-csv c:\output.csv
0
 
LVL 7

Author Closing Comment

by:SvenIA
ID: 40327791
It's that easy! Thanks so much!
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40327802
Yep. :^)
Glad I could help - thanks for the grade.
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

809 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