Set DNS servers from Command prompt

Open an elevated command prompt and type:

netsh

In order to view all network connections:

interface ip show config

In order to change the primary dns server:

interface ip set dns name="Local Area Connection" static 8.8.8.8

For the secondary DNS run:

netsh interface ip add dns name="Local Area Connection" 1.1.1.1 index=2
Posted in Windows | Leave a comment

Some useful Powershell One Liners

Find Public IP address

(Invoke-RestMethod ipinfo.io/json).ip

Traceroute

tnc google.com -tr

Check if a port is open

tnc google.com -p 443

Check if a specific update is installed

Get-Hotfix|Where-Object {$_.HotfixID -match "KB5018410"}

Search for a specific file type in a directory (files over 900MB)

Get-ChildItem -Path "\\fileserver\e$\Data\Folder1" -Filter *.zip -Recurse  -ErrorAction SilentlyContinue -Force | where-object {$_.length -gt 924288000}

Time of the last reboot

(Get-CimInstance Win32_OperatingSystem).LastBootUpTime

Find time and initiating user of last system reboot

Get-EventLog -log system -newest 10000 | where-object {$_.eventid -eq '1074'} | format-table machinename, username, timegenerated -autosize

Tail a file

Get-Content ./logfile.log -Tail 5 –Wait

Display all Domain Controllers

Get-ADDomainController -Filter * -server domain.com | Select-Object name, domain

Display information about a specific computer

Get-ADComputer -Filter {Name -Like ""} -Property * | Format-Table Name,ipv4address,OperatingSystem,OperatingSystemServicePack,LastLogonDate -Wrap -Auto

Get all Name Servers in a zone

Get-DnsServerResourceRecord -ZoneName "domain.com" -ComputerName "DC" -RRType "NS" -Node

Search AD Name Server for specific Hostname entry

Get-DnsServerResourceRecord -ZoneName "domain.com" -ComputerName "DC" | where {$_.hostname -like "*gavriil*"} | ft -autosize

Search AD Name Server for specific IP Address entry

Get-DnsServerResourceRecord -ZoneName "domain.com" -ComputerName "DC" | where {$_.RecordData.Ipv4Address.IPAddressToString -contains '192.168.1.83'} | ft -autosize

Search for a user in Active Directory

Get-ADUser -Filter {name -like 'gabriel*'}

Get AD users created in the last 30 days

Get-ADUser -Filter * -Properties whenCreated, description | Where-Object {$_.whenCreated -ge ((Get-Date).AddDays(-30)).Date} |select samaccountname, description

Search all DCs for account lockout events and output to file

ipmo activedirectory;$(Get-ADDomainController -Filter  {(OperatingSystem -ne "") -and (IsReadOnly -ne "True")} | %{Get-WinEvent -ComputerName $_.name -LogName security -FilterXPath "*[System[EventID='4740']]" | Select machinename,TimeCreated,@{Label='User Name';Expression={$_.Properties[0].Value}},@{Label='Client Name';Expression={$_.Properties[1].Value}}}) | Out-File C:\lockout.txt
Posted in Windows | Comments Off on Some useful Powershell One Liners

Create pfx file from certificate and private key

In order to create a pfx file we will need the certificate (crt file) and the private key file.
We need to download and install OpenSSL for windows https://slproweb.com/products/Win32OpenSSL.html.

The first step is to give the same filename to both files (crt and private key) and give the extension .key to the private key file.
So assuming we have the 2 files certificate.crt and certificate.key we have to open OpenSSL and execute the following command:

openssl pkcs12 -export -out newcertificate.pfx -inkey certificate.key -in certificate.crt

We will be asked for a password for the pfx file and we are done.

Posted in Windows | Leave a comment

Move share folder from one drive to another retaining share and security permissions

We will use the robocopy command in order to copy the folder and retain all the security permissions

robocopy "D:\Data\FolderName" "G:\Data4\FolderName" /MIR /SEC /ZB /V /r:1 /w:2 /nfl /ndl

In order to move the share permissions we have to open the following registry key,locate the share and change the path:
HKLM\SYSTEM\CurrentControlSet\Services\LanmanServer\Shares
Restart the Server service and we are done.

Posted in Windows | Leave a comment

Filter Active Directory users based on custom attribute and export to csv

Get-ADUser -Filter {attribute -like 'Yes'} -Properties * | select attribute1, attribute2, attribute3, attribute4, attribute5, attribute5, attribute6 | Export-Csv -path 'd:\edu4.csv' -Encoding unicode

The attributes after the select are exported in the csv file.

If you want to find users with attribute not set you can use the following filter:

Get-ADUser -Filter {-not(extensionAttribute1 -like "*") }
Posted in Windows | Leave a comment

Change the smtp address of a Microsoft 365 Group

In order to create a remote powershell session to 365, open Windows Powershell and run below commands:

$UserCredential = Get-Credential

and enter your 365 credentials when prompted.
Then type:

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session -DisableNameChecking

To add required smtp address:

Set-UnifiedGroup -Identity "Group Name" -EmailAddresses: @{Add ="[email protected]"}

To promote as a primary smtp address:

Set-UnifiedGroup -Identity "Group Name" -PrimarySmtpAddress "[email protected]"

To remove the old address from the group:

Set-UnifiedGroup -Identity "Group Name" -EmailAddresses: @{Remove="[email protected]"

To end your Exchange Online Powershell session:

Remove-PSSession $Session
Posted in Windows | Leave a comment

Freenas issue when replacing failed disk

When replacing a failed hard disk from the GUI you get the following error:
“Unable to GPT format the disk ada…”.
From the command line:
sysctl kern.geom.debugflags=0x10

dd if=/dev/zero of=/dev/ada3 bs=512 count=1

Posted in Linux | Comments Off on Freenas issue when replacing failed disk