Pinging list of remote computers

This is another simple yet elegant script in pinging a bunch of remote boxes on your network. You can certainly export the “successful” boxes to text file or CSV by adding an empty array variable such as; $Aliveboxes = @() and then calling that variable to export to text or csv. Example, $Aliveboxes | out-file .\Aliveboxes.txt

 

#This script will ping a list of computer names from a text file

#Variables
$VerbosePreference = “continue”
$Boxes = gc d:\Boxes.txt

#Let the ping begin!
$Boxes | ForEach-Object {
write-host -ForegroundColor Cyan “Pinging $_”
$Test = Test-Connection -ComputerName $_ -Delay 2 -Count 2 -Quiet

if ($Test -match ‘True’) {
#Computers are reachable
write-host -ForegroundColor Green “Successful”
} else {
#Ccomputer unreachable
write-host -ForegroundColor Red “Failed”
}
}
write-host -ForegroundColor Cyan “Script complete!”

Pinging list of remote computers

Rename computer remotely

This came in handy today. I had to rename a computer on the network that was in another department and busy with others tasks – Powershell to the rescue!

Rename-Computer -ComputerName somecomputer01 -NewName somecomputer02 -DomainCredential DomainName\domainadminaccount -Force -PassThru -Restart

This can also be script for multiple computer renames. Check out this blog post for more information on that here.

Rename computer remotely

Removing Office 365 license options from all licensed users

In Exchange 2013 hybrid mode, migrating mailboxes to Office 365 works flawlessly. Although, some proactive action involving licensing options can prevent you from beating your head on the wall when an Office 365 mailbox migration job errors out. If the Office 365 user has “Exchange online” license option checked, the mailbox migration job errors because Office 365 thinks a mailbox already exists in Office 365.

Wordpress1

Wordpress2

Unfortunately, one of the other admins had already licensed all of our Office 365 users and running through all the Office 365 users and unchecking the “Exchange online” license option would be too tedious of a task. Instead let’s do it via Powershell!

First task, enumerate all licensed users in Office 365:

Get-MsolUser | where {$_.isLicensed -eq “True”} | select UserPrincipalName | export-csv d:\licensedusers.csv -NoTypeInformation

The above command retrieves all the licensed users in Office 365, only grabs the UserPrinicipalName and exports that to a CSV.

From there, I did some formatting in excel and copied my finished list to a text file.

Second task, remove the Exchange license option from all users in a text file.

$DisableExchange = New-MsolLicenseOptions -AccountSkuId domainname:ENTERPRISEPACK -DisabledPlans EXCHANGE_S_ENTERPRISE
$Users = gc D:\licensedusers.txt

ForEach ($user in $Users) {
Set-MsolUserLicense -UserPrincipalName $User -LicenseOptions $DisableExchange
}

Now, no more issues moving mailboxes to Office 365!

Removing Office 365 license options from all licensed users

Sync your Active Directory with Powershell

Aside from the regular automatic replication schedule Active Directory in Windows server, you may come across the need to immediately replicate your AD to full convergence.

# Get ready to replicate your AD!
Write-Host “Beginning AD Replication”
Write-Host “==========================”

# Find each domain controller, then do a foreach-object
Get-ADGroupMember ‘Domain Controllers’ | % {
Write-Host “Replicating $($_.Name)”

# Recalculate topology for this server
$null = repadmin /kcc $_.Name

# Replicate it
$null = repadmin /syncall /A /e $_.Name
}
Write-Host “==========================”
Write-Host “Completed AD Replication”

Enjoy!

Sync your Active Directory with Powershell

Track who logs on to domain computers

In a previous job, I had written a batch file script to track who was logging to domain computers.

Batch file script:

for /f “delims=: tokens=2” %%a in (‘ipconfig ^| findstr /R /C:”IPv4 Address”‘) do (set ip=%%a)
set ip=%ip%
echo %COMPUTERNAME% IP:%IP% logged on by USER:%USERNAME% @%TIME%-%DATE% >>\\ServerName\ShareName\Computers\%COMPUTERNAME%.log

This was added as a logon script to a GPO and assigned to all domain workstations.

