Perrypackettracer / automation

0 stars 0 forks source link

automated task guide #3

Open Perrypackettracer opened 9 months ago

Perrypackettracer commented 9 months ago

Here are the first 5 PowerShell scripts for the mentioned tasks:

1. User Account Creation:

# PowerShell script for user account creation

# Set user details
$Username = "NewUser"
$Password = "P@ssw0rd"
$OU = "OU=Users,DC=domain,DC=com"

# Create new user
New-ADUser -SamAccountName $Username -UserPrincipalName "$Username@domain.com" -Name $Username -GivenName $Username -Surname "Lastname" -Enabled $true -AccountPassword (ConvertTo-SecureString -AsPlainText $Password -Force) -Path $OU

2. Folder/File Backups:

# PowerShell script for folder/file backups

# Set source and destination directories
$SourceDirectory = "C:\Path\To\Source"
$DestinationDirectory = "D:\Path\To\Backup"

# Copy files and directories
Copy-Item -Path $SourceDirectory\* -Destination $DestinationDirectory -Recurse -Force

3. Windows Update Status Reporting:

# PowerShell script for Windows Update status reporting

# Get Windows Update status
$UpdateStatus = Get-HotFix

# Display update status
$UpdateStatus | Format-Table -AutoSize

4. Print Server Configuration:

# PowerShell script for print server configuration

# Install Print Server feature
Install-WindowsFeature Print-Server

# Add a new printer
Add-Printer -Name "PrinterName" -DriverName "PrinterDriver" -PortName "PrinterPort"

5. Event Log Archiving:

# PowerShell script for event log archiving

# Set log and archive paths
$LogPath = "C:\Path\To\EventLogs"
$ArchivePath = "C:\Path\To\Archive"

# Archive event logs
Get-EventLog -LogName Application | Export-Csv "$ArchivePath\Application_Log.csv"

Certainly! Here are the scripts for tasks 6 to 10:

6. Service Restart:

# PowerShell script for service restart

# Set service name
$ServiceName = "YourServiceName"

# Restart the service
Restart-Service -Name $ServiceName

7. Certificate Renewals:

# PowerShell script for certificate renewals

# Set certificate thumbprint
$Thumbprint = "YourCertificateThumbprint"

# Renew the certificate
$certificate = Get-Item -Path Cert:\LocalMachine\My\$Thumbprint
$certificate | Renew-ItemCert

8. File System Permissions:

# PowerShell script for file system permissions

# Set folder path and user
$FolderPath = "C:\Path\To\Folder"
$UserName = "YourUsername"

# Grant permissions to the user
$Acl = Get-Acl -Path $FolderPath
$Ar = New-Object System.Security.AccessControl.FileSystemAccessRule($UserName, "FullControl", "Allow")
$Acl.AddAccessRule($Ar)
Set-Acl -Path $FolderPath -AclObject $Acl

9. Registry Modifications:

# PowerShell script for registry modifications

# Set registry key path and value
$RegistryPath = "HKLM:\Path\To\Registry\Key"
$RegistryValue = "YourRegistryValue"

# Set the new value
Set-ItemProperty -Path $RegistryPath -Name $RegistryValue -Value "NewValue"

10. Network Configuration Changes:

# PowerShell script for network configuration changes

# Set network parameters
$InterfaceAlias = "YourNetworkInterfaceAlias"
$IPAddress = "192.168.1.2"
$SubnetMask = "255.255.255.0"
$DefaultGateway = "192.168.1.1"

# Set network configuration
New-NetIPAddress -InterfaceAlias $InterfaceAlias -IPAddress $IPAddress -PrefixLength 24 -DefaultGateway $DefaultGateway

Certainly! Here are the PowerShell scripts for tasks 11 to 15:

11. Active Directory Cleanup:

# PowerShell script for Active Directory cleanup

# Set criteria for inactive user cleanup
$InactiveDays = 90
$InactiveUsers = Get-ADUser -Filter {LastLogonDate -lt (Get-Date).AddDays(-$InactiveDays)} -Properties LastLogonDate

# Disable and move inactive users
foreach ($User in $InactiveUsers) {
    Disable-ADAccount -Identity $User
    Move-ADObject -Identity $User.DistinguishedName -TargetPath "OU=Disabled Users,DC=domain,DC=com"
}

