Tuesday, November 19, 2013

Script for adding a domain user account to remote local admin group

Below is a script to make a domain user  admin of a local computer

1) The Computer on which the user needs to be added should have a Static IP
2) The user executing the script should have domain admin rights

------------------------------------------------------------------------------------------------------------
### Domain (edit with your domain)
$Domain = "xxx.com"

### Get machine hostname
$Computer = Read-Host "Enter machinename to add user to local admins"

### Get User account in samaccountname format
$UserName = Read-Host "Enter username to add to local admin group of $Computer"

# Bind to the local Administrators group on the computer.
$Group = [ADSI]"WinNT://$Computer/Administrators,group"

# Bind to the domain user.
$User = [ADSI]"WinNT://$Domain/$UserName,user"

# Add the domain user to the group.
$Group.Add($User.Path)
*****

------------------------------------------------------------------------------------------------------------

PowerShell Script for finding pdf files in multiple computers

These day's I started working on Powershell, after a quite bit of time I found that using powershell was fun and easy.I have been assigned to get a list of pdf files from multiple computers over the network.After struggling I got it, Below is the script which collects the list of all pdf's.The result is exported to a CSV file.

--------------------------------------------------------------------------------------------------------
$Computers = Get-Content -Path "Give the Path where the list of computers are,this can be in a txt file"
Foreach ($Computer in $Computers){
$PdfFiles = Get-Wmiobject -namespace "root\CIMV2" -computername $Computer -Query "Select * from CIM_DataFile Where Extension = 'pdf'"
$output = @()

foreach ($file in $PdfFiles){
[bool]$firstOutput = $true
if($File.FileName){
$pdfinfo = "" | select Computer, Path, Owner, FileSize, LastModified, LastAccessed
$filepath = $file.description
$query = "ASSOCIATORS OF {Win32_LogicalFileSecuritySetting=`'$filepath`'} WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner"
$Owner = @(Get-Wmiobject -namespace "root\CIMV2" -computername $Computer -Query $query)
$Owner = $Owner[0]
$pdfinfo.Computer = $Computer
$pdfinfo.Path = $filepath
$pdfinfo.FileSize = $file.FileSize/1KB
$pdfinfo.Owner = "$($Owner.ReferencedDomainName)\$($Owner.AccountName)"
$pdfinfo.LastModified = [System.Management.ManagementDateTimeConverter]::ToDateTime($($file.LastModified))
$pdfinfo.LastAccessed = [System.Management.ManagementDateTimeConverter]::ToDateTime($($file.LastAccessed))
$output += $pdfinfo
$output | Export-Csv -Append "x:\report -$((Get-Date -uformat %Y%m%d).ToString()).csv" -NoClobber -NoTypeInformation
}
-----------------------------------------------------------------------------------------------------------
In the last line export to CSV in place of x give the drive where you want to save the report