Don Jones
![]() |
| VIDEO: Automating Active Directory Administration with Windows PowerShell |
Session level: 300
![]() |
| VIDEO: Automating Active Directory Administration with Windows PowerShell |
<#
.SYNOPSIS
Identifies services with nonstandard accounts
.DESCRIPTION
Retrieves services that use accounts other than LocalSystem, LocalService or NetworkService
.PARAMETER computername
one or more computernames or IP addresses
you will need local administrator privileges, and the firewall needs to allow access
to enable firewall access, run this command on target machines:
netsh firewall set service type = remoteadmin mode = enable
.EXAMPLE
Get-NonstandardService
lists services with nonstandard accounts on local machine
.EXAMPLE
Get-NonstandardService -computername 10.10.10.33
lists services with nonstandard accounts on remote machine with IP 10.10.10.33
.EXAMPLE
Get-NonstandardService -computername 10.10.10.33, 127.0.0.1, serv12-1, client3
lists services with nonstandard accounts on four machines (including local system)
.LINK
http://www.powershell.com
#>
function Get-NonstandardService {
param(
[String[]]
$computername = '127.0.0.1'
)
# server-side WMI query to minimize network traffic and maximize performance
$wql = 'Select Name, DisplayName, StartName, __Server From Win32_Service WHERE ((StartName != "LocalSystem") and (StartName != "NT Authority\\LocalService") and (StartName != "NT Authority\\NetworkService"))'
# examine all computers submitted:
$computername |
ForEach-Object {
Write-Progress 'examining computer:' $_
# create new object to return information
$rv = New-Object PSObject | Select-Object Computer, Result, Name, DisplayName
$rv.computer = $_
# search for nonstandard services
try {
# always return result as array
$result = @(Get-WmiObject -Query $wql -ComputerName $rv.computer -ErrorAction Stop | Sort-Object DisplayName)
# no results?
if ($result.Count -eq 0) {
# then all services use standard accounts, good:
$rv.Result = 'OK'
$rv
} else {
# return a result set for each nonstandard service
$result | ForEach-Object {
$rv.Computer = $_.__Server
$rv.Name = $_.Name
$rv.DisplayName = $_.DisplayName
$rv.Result = $_.StartName
$rv
}
}
}
catch {
# WMI was unable to retrieve the information
switch ($_) {
# sort out most common errors and return qualified information
{ $_.Exception.ErrorCode -eq 0x800706ba} { $rv.Result = 'WARN: Unavailable (offline, firewall)' }
{ $_.CategoryInfo.Reason -eq 'UnauthorizedAccessException' } { $rv.Result = 'WARN: Access denied' }
# return all other non-common errors
default { $rv.Result = 'WARN: ' + $_.Exception.Message }
}
# return error information
$rv
}
}
}
# This Command will list the user not inheriting Permission
Get-QADUser -SizeLimit 0 | `
Where-Object {$_.DirectoryEntry.PSBase.ObjectSecurity.AreAccessRulesProtected}
# This Command will enable inheriting Permission for all the accounts
Get-QADUser -SizeLimit 0 | `
Where-Object {$_.DirectoryEntry.PSBase.ObjectSecurity.AreAccessRulesProtected} | `
Set-QADObjectSecurity -UnLockInheritance
function Get-NetworkStatistics
{
$properties = 'Protocol','LocalAddress','LocalPort'
$properties += 'RemoteAddress','RemotePort','State','ProcessName','PID'
netstat -ano | Select-String -Pattern '\s+(TCP|UDP)' | ForEach-Object {
$item = $_.line.split(" ",[System.StringSplitOptions]::RemoveEmptyEntries)
if($item[1] -notmatch '^\[::')
{
if (($la = $item[1] -as [ipaddress]).AddressFamily -eq 'InterNetworkV6')
{
$localAddress = $la.IPAddressToString
$localPort = $item[1].split('\]:')[-1]
}
else
{
$localAddress = $item[1].split(':')[0]
$localPort = $item[1].split(':')[-1]
}
if (($ra = $item[2] -as [ipaddress]).AddressFamily -eq 'InterNetworkV6')
{
$remoteAddress = $ra.IPAddressToString
$remotePort = $item[2].split('\]:')[-1]
}
else
{
$remoteAddress = $item[2].split(':')[0]
$remotePort = $item[2].split(':')[-1]
}
New-Object PSObject -Property @{
PID = $item[-1]
ProcessName = (Get-Process -Id $item[-1] -ErrorAction SilentlyContinue).Name
Protocol = $item[0]
LocalAddress = $localAddress
LocalPort = $localPort
RemoteAddress =$remoteAddress
RemotePort = $remotePort
State = if($item[0] -eq 'tcp') {$item[3]} else {$null}
} | Select-Object -Property $properties
}
}
}
#
#Get "fabrikam, kenmyer" to SID
$objUser = New-Object System.Security.Principal.NTAccount("fabrikam", "kenmyer")
$strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])
$strSID.Value
#
# translate "S-1-5-21-1454471165-1004335555-1606985555-5555" to Account Name
$objSID = New-Object System.Security.Principal.SecurityIdentifier `
("S-1-5-21-1454471165-1004335555-1606985555-5555")
$objUser = $objSID.Translate( [System.Security.Principal.NTAccount])
$objUser.Value
http://go.microsoft.com/?linkid=9670048&clcid=0x409
Demo 1: Introducing DirectAccess
http://go.microsoft.com/?linkid=9670126&clcid=0x409
Demo 2: Using Search Federation
http://go.microsoft.com/?linkid=9670127&clcid=0x409
Demo 3: Using Windows PowerShell 2.0
http://go.microsoft.com/?linkid=9670128&clcid=0x409
Demo 4: Configuring AppLocker
http://go.microsoft.com/?linkid=9670129&clcid=0x409
Demo 5: Troubleshooting Windows 7
http://go.microsoft.com/?linkid=9670130&clcid=0x409
http://go.microsoft.com/?linkid=9670049&clcid=0x409
Demo 1: Modifying Windows 7 Operating Systems with DISM
http://go.microsoft.com/?linkid=9670131&clcid=0x409
Demo 2: Automating Deployment Using Windows Deployment Services
http://go.microsoft.com/?linkid=9670132&clcid=0x409
Demo 3: Provisioning Virtual Machines
http://go.microsoft.com/?linkid=9670133&clcid=0x409
http://go.microsoft.com/?linkid=9670050&clcid=0x409
Demo 1: Configuring Group Policy
http://go.microsoft.com/?linkid=9670134&clcid=0x409
Demo 2: Using Windows PowerShell 2.0
http://go.microsoft.com/?linkid=9670135&clcid=0x409
Demo 3: Using Support Tools
http://go.microsoft.com/?linkid=9670136&clcid=0x409
Demo 4: Exploring System Recovery Options
http://go.microsoft.com/?linkid=9670137&clcid=0x409
http://go.microsoft.com/?linkid=9670051&clcid=0x409
Demo 1: Using Hyper-V™ Live Migration
http://go.microsoft.com/?linkid=9670138&clcid=0x409
Demo 2: Booting from Virtual Hard Disk (VHD)
http://go.microsoft.com/?linkid=9670139&clcid=0x409
Demo 3: Administering Windows PowerShell™ Remotely
http://go.microsoft.com/?linkid=9670140&clcid=0x409
Demo 4: Using Active Directory® Management Enhancements
http://go.microsoft.com/?linkid=9670141&clcid=0x409
http://go.microsoft.com/?linkid=9670052&clcid=0x409
Demo 1: Improving Availability and Scalability with Server Core
http://go.microsoft.com/?linkid=9670142&clcid=0x409
Demo 2: Managing Web Applications with the Configuration Editor
http://go.microsoft.com/?linkid=9670143&clcid=0x409
Demo 3: Installing and Using the Windows PowerShell™ Snap-In for IIS 7.5
http://go.microsoft.com/?linkid=9670144&clcid=0x409
Demo 4: Configuring an FTP Server with the New Administration Interface
http://go.microsoft.com/?linkid=9670145&clcid=0x409
Demo 5: Connecting to Windows 7 Clients Using DirectAccess
http://go.microsoft.com/?linkid=9670146&clcid=0x409
http://go.microsoft.com/?linkid=9670053&clcid=0x409
Demo 1: Installing Windows Server Migration Tools
http://go.microsoft.com/?linkid=9670147&clcid=0x409
Demo 2: Migrating Active Directory®
http://go.microsoft.com/?linkid=9670148&clcid=0x409
Demo 3: Migrating DNS Servers
http://go.microsoft.com/?linkid=9670149&clcid=0x409
Demo 4: Migrating IP Settings
http://go.microsoft.com/?linkid=9670150&clcid=0x409
Demo 5: Migrating DHCP Servers
http://go.microsoft.com/?linkid=9670151&clcid=0x409
Demo 6: Migrating Local Users and Groups
http://go.microsoft.com/?linkid=9670152&clcid=0x409
Demo 7: Migrating File Servers
http://go.microsoft.com/?linkid=9670153&clcid=0x409
Demo 8: Migrating Print Servers
http://go.microsoft.com/?linkid=9670154&clcid=0x409