Solved

Need some help with PowerShell script

Posted on 2014-09-17
6
294 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
[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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

This article outlines the process to identify and resolve account lockout in an Active Directory environment.
This article demonstrates probably the easiest way to configure domain-wide tier isolation within Active Directory. If you do not know tier isolation read https://technet.microsoft.com/en-us/windows-server-docs/security/securing-privileged-access/s…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

749 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