This is a guest post Mr. Troy Collins
<#
This script will create a local user account on a remote system and added it to the Administrators group..
Writen by Troy Collin
#>
############################## ###
$pingerror = ".\nopingable.txt"
$already = ".\already.exists.txt"
$error = ".\error.txt"
$created = ".\created.txt"
$computers = Get-Content serverslist.txt
$Username = 'natadm'
$Password = Read-Host -Prompt "Enter password for $Username" -AsSecureString
############################## ####
########### Creating Report Directory #############
$FileExists = Test-Path "./Reporting"
If ($FileExists -eq $True) {
Write-Host "Reporting Directory already here moving on.. " -ForegroundColor Green
} else {
if ($FileExists -eq $false) {
Write-Host "Making Directory Called Reporting.." -ForegroundColor Red
New-Item ./reporting -Type directory
}
}
############ end of Reporting #####################
foreach ($computer in $computers)
{
if (test-Connection -ComputerName $computer -Count 2 -Quiet ) {
write-Host "$computer is alive and Pinging " -ForegroundColor Green
Try {
$checkuser = [adsi]::Exists("WinNT://$ computer/$Username")
if($checkuser -NotContains $Username)
{
Write-host "creating user account and settting password on $computer"
$ADSIComp = [adsi]"WinNT://$computer"
$NewUser = $ADSIComp.Create('User',$ Username)
#Create password
$BSTR = [system.runtime. interopservices.marshal]:: SecureStringToBSTR($Password)
$_password = [system.runtime. interopservices.marshal]:: PtrToStringAuto($BSTR)
#Set password on account
$NewUser.SetPassword(($_ password))
$NewUser.SetInfo()
Write-host "Adding new Account to Local Adminitrators on $computer"
$AdminGroup = [ADSI]"WinNT://$computer/ Administrators,group"
$User = [ADSI]"WinNT://$computer/$ UserName,user"
$AdminGroup.Add($User.Path)
write-Host "Account created on $computer" -foregroundcolor Green
$computer | out-file ./reporting/$created -Append
}
else
{
write-host "User $($username) already exists"
$computer | out-file ./reporting/$already -Append
}
}
Catch {
$computer | Out-File ./reporting/$error -Append
}
} else
{ Write-Host "$computer does not respond adding to error.txt" -BackgroundColor White -ForegroundColor Red
$computer | Out-File ./reporting/$pingerror -Append
}
}
No comments:
Post a Comment