About Me

My photo
HANUMANGARH, RAJASTHAN, India

Tuesday, 20 December 2022

Update User properties in Active Directory from CSV

 

  1. Import-Module ActiveDirectory
  2. $USERS = Import-CSV c:\users.csv
  3. $USERS | foreach {
  4. $empid=$_.employeeid
  5. $company=$_.company
  6. $title=$_.title
  7. $department=$_.department
  8. $Division=$_.division
  9. $pobox=$_.pobox
  10. $city=$_.city
  11. Set-ADUSer -Identity $_.samaccountname -Replace @{employeeID=$empid; company=$company; title=$title; department=$department; division=$division; postofficebox=$pobox; l=$City}
  12. }


=====================================================================================================

Import-Module ActiveDirectory

$USERS = Import-CSV c:\users.csv

$empid=$USERS.employeeid
$company=$USERS.company
$title=$USERS.title
$department=$USERS.department
$Division=$USERS.division
$pobox=$USERS.pobox
$city=$USERS.city
$acc=$USERS.samaccountname

ForEach ($user in $USERS) {
Get-ADUser -Filter "samaccountname -eq '$Acc'" | Foreach {
Set-ADUser $ -Replace @{title = $title}
Set-ADUser $
 -Replace @{company = $company}
Set-ADUser $ -Replace @{department = $department}
Set-ADUser $
 -Add @{division = $division}
Set-ADUser $ -Replace @{postOfficeBox = $pobox}
Set-ADUser $
 -Replace @{l = $city}
Set-ADUser $_ -Replace @{employeeid = $empid}
}
}

Monday, 19 December 2022

Create an ICON in system Tray

 #Create an ICON in system tray

Param

 (

[String]$Restart

 )

Hostname to IP address

 $location = Read-Host "CSV Path: "

$Names = Import-Csv -Path $location


foreach($n in $Names)

{

    try {

        $Computer = [system.net.dns]::resolve($n.NAME) | Select HostName,AddressList 

        $IP = ($Computer.AddressList).IPAddressToString

        New-Object PSObject -Property @{IPAddress=$IP; Name=$Computer.HostName} | Export-Csv \\filepath\Working.csv -NoTypeInformation -Append

    } catch {

        Write-Host "$($n.NAME) is unreachable."

        Write-Output $n | Export-Csv \\Filepath\Unreachable.csv -Append -Encoding ASCII

    }

}

Thursday, 13 October 2022

Create Active Directory Users from CSV with PowerShell

Create csv with below link:

https://www.alitajran.com/wp-content/uploads/spreadsheets/NewUsersReceived.csv



# Import active directory module for running AD cmdlets

Import-Module ActiveDirectory
  
# Store the data from NewUsersFinal.csv in the $ADUsers variable
$ADUsers = Import-Csv C:\temp\NewUsersFinal.csv -Delimiter ";"

# Define UPN
$UPN = "exoip.com"

# Loop through each row containing user details in the CSV file
foreach ($User in $ADUsers) {

    #Read user data from each field in each row and assign the data to a variable as below
    $username = $User.username
    $password = $User.password
    $firstname = $User.firstname
    $lastname = $User.lastname
    $initials = $User.initials
    $OU = $User.ou #This field refers to the OU the user account is to be created in
    $email = $User.email
    $streetaddress = $User.streetaddress
    $city = $User.city
    $zipcode = $User.zipcode
    $state = $User.state
    $country = $User.country
    $telephone = $User.telephone
    $jobtitle = $User.jobtitle
    $company = $User.company
    $department = $User.department

    # Check to see if the user already exists in AD
    if (Get-ADUser -F { SamAccountName -eq $username }) {
        
        # If user does exist, give a warning
        Write-Warning "A user account with username $username already exists in Active Directory."
    }
    else {

        # User does not exist then proceed to create the new user account
        # Account will be created in the OU provided by the $OU variable read from the CSV file
        New-ADUser `
            -SamAccountName $username `
            -UserPrincipalName "$username@$UPN" `
            -Name "$firstname $lastname" `
            -GivenName $firstname `
            -Surname $lastname `
            -Initials $initials `
            -Enabled $True `
            -DisplayName "$lastname, $firstname" `
            -Path $OU `
            -City $city `
            -PostalCode $zipcode `
            -Country $country `
            -Company $company `
            -State $state `
            -StreetAddress $streetaddress `
            -OfficePhone $telephone `
            -EmailAddress $email `
            -Title $jobtitle `
            -Department $department `
            -AccountPassword (ConvertTo-secureString $password -AsPlainText -Force) -ChangePasswordAtLogon $True

        # If user is created, show message.
        Write-Host "The user account $username is created." -ForegroundColor Cyan
    }
}

Read-Host -Prompt "Press Enter to exit"

Thursday, 15 September 2022

Get Domain Name with DC

 $ADForestInfo = Get-ADForest

$ADForestInfoName = $ADForestInfo.Name

$ADForestInfoDomains = $ADForestInfo.Domains

$ADForestInfoForestMode = $ADForestInfo.ForestMode

$AllDCs = $Null

ForEach ($DomainDNS in $ADForestInfoDomains)

{

[string]$DomainDC = (Get-ADDomainController -Domain $DomainDNS -discover -service “ADWS”).HostName

$DomainInfo = Get-ADDomain -server $DomainDC

$DomainInfoDomainMode = $DomainInfo.DomainMode

[array]$AllDomainDCs = Get-ADDomainController -server $DomainDC -filter *

$AllDomainDCsCount = $AllDomainDCs.Count

Write-Output “Discovered $AllDomainDCsCount Domain Controllers in the $DomainDNS Domain (DFL: $DomainInfoDomainMode) ”

[array]$AllDCs += $AllDomainDCs

}