12. Print Queue Management:

# PowerShell script for print queue management

# Set print queue name
$PrintQueueName = "YourPrintQueueName"

# Clear print queue
Get-PrintJob -Name $PrintQueueName | Remove-PrintJob

13. Database Maintenance:

# PowerShell script for database maintenance

# Set database parameters
$DatabaseServer = "YourDatabaseServer"
$DatabaseName = "YourDatabaseName"

# Perform database maintenance tasks (e.g., backup, index rebuild)
# Your specific maintenance tasks here

14. Email Notification:

# PowerShell script for email notification

# Set email parameters
$EmailTo = "recipient@example.com"
$Subject = "Notification Subject"
$Body = "Notification Body"

# Send email
Send-MailMessage -To $EmailTo -Subject $Subject -Body $Body -SmtpServer "smtp.example.com"

15. Folder Redirection:

# PowerShell script for folder redirection

# Set user and folder details
$Username = "YourUsername"
$SourceFolder = "C:\Users\$Username\Documents"
$DestinationFolder = "\\FileServer\Redirection\Documents"

# Redirect user's Documents folder
Redirect-Item -Path $SourceFolder -Destination $DestinationFolder -Force

Certainly! Here are the PowerShell scripts for tasks 16 to 20:

16. Windows Firewall Rules:

# PowerShell script for Windows Firewall rules

# Set firewall rule details
$RuleName = "YourFirewallRule"
$Port = 8080

# Create a new inbound firewall rule
New-NetFirewallRule -DisplayName $RuleName -Direction Inbound -Protocol TCP -LocalPort $Port -Action Allow

17. Task Scheduler Cleanup:

# PowerShell script for Task Scheduler cleanup

# Set Task Scheduler folder path
$TaskFolder = "\YourTaskFolder"

# Get and remove completed tasks
Get-ScheduledTask -TaskPath $TaskFolder | Where-Object State -eq "Ready" | ForEach-Object { Unregister-ScheduledTask -TaskName $_.TaskName -Confirm:$false }

18. Scheduled Reboots:

# PowerShell script for scheduled reboots

# Schedule a reboot at a specific time
$RebootTime = Get-Date "03:00 AM"
shutdown.exe /r /t 0 /f /d p:0:0 /c "Scheduled Reboot" /time:$RebootTime

19. Disk Cleanup:

# PowerShell script for disk cleanup

# Run built-in Disk Cleanup utility
Start-Process -FilePath "cleanmgr.exe" -ArgumentList "/sagerun:1" -Wait

20. Remote Desktop Session Management:

# PowerShell script for remote desktop session management

# Set user and session details
$Username = "YourUsername"
$SessionID = 1

# Disconnect or log off a specific remote desktop session
# Disconnect: Stop-RemoteDesktopSession -SessionId $SessionID
# Log Off: Stop-RemoteDesktopSession -SessionId $SessionID -LogOff

Certainly! Here are the PowerShell scripts for tasks 21 to 25:

21. Azure AD Sync:

# PowerShell script for Azure AD synchronization

# Trigger Azure AD synchronization
Start-ADSyncSyncCycle -PolicyType Delta

22. DNS Record Updates:

# PowerShell script for DNS record updates

# Set DNS record details
$RecordName = "YourRecordName"
$RecordType = "A"
$IPAddress = "192.168.1.3"

# Update DNS record
Set-DnsServerResourceRecord -ZoneName "yourdomain.com" -Name $RecordName -Type $RecordType -A -IPv4Address $IPAddress

23. Backup Verification:

# PowerShell script for backup verification

# Set backup and verification paths
$BackupPath = "C:\Path\To\Backup"
$VerificationPath = "C:\Path\To\Verification"

# Compare backup and verification folders
Compare-Object -ReferenceObject (Get-ChildItem -Path $BackupPath) -DifferenceObject (Get-ChildItem -Path $VerificationPath)

24. Azure VM Scaling:

# PowerShell script for Azure VM scaling

# Set Azure VM details
$ResourceGroupName = "YourResourceGroup"
$VMName = "YourVMName"
$NewVMSize = "Standard_DS2_v2"

