Aperture Script Memos
From Datateknik
(Difference between revisions)
(4 intermediate revisions by one user not shown) | |||
Line 4: | Line 4: | ||
*Create a PowerShell script in the same location as the CSV file is located | *Create a PowerShell script in the same location as the CSV file is located | ||
*Use the following script | *Use the following script | ||
+ | <div class="toccolours mw-collapsible mw-collapsed"> | ||
+ | '''Run with PowerShell:''' | ||
+ | <div class="mw-collapsible-content"> | ||
<blockquote><code># Import Active Directory module and import CSV File to list<br>Import-Module Active Directory<br>$import_users = Import-Csv -Path .\Users.csv -Header "FirstName","LastName","Password" -Delimiter ";"<br># Start a loop and send each line of the CSV file to create a user<br> | <blockquote><code># Import Active Directory module and import CSV File to list<br>Import-Module Active Directory<br>$import_users = Import-Csv -Path .\Users.csv -Header "FirstName","LastName","Password" -Delimiter ";"<br># Start a loop and send each line of the CSV file to create a user<br> | ||
$import_users | ForEach-Object{<br> | $import_users | ForEach-Object{<br> | ||
Line 17: | Line 20: | ||
-Enabled $True `<br> | -Enabled $True `<br> | ||
} | } | ||
− | </code></blockquote> | + | </code></blockquote></div> |
+ | </div> | ||
<h2>Blocking open resolver and enabeling WSUS Service on Windows Server 2019 AD</h2> | <h2>Blocking open resolver and enabeling WSUS Service on Windows Server 2019 AD</h2> | ||
*Disable recursive queries | *Disable recursive queries | ||
*Use the following script | *Use the following script | ||
+ | <div class="toccolours mw-collapsible mw-collapsed"> | ||
+ | '''Run with PowerShell:''' | ||
+ | <div class="mw-collapsible-content"> | ||
<blockquote><code># Script for allowing internal hosts to send recursive queries to a public DNS server<br># Adding internal network IP addresses to the client subnet<br># Creating a scope to enable recursion and where to send queries<br># Creating policy for recursion matching client subnet and recursion scope<br>Add-DnsServerClientSubnet -Name InternalNetworkIP -IPv4Subnet 1.2.3.4 /n | <blockquote><code># Script for allowing internal hosts to send recursive queries to a public DNS server<br># Adding internal network IP addresses to the client subnet<br># Creating a scope to enable recursion and where to send queries<br># Creating policy for recursion matching client subnet and recursion scope<br>Add-DnsServerClientSubnet -Name InternalNetworkIP -IPv4Subnet 1.2.3.4 /n | ||
-IPv6Subnet aaa:bbbb:ccc::/64<br>Add-DnsServerRecursionScope -Name InternalRecursionScope -EnableRecursion $true | -IPv6Subnet aaa:bbbb:ccc::/64<br>Add-DnsServerRecursionScope -Name InternalRecursionScope -EnableRecursion $true | ||
-Forwarder 8.8.8.8, 2001:4860:4860::8888<br>Add-DnsServerQueryResolutionPolicy -Name InternalQueryPolicy -Action ALLOW -ApplyOnRecursion -ClientSubnet "EQ,InternalNetworkIP" -RecursionScope InternalRecursionScope<br># Confirm configuration by showing DNS recursion policy, DNS recursion scope, and our query policy.<br>Get-DnsServerClientSubnet<br>Get-DnsServerRecursion<br>Get-DnsServerRecursionScope<br>Get-DnsServerQueryResolutionPolicy | -Forwarder 8.8.8.8, 2001:4860:4860::8888<br>Add-DnsServerQueryResolutionPolicy -Name InternalQueryPolicy -Action ALLOW -ApplyOnRecursion -ClientSubnet "EQ,InternalNetworkIP" -RecursionScope InternalRecursionScope<br># Confirm configuration by showing DNS recursion policy, DNS recursion scope, and our query policy.<br>Get-DnsServerClientSubnet<br>Get-DnsServerRecursion<br>Get-DnsServerRecursionScope<br>Get-DnsServerQueryResolutionPolicy | ||
− | </code></blockquote> | + | </code></blockquote></div> |
+ | </div> | ||
+ | <h2>Creating a CSV file with names</h2> | ||
+ | *Copy names from Canvas from People > Group > Group start page > Group People into TXT file with ISO 8859-4 encoding. | ||
+ | *Run the following script. | ||
+ | <div class="toccolours mw-collapsible mw-collapsed"> | ||
+ | '''Run with Python 3.6 and higher:''' | ||
+ | <div class="mw-collapsible-content"> | ||
+ | <blockquote><code># Python 3.8 Test script to extract student groups to CSV file to be opened with Excel. <br># Make sure Encoding is set to ISO 8859-4 for both this script and TXT file. | ||
+ | <br>import sys | ||
+ | <br>from itertools import islice<br><br> | ||
+ | FILENAME = "Group1Names.txt"<br> | ||
+ | FILENAME2 = "Group2Names.txt"<br> | ||
+ | GROUPFILENAME = "Studentgroups.csv"<br> | ||
+ | def CSVFile(Group, FILENAME, GROUPFILENAME):<br> | ||
+ | Students = ""<br> | ||
+ | with open(FILENAME, 'rt') as f:<br> | ||
+ | for line in islice(f,0,None, 4):<br> | ||
+ | Students += ';'+line<br> | ||
+ | with open(GROUPFILENAME, 'a') as f:<br> | ||
+ | f.write(Group + Students)<br><br> | ||
+ | CSVFile('Grupp 1;\n',FILENAME,GROUPFILENAME)<br> | ||
+ | CSVFile('Grupp 2;\n',FILENAME2,GROUPFILENAME) | ||
+ | </code></blockquote></div> | ||
+ | </div> | ||
+ | <h2>Creating a short status list script from systemctl status on CentOS 8</h2> | ||
+ | <div class="toccolours mw-collapsible mw-collapsed"> | ||
+ | '''Run with Python 3.6 and higher on CentOS 8:''' | ||
+ | <div class="mw-collapsible-content"> | ||
+ | <blockquote> | ||
+ | <code> | ||
+ | <nowiki> #!/bin/python3</nowiki><br><br> | ||
+ | |||
+ | <nowiki># Import needed modules</nowiki><br> | ||
+ | <nowiki>import os</nowiki><br> | ||
+ | <nowiki>import subprocess</nowiki><br><br> | ||
+ | |||
+ | <nowiki># Get terminalsize</nowiki><br> | ||
+ | <nowiki>TERMINALSIZE = os.get_terminal_size()</nowiki><br> | ||
+ | <nowiki>COLUMNLENGHT = TERMINALSIZE.columns</nowiki><br><br> | ||
+ | |||
+ | <nowiki>def StatusFilter(SERVICE):</nowiki><br> | ||
+ | <nowiki># Function that sends the systemctl status command to shell.</nowiki><br> | ||
+ | <nowiki># Saves output as a string and divides the string into lines.</nowiki><br> | ||
+ | <nowiki># Extracts the "Active:" line from lines and returnes it.</nowiki><br> | ||
+ | <nowiki>CMD = 'systemctl status ' + SERVICE</nowiki><br> | ||
+ | <nowiki>output = subprocess.check_output([CMD], shell=True, universal_newlines=True)</nowiki><br> | ||
+ | <nowiki>OUT = output.splitlines()</nowiki><br> | ||
+ | <nowiki>for line in OUT:</nowiki><br> | ||
+ | <nowiki>if 'Active:' in line:</nowiki><br> | ||
+ | <nowiki>return line</nowiki><br><br> | ||
+ | |||
+ | <nowiki>def ServiceStatus(LIST):</nowiki><br> | ||
+ | <nowiki># Loop the a list of services and prints them neatly.</nowiki><br> | ||
+ | <nowiki>for elem in LIST:</nowiki><br> | ||
+ | <nowiki>S = elem + ': '</nowiki><br> | ||
+ | <nowiki>print(S.rjust(15), StatusFilter(elem.lower()))</nowiki><br><br> | ||
+ | |||
+ | <nowiki># Stores a list of services to check status of.</nowiki><br> | ||
+ | <nowiki>LIST = ['SSHD','NFtables']</nowiki><br><br> | ||
+ | <nowiki># Spacing and other strings to print</nowiki><br> | ||
+ | <nowiki>SPACING = '\n' + '='*COLUMNLENGHT +'\n'</nowiki><br> | ||
+ | <nowiki>SPACING2 = '\n' + '-'*COLUMNLENGHT + '\n'</nowiki><br> | ||
+ | <nowiki>HEADING = 'Status of installed/configured services:'</nowiki><br><br> | ||
+ | <nowiki># Calles function to loop through desired services to print status.</nowiki><br> | ||
+ | <nowiki>print(SPACING)</nowiki><br> | ||
+ | <nowiki>print(HEADING.center(int(COLUMNLENGHT - 20)))</nowiki><br> | ||
+ | <nowiki>print(SPACING2)</nowiki><br> | ||
+ | <nowiki>ServiceStatus(LIST)</nowiki><br> | ||
+ | <nowiki>print(SPACING)</nowiki><br> | ||
+ | </code> | ||
+ | </blockquote></div> | ||
+ | </div> | ||
+ | <h2>Use Windows Scheduler to remove old files</h2> | ||
+ | * Create a schedule in Windows Scheduler | ||
+ | * Add the following script to run: | ||
+ | <div class="toccolours mw-collapsible mw-collapsed"> | ||
+ | '''Save file as .bat''' | ||
+ | <blockquote> | ||
+ | <code> | ||
+ | :: Text file formats<br> | ||
+ | ForFiles -p "C:\Users\user\Desktop" -s -m *.txt* /C "cmd /c del @path"<br> | ||
+ | ForFiles -p "C:\Users\user\Desktop" -s -m *.doc* /C "cmd /c del @path"<br> | ||
+ | ForFiles -p "C:\Users\user\Desktop" -s -m *.pdf* /C "cmd /c del @path"<br> | ||
+ | :: txt, doc, odt, rtf, ods, xls, pdf, ppt, odp, pkt, pka, zip.<br> | ||
+ | </code> | ||
+ | </blockquote> | ||
+ | </div> |
Latest revision as of 15:11, 16 August 2021
Contents |
Creating users with CSV file on Windows Server 2019 AD
- Create the CSV file with ";" as delimiter
- Create an OU container in your Active Directory server and record the location
- Create a PowerShell script in the same location as the CSV file is located
- Use the following script
Run with PowerShell:
# Import Active Directory module and import CSV File to list
Import-Module Active Directory
$import_users = Import-Csv -Path .\Users.csv -Header "FirstName","LastName","Password" -Delimiter ";"
# Start a loop and send each line of the CSV file to create a user
$import_users | ForEach-Object{
New-ADUser `
-Name $($_.FirstName + " " + $_.Lastname) `
-GivenName $_.FirstName `
-SurName $_.LastName `
-DisplayName $($_.FirstName + " " + $_.Lastname) `
-SamAccountName $($_.FirstName.Substring(0,3).ToLower() + $_$.LastName.Substring(0,3).ToLower()) `
-UserPrincipalName $($_.FirstName.ToLower() + "." + $_.LastName.ToLower() + "@example.com") `
-AccountPassword $(ConvertTo-SecureString $_.Password -AsPlainText -Force)`
-Path "OU=ExampleUsers,DC=example,DC=com" `
-Enabled $True `
}
Blocking open resolver and enabeling WSUS Service on Windows Server 2019 AD
- Disable recursive queries
- Use the following script
Run with PowerShell:
# Script for allowing internal hosts to send recursive queries to a public DNS server
# Adding internal network IP addresses to the client subnet
# Creating a scope to enable recursion and where to send queries
# Creating policy for recursion matching client subnet and recursion scope
Add-DnsServerClientSubnet -Name InternalNetworkIP -IPv4Subnet 1.2.3.4 /n
-IPv6Subnet aaa:bbbb:ccc::/64
Add-DnsServerRecursionScope -Name InternalRecursionScope -EnableRecursion $true
-Forwarder 8.8.8.8, 2001:4860:4860::8888
Add-DnsServerQueryResolutionPolicy -Name InternalQueryPolicy -Action ALLOW -ApplyOnRecursion -ClientSubnet "EQ,InternalNetworkIP" -RecursionScope InternalRecursionScope
# Confirm configuration by showing DNS recursion policy, DNS recursion scope, and our query policy.
Get-DnsServerClientSubnet
Get-DnsServerRecursion
Get-DnsServerRecursionScope
Get-DnsServerQueryResolutionPolicy
Creating a CSV file with names
- Copy names from Canvas from People > Group > Group start page > Group People into TXT file with ISO 8859-4 encoding.
- Run the following script.
Run with Python 3.6 and higher:
# Python 3.8 Test script to extract student groups to CSV file to be opened with Excel.
# Make sure Encoding is set to ISO 8859-4 for both this script and TXT file.
import sys
from itertools import islice
FILENAME = "Group1Names.txt"
FILENAME2 = "Group2Names.txt"
GROUPFILENAME = "Studentgroups.csv"
def CSVFile(Group, FILENAME, GROUPFILENAME):
Students = ""
with open(FILENAME, 'rt') as f:
for line in islice(f,0,None, 4):
Students += ';'+line
with open(GROUPFILENAME, 'a') as f:
f.write(Group + Students)
CSVFile('Grupp 1;\n',FILENAME,GROUPFILENAME)
CSVFile('Grupp 2;\n',FILENAME2,GROUPFILENAME)
Creating a short status list script from systemctl status on CentOS 8
Run with Python 3.6 and higher on CentOS 8:
#!/bin/python3
# Import needed modules
import os
import subprocess
# Get terminalsize
TERMINALSIZE = os.get_terminal_size()
COLUMNLENGHT = TERMINALSIZE.columns
def StatusFilter(SERVICE):
# Function that sends the systemctl status command to shell.
# Saves output as a string and divides the string into lines.
# Extracts the "Active:" line from lines and returnes it.
CMD = 'systemctl status ' + SERVICE
output = subprocess.check_output([CMD], shell=True, universal_newlines=True)
OUT = output.splitlines()
for line in OUT:
if 'Active:' in line:
return line
def ServiceStatus(LIST):
# Loop the a list of services and prints them neatly.
for elem in LIST:
S = elem + ': '
print(S.rjust(15), StatusFilter(elem.lower()))
# Stores a list of services to check status of.
LIST = ['SSHD','NFtables']
# Spacing and other strings to print
SPACING = '\n' + '='*COLUMNLENGHT +'\n'
SPACING2 = '\n' + '-'*COLUMNLENGHT + '\n'
HEADING = 'Status of installed/configured services:'
# Calles function to loop through desired services to print status.
print(SPACING)
print(HEADING.center(int(COLUMNLENGHT - 20)))
print(SPACING2)
ServiceStatus(LIST)
print(SPACING)
Use Windows Scheduler to remove old files
- Create a schedule in Windows Scheduler
- Add the following script to run:
Save file as .bat
:: Text file formats
ForFiles -p "C:\Users\user\Desktop" -s -m *.txt* /C "cmd /c del @path"
ForFiles -p "C:\Users\user\Desktop" -s -m *.doc* /C "cmd /c del @path"
ForFiles -p "C:\Users\user\Desktop" -s -m *.pdf* /C "cmd /c del @path"
:: txt, doc, odt, rtf, ods, xls, pdf, ppt, odp, pkt, pka, zip.