$AllDCsCount = $AllDCs.Count

Write-Output “Discovered $AllDCsCount Domain Controllers in the AD Forest $ADForestInfoName (FFL: $ADForestInfoForestMode) ”

$AllDCs | select domain,name,operatingsystem,OperatingSystemServicePack | sort domain | ft -auto

Get all DC detail with OS, IP, Forest and Site Name

 # Import AD module

Import-Module ActiveDirectory

 

# Get your ad domain

$DomainName = (Get-ADDomain).DNSRoot

 

# Setup email parameters

$subject = "Domain Controllers in $DomainName"

$priority = "Normal"

$smtpServer = "smtp.powershellbros.com"

$emailFrom = "paweljanowicz@powershellbros.com"

$emailTo = (Get-ADUser -Identity $ENV:Username -Properties mail).mail

$port = 25

 

# Get all DC's

$AllDCs = Get-ADDomainController -Filter * -Server $DomainName | Select-Object Hostname,Ipv4address,isGlobalCatalog,Site,Forest,OperatingSystem

  

# Create empty DataTable object

$DCTable = New-Object System.Data.DataTable

      

# Add columns

$DCTable.Columns.Add() | Out-Null

$DCTable.Columns[0].Caption = "Hostname"

$DCTable.Columns[0].ColumnName = "Hostname"

  

$DCTable.Columns.Add() | Out-Null

$DCTable.Columns[1].Caption = "IPv4Address"

$DCTable.Columns[1].ColumnName = "IPv4Address"

                      

$DCTable.Columns.Add() | Out-Null

$DCTable.Columns[2].Caption = "isGlobalCatalog"

$DCTable.Columns[2].ColumnName = "isGlobalCatalog"

$DCTable.Columns[2].DataType = "Boolean"

  

$DCTable.Columns.Add() | Out-Null

$DCTable.Columns[3].Caption = "Site"

$DCTable.Columns[3].ColumnName = "Site"

  

$DCTable.Columns.Add() | Out-Null

$DCTable.Columns[4].Caption = "Forest"

$DCTable.Columns[4].ColumnName = "Forest"

  

$DCTable.Columns.Add() | Out-Null

$DCTable.Columns[5].Caption = "OperatingSystem"

$DCTable.Columns[5].ColumnName = "OperatingSystem"

 

$DCTable.Columns.Add() | Out-Null

$DCTable.Columns[6].Caption = "PingStatus"

$DCTable.Columns[6].ColumnName = "PingStatus"

 

# Loop each DC                        

ForEach($DC in $AllDCs)

{  

    $ping = ping $DC.Hostname -n 1 | Where-Object {$_ -match "Reply" -or $_ -match "Request timed out" -or $_ -match "Destination host unreachable"}

 

    switch ($ping)

    {

        {$_ -like "Reply*" }                          { $PingStatus = "Success" }

        {$_ -like "Request timed out*"}               { $PingStatus = "Timeout" }

        {$_ -like "Destination host unreachable*"}    { $PingStatus = "Unreachable" }

        default                                       { $PingStatus = "Unknown" }

    }

          

    $DCTable.Rows.Add(  $DC.Hostname,

                        $DC.Ipv4address,

                        $DC.isGlobalCatalog,

                        $DC.Site,

                        $DC.Forest,

                        $DC.OperatingSystem,

                        $PingStatus

                              

                        )| Out-Null                          

}

 

# Display results in console 

$DCTable | Sort-Object Site | Format-Table

 

#Creating head style

$Head = @"

<style>

  body {

    font-family: "Arial";

    font-size: 8pt;

    }

  th, td, tr { 

    border: 1px solid #e57300;

    border-collapse: collapse;

    padding: 5px;

    text-align: center;

    }

  th {

    font-size: 1.2em;

    text-align: left;

    background-color: #003366;

    color: #ffffff;

    }

  td {

    color: #000000;

     

    }

  .even { background-color: #ffffff; }

  .odd { background-color: #bfbfbf; }

  h6 { font-size: 12pt; 

       font-color: black;

       font-weight: bold;

       }

 

 text { font-size: 10pt;

        font-color: black;

        }

 }

</style>

"@

 

 

# Email body

[string]$body = [PSCustomObject]$DCTable | Select-Object Hostname,Ipv4address,isGlobalCatalog,Site,Forest,OperatingSystem,PingStatus | Sort-Object -Property Site | ConvertTo-HTML -Head $head -Body "<h6>Domain Controllers</h6></font>"

 

# Send the report email

Send-MailMessage -To $emailTo -Subject $subject -BodyAsHtml $body  -SmtpServer $smtpServer -Port $port -From $emailFrom -Priority $priority

 

Resolve SID to user name

 Function f-sid {

param ( [Parameter(Mandatory=$true)][String]$Sid)

$objSID = New-Object System.Security.Principal.SecurityIdentifier($Sid)

Try {

($objSID.Translate( [System.Security.Principal.NTAccount])).value

}

Catch {

Write-host “`nCouldn’t find any entry matching SID : $Sid” -foregroundcolor cyan

}

}

#Put all SID in text file and run:

get-content C:\Users\su-skumar\Desktop\ser.txt | % { f-sid $_ }

Extract Disk Info from Remote host

 # Define the remote hostname or IP address $remoteHost = Get-Content "C:\temp\RemoteServer.txt" # Define the output CSV file path...