# Resize Azure VM
Resize-AzVM -ResourceGroupName $ResourceGroupName -VMName $VMName -Size $NewVMSize

25. Security Group Management:

# PowerShell script for security group management

# Set security group and user details
$GroupName = "YourSecurityGroup"
$Username = "YourUsername"

# Add user to security group
Add-ADGroupMember -Identity $GroupName -Members $Username
Perrypackettracer commented 9 months ago

Certainly! Here are the PowerShell scripts for tasks 26 to 30:

26. DHCP Lease Cleanup:

# PowerShell script for DHCP lease cleanup

# Set DHCP server IP address
$DHCPServer = "YourDHCPServer"

# Get and remove expired DHCP leases
Get-DhcpServerv4Lease -ComputerName $DHCPServer | Where-Object { $_.LeaseExpiry -lt (Get-Date) } | Remove-DhcpServerv4Lease

27. SSL Certificate Installation:

# PowerShell script for SSL certificate installation

# Set certificate details
$CertPath = "C:\Path\To\Certificate.pfx"
$CertPassword = "YourCertPassword"

# Install SSL certificate
Import-PfxCertificate -FilePath $CertPath -CertStoreLocation Cert:\LocalMachine\My -Password (ConvertTo-SecureString $CertPassword -AsPlainText -Force)

28. Windows Update Management:

# PowerShell script for Windows Update management

# Check for and install available updates
Install-Module PSWindowsUpdate -Force -Confirm:$false
Get-WindowsUpdate -Install -AcceptAll -Force

29. Task Scheduler Configuration:

# PowerShell script for Task Scheduler configuration

# Set task details
$TaskName = "YourTaskName"
$ScriptPath = "C:\Path\To\YourScript.ps1"
$TriggerTime = (Get-Date).AddHours(1)

# Create a new scheduled task
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-File $ScriptPath"
$trigger = New-ScheduledTaskTrigger -At $TriggerTime -Once
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $TaskName

30. Logon Script Configuration:

# PowerShell script for logon script configuration

# Set logon script details
$ScriptPath = "\\FileServer\Scripts\LogonScript.ps1"

# Configure logon script for all users
Set-GPRegistryValue -Name "LogonScriptGPO" -Key "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run" -ValueName "LogonScript" -Type String -Value $ScriptPath

Certainly! Here are the PowerShell scripts for tasks 31 to 40:

31. DHCP Lease Cleanup:

# PowerShell script for DHCP lease cleanup

# Set DHCP server IP address
$DHCPServer = "YourDHCPServer"

# Get and remove expired DHCP leases
Get-DhcpServerv4Lease -ComputerName $DHCPServer | Where-Object { $_.LeaseExpiry -lt (Get-Date) } | Remove-DhcpServerv4Lease

32. SSL Certificate Installation:

# PowerShell script for SSL certificate installation

# Set certificate details
$CertPath = "C:\Path\To\Certificate.pfx"
$CertPassword = "YourCertPassword"

# Install SSL certificate
Import-PfxCertificate -FilePath $CertPath -CertStoreLocation Cert:\LocalMachine\My -Password (ConvertTo-SecureString $CertPassword -AsPlainText -Force)

33. Windows Update Management:

# PowerShell script for Windows Update management

# Check for and install available updates
Install-Module PSWindowsUpdate -Force -Confirm:$false
Get-WindowsUpdate -Install -AcceptAll -Force

34. Task Scheduler Configuration:

# PowerShell script for Task Scheduler configuration

# Set task details
$TaskName = "YourTaskName"
$ScriptPath = "C:\Path\To\YourScript.ps1"
$TriggerTime = (Get-Date).AddHours(1)

# Create a new scheduled task
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-File $ScriptPath"
$trigger = New-ScheduledTaskTrigger -At $TriggerTime -Once
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $TaskName

35. Logon Script Configuration:

# PowerShell script for logon script configuration

# Set logon script details
$ScriptPath = "\\FileServer\Scripts\LogonScript.ps1"

# Configure logon script for all users
Set-GPRegistryValue -Name "LogonScriptGPO" -Key "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run" -ValueName "LogonScript" -Type String -Value $ScriptPath

36. Local User Account Management:

# PowerShell script for local user account management

# Set user details
$Username = "LocalUser"
$Password = "LocalPassword"

