Powershell : Find all active users who havent logged in

Request from a customer. Find me all active ( IE not disabled ) users in a particular OU who have never logged in.

“I have a powershell for that” appears to be the default answer for most queries these days …

Get-ADUser -Filter {enabled -eq $true} -SearchBase "OU=Eu,DC=eu,DC=notyou,DC=com" -Properties "LastLogonDate" | sort-object -property lastlogondate -descending |Format-Table -property Name, sAMAccountName, LastLogonDate -AutoSize >>c:temptestoutput.csv

Powershell : remote machines static dns to DHCP

This will read an input file , and using the WMI object change any statically assigned DNS to DHCP.

 

# grabs the ListofMachines from the CSV
$ListofMachines=Get-Content “c:tempmachines.csv”
# loop starts here
foreach ($machine in $ListofMachines)
{
#Performs a release ( if needed ! use with care remotely ! )
#Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $machine | Where { $_.IpEnabled -eq $true -and $_.DhcpEnabled -eq $true} | ForEach-Object -Process {$_.ReleaseDHCPLease()}
# query the $machine that has the DHCP enabled adapter and performs the renewal
$wmi = Get-WmiObject win32_networkadapterconfiguration -computerName $machine -filter “ipenabled =’true'”;
$wmi.EnableDHCP();
$wmi.SetDNSServerSearchOrder();

Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $machine | Where { $_.IpEnabled -eq $true -and $_.DhcpEnabled -eq $true} | ForEach-Object -Process {ipconfig /flushdns}

}

Powershell : renew multiple machines DHCP scopes

# grabs the ListofMachines from the CSV
$ListofMachines=Get-Content “c:tempmachines.csv”
# loop starts here
foreach ($machine in $ListofMachines)
{
#Performs a release ( if needed ! use with care remotely ! )
#Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $machine | Where { $_.IpEnabled -eq $true -and $_.DhcpEnabled -eq $true} | ForEach-Object -Process {$_.ReleaseDHCPLease()}
# query the $machine that has the DHCP enabled adapter and performs the renewal
Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $machine | Where { $_.IpEnabled -eq $true -and $_.DhcpEnabled -eq $true} | ForEach-Object -Process {$_.RenewDHCPLease()}
Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName 10.130.54.53 | Where { $_.IpEnabled -eq $true -and $_.DhcpEnabled -eq $true} | ForEach-Object -Process {ipconfig /flushdns}
#log for Output!
write-output “$machine has had IP renewed ” |out-file -filepath “c:tempresultsofDHCPLease04032015.csv” -append
}

Powershell : renew remote IP address

We wrote this at GiraffeIT.com. If you know the remote IP or DNS name of a machine it will help you renew a DHCP lease and flush the DNS.

Very useful if you have a remote machine with a scope change and its not being reflected.

 

write-host “—————————–”
write-host “DHCP lease renewal + flushDNS”
write-host “—————————–”
$machine = read-host “IP/Name of machine needing lease renewal ? ”
Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $machine | Where { $_.IpEnabled -eq $true -and $_.DhcpEnabled -eq $true} | ForEach-Object -Process {$_.RenewDHCPLease()}
Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $machine | Where { $_.IpEnabled -eq $true -and $_.DhcpEnabled -eq $true} | ForEach-Object -Process {ipconfig /flushdns}