Skip to content

Commit

Permalink
stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Dottn committed Dec 14, 2017
1 parent 7c5d460 commit b996d66
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 27 deletions.
125 changes: 98 additions & 27 deletions Eksamen.psm1 → AD.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ Standard 'Z'.
Nettverksaddressen til mappen hjemmemappen skal opprettes i.
Brukerens hjemmemappe vil ligge i denne mappen, og ha navn tilsvarende brukerens SAMAccountName.
.PARAMETER IkkeLeggIGruppe
Dersom brukeren ikke skal legges til standardgruppen for avdelingen, benytt denne.
.EXAMPLE
\\TODO
Expand Down Expand Up @@ -101,7 +105,9 @@ function New-CustomADBruker {
HelpMessage = "Adresse til hvor brukerens hjemmemappe skal opprettes. Standard C:\Share\Hjemmemappper."
)]
[string]
$HjemmemappeRot = "C:\Share\Hjemmemappper"
$HjemmemappeRot = "\\Solem-ad\Share\Hjemmemapper",
[switch]
$IkkeLeggIGruppe
)
$Brukernavn = New-Brukernavn $Fornavn $Etternavn
$FulltNavn = "{0} {1}" -f $Fornavn, $Etternavn
Expand All @@ -112,24 +118,88 @@ function New-CustomADBruker {
$Hjemmemappe = "{0}{1}" -f $HjemmemappeRot, $Brukernavn

$Domene = Invoke-Command -Session $Session -ScriptBlock {
"@{0}" -f (Get-ADDomain)
Get-ADDomain
} -ErrorVariable err
$AvdelingOUPath = "OU={0},OU={1},{2}" -f $Avdeling, $Bedrift, $Domene.DistinguishedName

New-ADUser `
-DisplayName $FulltNavn `
-GivenName $Fornavn `
-Surname $Etternavn `
-UserPrincipalName $UPN `
-SamAccountName $Brukernavn `
-AccountPassword $Engangspassord `
-Enabled $true `
-ChangePasswordAtLogon $true `
-Path $AvdelingOUPath `
-Department $Avdeling `
-Company $Bedrift `
-HomeDirectory $Hjemmemappe `
-HomeDrive $HjemmemappeDrev
$AvdelingOUPath = "OU={0},OU={1},{2}" -f $Avdeling, $Bedrift, ($Domene.DistinguishedName)
try {
Invoke-Command -Session $Session -ScriptBlock {
New-ADUser `
-Name using:$FulltNavn `
-DisplayName using:$FulltNavn `
-GivenName using:$Fornavn `
-Surname using:$Etternavn `
-UserPrincipalName using:$UPN `
-SamAccountName using:$Brukernavn `
-AccountPassword using:$Engangspassord `
-Enabled using:$true `
-ChangePasswordAtLogon using:$true `
-Path using:$AvdelingOUPath `
-Department using:$Avdeling `
-Company using:$Bedrift `
-HomeDirectory using:$Hjemmemappe `
-HomeDrive using:$HjemmemappeDrev
} -ErrorAction Stop
Write-Host ("Brukeren {0} ble opprettet for {1}." -f $Brukernavn, $FulltNavn) -ForegroundColor Green

If ( -not $IkkeLeggIGruppe) {
Add-ADGroupMember $Avdeling $UPN
Write-Host ("Brukeren {0} ble medlem av gruppen {1}." -f $Brukernavn, $Avdeling) -ForegroundColor Green
}
}
catch {
Write-Host ("Opprettelse av bruker {0} for {1} feilet. Se feilmelding under." -f $Brukernavn, $FulltNavn) -ForegroundColor Yellow
Write-Host $_.Exeption.Message
}
}

<#
.SYNOPSIS
Oppretter flere brukere fra CSV-fil.
Behøver feltene Fornavn, Etternavn, Avdeling, Engangspassord
.PARAMETER Path
Sti til CSV-fil. Dersom ikke oppgitt, vil en filutforsker dukke opp.
#>
function New-CustomADBrukerFromCSV {
Param (
[Parameter(
Position = 0
)]
[string]
# Sti til CSV-Fil
$Path = "",
[char]
# Separasjonstegn i CSV. Standard ';'.
$Delimiter = ';'
)

# Dersom brukeren ikke oppgir en sti til en CSV-fil, åpne dialogboks.
If ($Path.Length -le 0) {
do {
$CsvFil = New-Object System.Windows.Forms.OpenFileDialog
$CsvFil.Filter =
"csv files (*.csv)|*.csv|txt files (*.txt)|" +
"*.txt|All files (*.*)|*.*"
$CsvFil.Title = "Åpne opp CSV fil som inneholder brukere"
$CsvFil.ShowDialog()
} until ($CsvFil.FileName -ne "")
$Path = $CsvFil.FileName
}

$Brukere = Import-Csv $CsvFil.FileName -Delimiter ";"

foreach ($Bruker in $Brukere) {

# Hent verdier fra CSV-fil og opprett bruker
$Passord = ConvertTo-SecureString $Bruker.Passord -AsPlainText -Force
$Etternavn = $Bruker.Etternavn.Trim()
$Fornavn = $Bruker.Fornavn.Trim()
$Avdeling = $Bruker.Avdeling.Trim()

New-CustomADBruker $Fornavn $Etternavn $Avdeling $Passord
}
}

<#
Expand Down Expand Up @@ -186,15 +256,17 @@ Function New-Brukernavn {
$GrunnBrukernavn = "{0}{1}" -f $Fornavn, $Etternavn
$Brukernavn = $GrunnBrukernavn
#Kontrollerer om brukernavnet er i bruk
Get-ADUser -Identity $GrunnBrukernavn -ErrorVariable err
Invoke-Command -Session $Session -ScriptBlock {
Get-ADUser -Identity $GrunnBrukernavn
} -ErrorVariable err -ErrorAction SilentlyContinue
$i = 1
#Dersom brukernavnet er i bruk, vil $err.Count være lik 0. Legger til $i, som er monotonisk voksende,
#til et ledig brukernavn er funnet
while ($err.Count -eq 0) {
$Brukernavn = "{0}{1}" -f $GrunnBrukernavn, ($i++)
Invoke-Command -Session $Session -ScriptBlock {
Get-ADUser -Identity $Brukernavn
} -ErrorVariable err
} -ErrorVariable err -ErrorAction SilentlyContinue
}
return $Brukernavn
}
Expand Down Expand Up @@ -239,19 +311,19 @@ function New-UPN {
#Brukerens fulle etternavn.
$Etternavn = (Read-Host "Etternavn")
)
$Fornavn = Format-NorwegianChars $Fornavn.Trim().Replace(' ', '.')
$Etternavn = Format-NorwegianChars $Etternavn.Trim().Replace(' ', '.')
$Fornavn = Format-NorwegianChars ($Fornavn.Trim().Replace(' ', '.'))
$Etternavn = Format-NorwegianChars ($Etternavn.Trim().Replace(' ', '.'))
$Bruker = "{0}.{1}" -f $Fornavn, $Etternavn
$Domene = Invoke-Command -Session $Session -ScriptBlock {
"@{0}" -f (Get-ADDomain).Forest
} -ErrorVariable err
} -ErrorVariable err -ErrorAction SilentlyContinue

$GrunnUPN = "{0}{1}" -f $Bruker, $Domene
$UPN = $GrunnUPN
#Kontrollerer om UPN er i bruk
Invoke-Command -Session $Session -ScriptBlock {
Get-ADUser -Filter {UserPrincipalName -like $GrunnUPN} -ErrorVariable err
} -ErrorVariable err
} -ErrorVariable err -ErrorAction SilentlyContinue
$i = 1
#Dersom UPN er i bruk, vil $err.Count være lik 0. Legger til $i, som er monotonisk voksende,
#til en ledig UPN er funnet
Expand All @@ -269,7 +341,7 @@ function New-UPN {
Bytter ut tegnene ÆØÅæøå med EOAeoa.
#>
function Private:Format-NorwegianChars {
function Format-NorwegianChars {
Param (
[Parameter(
Mandatory = $true,
Expand All @@ -279,8 +351,7 @@ function Private:Format-NorwegianChars {
#Streng som skal normaliseres.
$OriginalString
)
$OriginalString.ToLower()
$FormatertString = $OrigialString.Replace('Æ', 'E').Replace('æ', 'e').Replace('Ø', 'O').Replace('ø', 'o').Replace('Å', 'A').Replace('å', 'a')
$FormatertString = $OriginalString.Replace('Æ', 'E').Replace('æ', 'e').Replace('Ø', 'O').Replace('ø', 'o').Replace('Å', 'A').Replace('å', 'a')
return $FormatertString

}
Expand Down
40 changes: 40 additions & 0 deletions ComputerInformation.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
**********************
Windows PowerShell transcript start
Start time: 20171214173335
Username: SOLEM\Administrator
RunAs User: SOLEM\Administrator
Machine: SOLEM-AD (Microsoft Windows NT 10.0.14393.0)
Host Application: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -Command & 'C:\Users\Administrator\.vscode\extensions\ms-vscode.powershell-1.5.1\scripts\Start-EditorServices.ps1' -EditorServicesVersion '1.5.1' -HostName 'Visual Studio Code Host' -HostProfileId 'Microsoft.VSCode' -HostVersion '1.5.1' -AdditionalModules @('PowerShellEditorServices.VSCode') -BundledModulesPath 'C:\Users\Administrator\.vscode\extensions\ms-vscode.powershell-1.5.1\modules' -EnableConsoleRepl -LogLevel 'Normal' -LogPath 'C:\Users\Administrator\.vscode\extensions\ms-vscode.powershell-1.5.1\logs\1513268506-d086db26-eef4-40f4-ac33-9377c61a6ed91513268496001\EditorServices.log' -SessionDetailsPath 'C:\Users\Administrator\.vscode\extensions\ms-vscode.powershell-1.5.1\sessions\PSES-VSCode-4716-858453' -FeatureFlags @()
Process ID: 6884
PSVersion: 5.1.14393.206
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.14393.206
BuildVersion: 10.0.14393.206
CLRVersion: 4.0.30319.42000
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
**********************
Transcript started, output file is .\ComputerInformation.txt
System Information for: SOLEM-AD
-------------------------------------------------------
Manufacturer: Gigabyte Technology Co., Ltd.
Model: H67MA-USB3-B3
Serial Number:
CPU: Intel(R) Core(TM) i7-2600K CPU @ 3.40GHz
HDD C:
HDD Capacity: 2 047,51GB
HDD Space: 99,08 % Free (2 028,62GB)
HDD E:
HDD Capacity: 3 725,90GB
HDD Space: 99,61 % Free (3 711,30GB)
RAM: 31,98GB
Operating System: Microsoft Windows Server 2016 Datacenter, Service Pack: 0
User logged In:
Last Reboot: 12/14/2017 16:47:46

-------------------------------------------------------
**********************
Windows PowerShell transcript end
End time: 20171214173338
**********************
50 changes: 50 additions & 0 deletions Get-RemoteSystemInformation.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Remote System Information
# Shows hardware and OS details from a list of PCs
# Thom McKiernan 22/08/2014
# Modified Sivert Solem 17/10/2017

$transcriptPath = ".\ComputerInformation.txt"

Start-Transcript $transcriptPath -force

$ArrComputers = Read-Host "Specify the list of PC names. '.' means local system"
Clear-Host
foreach ($Computer in $ArrComputers) {
$computerSystem = Get-WmiObject Win32_ComputerSystem -Computer $Computer
$computerBIOS = Get-WmiObject Win32_BIOS -Computer $Computer
$computerOS = Get-WmiObject Win32_OperatingSystem -Computer $Computer
$computerCPU = Get-WmiObject Win32_Processor -Computer $Computer
$computerHDD = Get-WmiObject Win32_LogicalDisk -ComputerName $Computer -Filter drivetype=3
Write-Host "System Information for: " $computerSystem.Name -BackgroundColor DarkCyan
"-------------------------------------------------------"
"Manufacturer: " + $computerSystem.Manufacturer
"Model: " + $computerSystem.Model
"Serial Number: " + $computerBIOS.SerialNumber
if ($computerCPU.Length -gt 1) {
foreach ($CPU in $computerCPU) {
"CPUID: " + $CPU.DeviceID
"CPU: " + $CPU.Name
}
}
else {
"CPU: " + $computerCPU.Name
}
if ($computerHDD.Length -gt 1) {
foreach ($HDD in $computerHDD) {
"HDD " + $HDD.DeviceID
"HDD Capacity: " + "{0:N2}" -f ($HDD.Size / 1GB) + "GB"
"HDD Space: " + "{0:P2}" -f ($HDD.FreeSpace / $HDD.Size) + " Free (" + "{0:N2}" -f ($HDD.FreeSpace / 1GB) + "GB)"
}
}
else {
"HDD Capacity: " + "{0:N2}" -f ($computerHDD.Size / 1GB) + "GB"
"HDD Space: " + "{0:P2}" -f ($computerHDD.FreeSpace / $computerHDD.Size) + " Free (" + "{0:N2}" -f ($computerHDD.FreeSpace / 1GB) + "GB)"
}
"RAM: " + "{0:N2}" -f ($computerSystem.TotalPhysicalMemory / 1GB) + "GB"
"Operating System: " + $computerOS.caption + ", Service Pack: " + $computerOS.ServicePackMajorVersion
"User logged In: " + $computerSystem.UserName
"Last Reboot: " + $computerOS.ConvertToDateTime($computerOS.LastBootUpTime)
""
"-------------------------------------------------------"
}
Stop-Transcript
8 changes: 8 additions & 0 deletions Logs.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
workflow Get-EventLogEntries {
$lognames = @("Application", "Security", "System")
foreach -parallel ($log in $lognames) {
Get-EventLog -LogName $log -Newest 10 |
Add-Member -NotePropertyName LogName -NotePropertyValue $log -PassThru |
Select-Object -Property Index, TimeGenerated, LogName, EntryType, Source, InstanceId, Message
}
}

0 comments on commit b996d66

Please sign in to comment.