# Create a new local user
New-LocalUser -Name $Username -Password (ConvertTo-SecureString -AsPlainText $Password -Force) -PasswordNeverExpires $true

37. File Integrity Monitoring:

# PowerShell script for file integrity monitoring

# Set file and hash details
$FilePath = "C:\Path\To\File.txt"
$ExpectedHash = "YourExpectedHash"

# Calculate the hash of the file
$ActualHash = Get-FileHash -Path $FilePath -Algorithm SHA256

# Compare hashes
if ($ActualHash.Hash -eq $ExpectedHash) {
    Write-Host "File integrity is intact."
} else {
    Write-Host "File integrity is compromised!"
}

38. Group Policy Updates:

# PowerShell script for Group Policy updates

# Force Group Policy update
gpupdate /force

39. Remote Desktop Gateway Configuration:

# PowerShell script for Remote Desktop Gateway configuration

# Set RD Gateway server details
$RDGatewayServer = "YourRDGatewayServer"

# Configure RD Gateway settings
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Terminal Server Gateway" -Name "RdGateway" -Value $RDGatewayServer

40. IIS Website Deployment:

# PowerShell script for IIS website deployment

# Set website details
$WebsiteName = "YourWebsite"
$PhysicalPath = "C:\Path\To\Website"

# Create a new IIS website
New-WebSite -Name $WebsiteName -PhysicalPath $PhysicalPath -Port 80

Certainly! Here are the PowerShell scripts for tasks 41 to 50:

41. Active Directory Group Membership Management:

# PowerShell script for Active Directory group membership management

# Set group and user details
$GroupName = "YourGroup"
$Username = "YourUser"

# Add user to group
Add-ADGroupMember -Identity $GroupName -Members $Username

42. Azure Resource Management:

# PowerShell script for Azure resource management

# Set Azure resource details
$ResourceGroupName = "YourResourceGroup"
$ResourceName = "YourResourceName"

# Start an Azure resource
Start-AzResource -ResourceGroupName $ResourceGroupName -Name $ResourceName

43. PowerShell Script Execution Policies:

# PowerShell script for PowerShell script execution policies

# Set execution policy
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine

44. Remote Desktop Gateway Configuration:

# PowerShell script for Remote Desktop Gateway configuration

# Set RD Gateway server details
$RDGatewayServer = "YourRDGatewayServer"

# Configure RD Gateway settings
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Terminal Server Gateway" -Name "RdGateway" -Value $RDGatewayServer

45. IIS Website Deployment:

# PowerShell script for IIS website deployment

# Set website details
$WebsiteName = "YourWebsite"
$PhysicalPath = "C:\Path\To\Website"

# Create a new IIS website
New-WebSite -Name $WebsiteName -PhysicalPath $PhysicalPath -Port 80

46. Disk Cleanup:

# PowerShell script for disk cleanup

# Run built-in Disk Cleanup utility
Start-Process -FilePath "cleanmgr.exe" -ArgumentList "/sagerun:1" -Wait

47. Remote Desktop Session Management:

# PowerShell script for remote desktop session management

# Set user and session details
$Username = "YourUsername"
$SessionID = 1

# Disconnect or log off a specific remote desktop session
# Disconnect: Stop-RemoteDesktopSession -SessionId $SessionID
# Log Off: Stop-RemoteDesktopSession -SessionId $SessionID -LogOff

48. Azure AD Sync:

# PowerShell script for Azure AD synchronization

# Trigger Azure AD synchronization
Start-ADSyncSyncCycle -PolicyType Delta

49. DNS Record Updates:

# PowerShell script for DNS record updates

# Set DNS record details
$RecordName = "YourRecordName"
$RecordType = "A"
$IPAddress = "192.168.1.3"

# Update DNS record
Set-DnsServerResourceRecord -ZoneName "yourdomain.com" -Name $RecordName -Type $RecordType -A -IPv4Address $IPAddress

50. Security Group Management:

# PowerShell script for security group management

# Set security group and user details
$GroupName = "YourSecurityGroup"
$Username = "YourUsername"

# Add user to security group
Add-ADGroupMember -Identity $GroupName -Members $Username

Adapt these scripts according to your specific environment and requirements. Always test thoroughly in a safe environment before deploying in production.