Skriptet består av fyra funktioner. Alla funktionerna använder pipen för input.
Exempel.
Körs frFör att kolla alla servrar vars namn börjar på 'SRV0*'Get-ADComputer -Filter {name -like "SRV0*"} | Find-UserProfiles | Translate-SidToUserName | Check-User
Lycka till
---- Skript ----
import-module activedirectory
Function Test-PortConnection
{
param([string]$sName)
try
{
$ret = ([Net.DNS]::GetHostAddresses($sName))[0].IPAddressToString
$tcpClient = New-Object System.Net.Sockets.TCPClient
$tcpClient.Connect("$ret",135)
$tcpClient.Connected
}
catch [System.Net.Sockets.SocketException]
{
Write-host " - " + $_.Exception.message -ForegroundColor red
$false
}
catch
{
}
}
Function Find-UserProfiles
{
Process
{
try
{
write-host $_.name -ForegroundColor Green -nonewline
if (Test-PortConnection ($_.name))
{
Get-WmiObject Win32_UserProfile -ComputerName ($_.name) -ea Stop | Select sid, lastusetime
write-host . -ForegroundColor Green
}
}
Catch [System.Runtime.InteropServices.COMException]
{
Write-host " Blockerad " -ForegroundColor Red
}
catch
{
write-host "Undantagsfel: $_.Exception.GetType()"
}
}
}
Function Translate-SidToUserName
{
Process
{
$oSID = New-Object System.Security.Principal.SecurityIdentifier($_.sid)
if($osid.IsAccountSid())
{
try
{
if($_.lastusetime -ne $null)
{
$Time = ([WMI] '').ConvertToDateTime($_.lastusetime)
$Days = (New-TimeSpan -Start $time -End (get-date)).days
}
Else
{
$days = 0
}
$osid = $oSID.Translate([System.Security.Principal.NTAccount])
Add-Member -InputObject $oSID -MemberType noteproperty -Name "LastUse" -Value $days -PassThru
}
catch [System.Management.Automation.MethodException]
{
if ($osid.IsEqualDomainSid((Get-ADDomain).domainsid.tostring())) {Write-host "`n $oSID `t $days"}
}
catch
{
" Undantagsfel: " + $_.Exception.GetType()
}
}
}
}
function Check-User
{
Process
{
if($_.value.length -gt 1 -and $_ -notmatch "NT AUTHORITY")
{
$dom = ($_.value).split("\")[0]
$usr = ($_.value).split("\")[1]
if($dom -notLike ($env:computername))
{
$sOut = $usr | get-aduser -Properties enabled |where {$_.enabled -eq $false}
if($sOut){Add-Member -InputObject $sOut -MemberType noteproperty -Name "LastUse" -Value ($_.lastuse) -force }
$sout | FT Name, lastuse -AutoSize -HideTableHeaders
}
}
}
}
Inga kommentarer:
Skicka en kommentar