Saturday, August 23, 2014

Powershell - get all host files on domain.


Purpose: This script will search AD for Window computers and attempted to connect to each one. Then it will read the contents of the host file and write them to a csv file.

Note: This will take a while to run unless someone wants to make it multithreaded.
I just wrote this so there may need to be some bug fixes but in general it seemed to work.

#Read all host files and write to CSV file


Import-Module ActiveDirectory

$PATH = "c:\temp\hostfiles.csv"


   $myCol = @()
   
$AllComputers = Get-ADComputer -Filter {OperatingSystem -Like "Windows*"} -Property * | Select -Expand Name

foreach ($Computer in $AllComputers){
$i++

Write-Progress -activity "Scanning Machine $Computer " -status "Scanned: $i of $($AllComputers.Count)" -percentComplete (($i / $AllComputers.Count)  * 100)
 Get-Content -Path "\\$Computer\c$\windows\system32\drivers\etc\hosts" |             
 where {!$_.StartsWith("#")} |            
 foreach {            
  if ($_ -ne ""){            
  $data = $_ -split " ",2            
  $Hosts = New-Object -TypeName PSObject -Property @{            
     Host = $Computer
  IPAddress = $data[0].Trim()   
  Node = $data[1].Trim()            
              
   }            
  } 
  $myCol += $Hosts
 }    
 $myCol |Select Host,Node,IPAddress| Export-Csv -Path $PATH -NoTypeInformation
}




Tuesday, August 5, 2014

Powershell: Get list of all services accounts used on Windows servers in domain.


Purpose: This script will search AD for Windows Servers that are enabled and attempted to connect to each server and get a listing of all services and the accounts used to run them.
Note: This will take a while to run unless someone wants to make it multithreaded.
I just wrote this so there may need to be some bug fixes but in general it seemed to work.
Requirements: RSAT tools WMI



Import-Module activedirectory
cls

$ServicesReport = @()

$AllServers = Get-ADComputer -Filter {(OperatingSystem -Like "Windows Server*")-and (enabled -eq "true")} -Property SamAccountName | select -expand Name

$i = 0
foreach($Server in $AllServers) {
$i++
Write-Host "Working on Server: $Server " $i " of " $AllServers.Count 

try
{
if (Test-Connection -ComputerName $Server -Quiet) {
$Services = Get-WmiObject win32_service -ComputerName $Server | select Name,
  @{N="StartupType";E={$_.StartMode}},
  @{N="ServiceAccount";E={$_.StartName}},
  @{N="SystemName";E={$_.Systemname}}
    foreach ($Service in $Services) {
       $Detail = New-Object PSObject 
    $Detail | Add-Member Noteproperty ServiceName $($Service.Name)
    $Detail | Add-Member Noteproperty StartupType $Service.StartupType
    $Detail | Add-Member Noteproperty ServiceAccount $Service.ServiceAccount
    $Detail | Add-Member Noteproperty SystemName $Service.Systemname
       $ServicesReport += $Detail
    }
 }
}
Catch
{
$Detail = New-Object PSObject 
    $Detail | Add-Member Noteproperty ServiceName "NA"
    $Detail | Add-Member Noteproperty StartupType "NA"
    $Detail | Add-Member Noteproperty ServiceAccount "Error"
    $Detail | Add-Member Noteproperty SystemName $Server
       $ServicesReport += $Detail
  
 }
  
  } 
  
$ServicesReport | Export-Csv -Path c:\temp\test.csv -NoTypeInformation


Powershell-DellBatchWarrentylookup


Purpose: This script will read serial numbers from a text file then search dells site for warranty information. The get-dellassetinfo function i found from some forum but don't remember where.


###################################
$ComputerSerials = gc "c:\temp\SerialNumbers.txt"
###################################
Function Get-DellAssetInfo([string]$ServiceTag){

 $Asset=New-WebServiceProxy -Uri 'http://xserv.dell.com/services/AssetService.asmx?WSDL' -UseDefaultCredential
 $Asset.GetAssetInformation([Guid]::NewGuid(),'AssetService',$ServiceTag);
}

$myCol = @()
foreach ($ComputerSerial in $ComputerSerials) {
 $results=Get-DellAssetInfo $ComputerSerial
 $SystemType = $results.AssetHeaderData.SystemType
 $ServiceLevelDescription = $results.Entitlements.ServiceLevelDescription
 $SystemModel = $results.AssetHeaderData.SystemModel
 $Region = $results.AssetHeaderData.Region         
 $StartDate = $results.Entitlements.StartDate.Date | Select -ExpandProperty DateTime 
 $EndDate = $results.Entitlements.EndDate.Date | Select -ExpandProperty DateTime  
 Write-Host "Writing $ComputerName info" 
 $x = 0
   foreach ($Sdate in $StartDate) {
    $Detail = New-Object PSObject 
    $Detail | Add-Member Noteproperty ComputerName $ComputerName
    $Detail | Add-Member Noteproperty Serial $ComputerSerial
    $Detail | Add-Member Noteproperty Manufacturer $ComputerManufacturer
    $Detail | Add-Member Noteproperty SystemType $SystemType
    $Detail | Add-Member Noteproperty SystemModel $SystemModel
    $Detail | Add-Member Noteproperty Region $Region
    $Detail | Add-Member Noteproperty ServiceLevelDescription $ServiceLevelDescription.get_Item($x)
    $Detail | Add-Member Noteproperty StartDate $Sdate
    $Detail | Add-Member Noteproperty EndDate $EndDate.get_Item($x)
    $x++
    $myCol += $Detail
   }
  

}

$myCol | Export-Csv -Path c:\temp\AD_WarrentyComputerSerialNumbers.csv -notype