But this is 2015 and time to convert batch file to a Powershell script!

Powershell:

$IP = ping $env:COMPUTERNAME -n 1
$File = $env:COMPUTERNAME

Write-Output “$env:COMPUTERNAME with IP:$($IP[4].substring(20)) was logged on by $env:USERNAME at $(get-date)” | out-file \\Server\ShareName\Computers\$File.txt -Append

Track who logs on to domain computers

View all licensed Office 365 users

A couple of pre-requisites before viewing all  the users in your organization that are licensed in Office 365:

  • Import MSonline module – “import-module msonline“. This is assuming you have the Azure AD powershell cmdlets installed.
  • Connect to your Office 365 tenant – “Connect-msolservice“. You will then be prompted for your Office 365 credentials. Make sure its an account that has administrative rights in your Office 365 tenant.

To view all your users with Office 365 licenses:

Get-MsolUser | where {$_.isLicensed -ne “True”} | sort Displayname -CaseSensitive | ft DisplayName

View all licensed Office 365 users

Find all user accounts set to “Passwords Never Expires”

You can specify a particular OU to search:

get-aduser -filter {(PasswordNeverExpires -eq $True)} -SearchBase “OU=Users,OU=Accounts,DC=Contoso,DC=Com” -Properties * | ft Name,PasswordLastSet,PasswordExpired -AutoSize

Or find all Active Directory user objects by removing the SEARCHBASE criteria:

get-aduser -filter {(PasswordNeverExpires -eq $True)} -Properties * | ft Name,PasswordLastSet,PasswordExpired -AutoSize

Enjoy!

Find all user accounts set to “Passwords Never Expires”

Finding deleted AD objects from a particular date range

I put together this script due to an ongoing AD account cleanup project and needed to put together a report of what was deleted from a particular date range.

*Will only work if the Active Directory Recycle Bin is enabled in your domain.*

write-host -ForegroundColor Cyan “Getting deleted AD objects from date specified”
$StartTime = read-host ” Enter start date in this format: 2/1/2015″
$EndTime = read-host “Enter end date in this format: 2/10/2015”
$Deleted = @()

$Deleted = Get-ADObject -Filter {(isdeleted -eq $true) -and (name -ne “Deleted Objects”)} -includeDeletedObjects -property whenChanged,LastKnownParent |
Where-Object {$_.whenChanged -ge $StartTime -and $_.whenChanged -le $EndTime} |
Select @{ Name=’UserName’; Expression={$_.Name}}, @{
Name=’LastLocation’; Expression={$_.LastKnownParent}}, @{
Name=’Deleted’;Expression={$_.whenChanged}} | sort whenChanged -Descending

$Deleted | out-gridview

Or, you can simply run this line below. Although, two interesting things to note here:

1. The information is truncated. And, setting the variable $FormatEnumerationLimit to =-1 doesn’t affect the truncated information.

2. Curious that it displays same day deletions except the above script. The above script is filtering for objects that have “isDeleted” attribute set to “True” and that object is found in “Deleted Objects’ container. The line below is using the “SearchBase” parameter and specifying the “Deleted Objects” DN path. Something might tweak to test this outcome.

Get-ADObject -SearchBase “CN=Deleted Objects,DC=hq,DC=crabel,DC=com” -Filter * -IncludeDeletedObjects -Properties LastKnownParent, whenChanged | Sort whenChanged -Descending | ft Name,whenChanged,LastKnownParent

Enjoy!

Finding deleted AD objects from a particular date range

Powershell: Find all services that are running or stopped

A quick one liner on how to discover services that are “running” or “stopped”.

Get-Service | where {$_.Status -eq “Running”}

This will give you all Windows services that are in a “running” state. The line gives you all services and pipes it to the “where” filter for a condition. That condition is to find all services where that have a “status” that is “equal” (-eq) to “running”.

To find all services that are not running or stopped, just simply change the “equal” condition to “not equal”

Get-Service | where {$_.Status -ne “Running”}

Or, another way, would be to simply keep the “-eq” condition and change the “status” to “STOPPED” to give you all services that are not running.

Powershell: Find all services that are running or stopped