Skip to content

Instantly share code, notes, and snippets.

View ankitjoshi8518's full-sized avatar

Ankit Joshi ankitjoshi8518

  • Graphic International Centre
  • Abu Dhabi
View GitHub Profile
@ankitjoshi8518
ankitjoshi8518 / CreateAzureCustomRole-ReadVMStatus.ps1
Created July 24, 2021 11:42
Create Azure Custom role using Powershell
#Run the below script to create new role "Virtual Machine Status" which allows the user to just view the Virtual Machine
#The most easiest way to get started is by checking the permissions to other role and modifying it as required
Get-AzureRmRoleDefinition -Name "Virtual Machine User Login"
(Get-AzureRmRoleDefinition -Name "Virtual Machine User Login").actions # list the actions that is possible with this role
$role= Get-AzureRmRoleDefinition -Name "Virtual Machine User Login" # copy the role to a variable
$role #shows the same output as line#3
#assign custom values to the new role that needs to be created
@ankitjoshi8518
ankitjoshi8518 / UserOffboardingFromM365.ps1
Created July 31, 2021 11:38
Converting mailbox to shared, grant permissions and setup email forwarding
#Install-Module -Name ExchangeOnlineManagement
Connect-ExchangeOnline
$upn=Read-Host "Enter the email address the mailbox to be converted to shared mailbox"
$manager=Read-Host "Enter the email address of the MANAGER"
$access=Read-Host "Does manager need access to old emails? Y for yes and N for No"
Set-Mailbox -Identity $upn -Type Shared
if ($access -eq 'y')
@ankitjoshi8518
ankitjoshi8518 / getloggedonuser.ps1
Created August 5, 2021 12:16
Identify the user logged on a remote computer
Get-WmiObject -ComputerName DESKTOP1 -Class Win32_ComputerSystem | Select-Object UserName
@ankitjoshi8518
ankitjoshi8518 / disableIEENhancedSecurityonIE.ps1
Created October 10, 2021 12:22
Disable IE Enhanced Security
function Disable-IEESC {
$AdminKey = "HKLM:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A7-37EF-4b3f-8CFC-4F3A74704073}"
$UserKey = "HKLM:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A8-37EF-4b3f-8CFC-4F3A74704073}"
Set-ItemProperty -Path $AdminKey -Name "IsInstalled" -Value 0
Set-ItemProperty -Path $UserKey -Name "IsInstalled" -Value 0
Stop-Process -Name Explorer
Write-Host "IE Enhanced Security Configuration (ESC) has been disabled." -ForegroundColor Green
}
Disable-IEESC
@ankitjoshi8518
ankitjoshi8518 / SearchADComputers.ps1
Created October 31, 2021 07:51
Get list of all computers (Windows Servers and Client OS) from AD
#Get all active computers as seeb by AD in last 50 days:
$Date = (Get-Date).AddDays(-49)
Get-ADComputer -Filter {LastLogonDate -gt $Date} | Select distinguishedName
#Get all computers from AD where OS version includes "server":
Get-ADComputer -Filter 'operatingsystem -like "*server*" -and enabled -eq "true"' `
-Properties Name,Operatingsystem,OperatingSystemVersion,IPv4Address |
Sort-Object -Property Operatingsystem |
@ankitjoshi8518
ankitjoshi8518 / checkfilelockingandCloseit.ps1
Created November 9, 2021 06:17
Check if a file on windows file share is locked and forcefully close it
#sometimes if the file has been opened in Read+Write mode by excel, a temporary file will be created in the same directory starting with ~$filename.xlsx
#delete that temporary file which is in edit mode
Get-childitem -path C:\sharefolderpath
remove-item -path C:\sharefolderpath\~$File.xlsx
#check if the actual file is locked - This will also display the username and client IP along with fileID and sessionID
Get-SmbOpenFile | Where-Object -Property sharerelativepath -match "filename.XLSX" | fl
#Find computers added to domain before 30 days
$limit = (get-date).adddays(-30)
$computers = get-adcomputer -Properties whencreated -Filter {whencreated -lt $limit}
@ankitjoshi8518
ankitjoshi8518 / ExchangeOnline.ps1
Last active December 21, 2021 05:52
Exchange online commands
#Get folder statistics and check if mailbox is full. If in place archive is enabled, mailbox size in exchange admin centre and outlook
#is not correct. This command will show the stats
Get-MailboxFolderStatistics -Identity upn@domain.com -IncludeAnalysis -FolderScope RecoverableItems | Format-Table Name,ItemsInFolder,FolderSize,*Subject*
# grant mailbox access without automapping in outlook
# mailbox id2 is the user who will get access to the mailbox id1
Add-MailboxPermission -Identity <Mailbox ID1> -User <Mailbox ID2> -AccessRights FullAccess -AutoMapping:$false
@ankitjoshi8518
ankitjoshi8518 / Sync internet time on windows client OS.txt
Created December 19, 2021 06:11
Access denied error when trying to set internet time on windows client
#Problem - windows client shows wrong time
#tried to adjust date/time > change date and time > Throws access denied error
#found the below commands to fix this:
net stop w32time
w32tm /unregister
w32tm /register
net start w32time
# Windows time service missing in services so the first command failed
@ankitjoshi8518
ankitjoshi8518 / Azuretablestorage.ps1
Created December 28, 2021 10:35
Working with Azure table storage - Adding and retrieving items
#variables
$storageAccountName = 'storageacc'
$sasToken = 'sastoken'
$dateTime = get-date
# creating new context and define table
$storageCtx = New-AzStorageContext -StorageAccountName $storageAccountName -SasToken $sasToken
$tablename = (Get-AzStorageTable -Name $tableName -Context $storageCtx).Cloudtable
# setting partition key