Monday, August 20, 2018

Powershell - Find IP range for device

This was a pre-check for client computers about the be ADMT'ed to a new domain. The back story is a computer could be on a wired connection, a few WIFI networks which some had domain connectivity and some did not, and 4g/Home connection and direct access. At the point of migrate we bumped out the laptops that were going to fail, this was just a pre-check.

This was just a report so we could chase the user to correct the problem.

This script could be much easier in Windows 10 due to new functions (ip v6), but I did not have the option.


$computers = Import-Csv C:\temp\computerlist.txt
ipconfig /flushdns > null
#cls

foreach ($computers in $computers){
$ThisComputer=$computers.computername.trim()
$NoMigComputer=$ThisComputer + ".old.gov.au"

$testcon = " NOT reachable"
$didit = Test-Connection $NoMigComputer -count 1 -quiet
if ($didit -ne $false)
{$testcon = "and is contactable"}

#write-host Attempt to look at $NoMigComputer
try {

$result = $null
$JustIP = $null
$result = [System.Net.Dns]::GetHostAddresses($NoMigComputer)
$JustIP = $result.ipaddresstostring

if ($result -eq $null)
    {    write-host $NoMigComputer " is not in DNS"    }
else
    {
    #write-host $NoMigComputer" IP is" $JustIP
        if
        (
        $JustIP -like '192.22.124.*' -or
        $JustIP -like '192.28.126.*' -or
        $JustIP -like '192.22.234.*'
        )
        {
        write-host $NoMigComputer $JustIP  "wired" $testcon

        }
        else
        {
        write-host $NoMigComputer $JustIP "is on WIFI" $testcon
        } } }
catch {

$YesMigComputer=$ThisComputer + ".ic.nsw.gov.au"
try {
$Yesresult = [System.Net.Dns]::GetHostAddresses($YesMigComputer)
}
catch {
#$YesMigComputer + " not in IC domain EITHER"
}

$JustIP = $result.ipaddresstostring

if ($JustIP -eq $null)
    {
    write-host $NoMigComputer "is not in DNS *V4"
    }

if ($NoMigComputer -eq $null)
{
Write-host $NoMigComputer "On 4G connection"
} } }

Powershell - ping the top of a list

This is a bit of a dodgy, basically the migration was many computers at once each day, feed in from a list. This just picked the top computer and would ping it on screen so I could see if the process had started for the whole group. The first step in my migration was a pre-reboot.



$computers = Import-Csv C:\temp\computerlist.txt
ipconfig /flushdns
#cls

foreach ($computers in $computers){
$ThisComputer=$computers.computername.trim()
$NoMigComputer=$ThisComputer + ".old.gov.au"
ping -t $NoMigComputer -4
}



Powershell - create uses from CSV in Active Directory

I did not write this, but fixed it, but I know it will come in handy again.



$targetpassword=ConvertTo-SecureString "Password!" -asplaintext -force
$thisUser= New-Object System.Management.Automation.PSCredential ("name\service_account", $targetpassword)
#$thisUser=Get-Credential

cls
$csv = Import-Csv -Path "C:\temp\User-create.csv"

foreach ($i in $csv) {
#$i.SamAccountName
$pass = [string] $i.password
$oupath = [string] $i.OUDN

New-ADUser -SamAccountName $i.samaccountname -Name $i.Name -GivenName $i.givenname -Surname $i.surname -DisplayName $i.name -UserPrincipalName $i.upn -EmailAddress $i.emailaddress -Path $oupath -Department $i.AgencyNameinCRM -OfficePhone $i.WorkPhoneNumber -title $i.Jobtitle -mobile $i.mobile -streetAddress $i.Street -city $i.city -postalCode $i.PostalCode -description $i.Description  -Credential $thisUser

#write-host -SamAccountName $i.samaccountname -Name $i.Name -GivenName $i.givenname -Surname $i.surname -DisplayName $i.name -UserPrincipalName $i.upn -EmailAddress $i.emailaddress -Path $oupath -Department $i.AgencyNameinCRM -OfficePhone $i.WorkPhoneNumber
write-host "Creating"  $i.givenname $i.surname

Set-ADAccountPassword -Identity $i.SamAccountName -NewPassword (ConvertTo-SecureString -AsPlainText $pass -Force) -Reset  -Credential $thisUser
Set-ADUser -Identity $i.SamAccountName -Enabled $true  -Credential $thisUser
}

Blog Archive