08 August 2022

Read IIS log file using PowerShell

 Actually you need just few lines

$file = '\\iisserver\c$\inetpub\logs\LogFiles\W3SVC1\u_ex220808.log'
$headers = (Get-Content $file | Select-Object -Skip 3 -First 1).Split(' ') | Select-Object -Skip 1
$result = Import-Csv $file ' ' -Header $headers | Where-Object date -NotLike '#*'
$result | ConvertTo-Csv -Delimiter `t -NoTypeInformation | Set-Clipboard

Output will be in clipboard (in this case it will be with tab separated for easy pasting into Excel or similar)

21 June 2022

Resize partition using PowerShell

 Resize using this command line (PowerShell as Administrator):

Get-Partition -DriveLetter c | Resize-Partition -Size 96GB

21 February 2022

Start service on remote machine using PowerShell

If you just want remotely use mmc, then start services.msc and connect to remote computer

If you want to use PowerShell, use the following command (atagateway is one example service and serverX is example server name):

Get-Service atagateway -ComputerName serverX | Set-Service -Status running

If you have remote PowerShell enabled, then you can also use this command:

Invoke-Command serverX {Start-Service atagateway}

03 February 2022

Upload domain joined computer bitlocker key to Active Directory

To upload already encrypted drive bitlocker key to AD, you can use this PowerShell oneliner. Remember to use "run as administrator"

Backup-BitLockerKeyProtector c: ((Get-BitLockerVolume c:).KeyProtector | ? KeyProtectorType -eq RecoveryPassword).KeyProtectorId

If you already have the information in AD, then it doesn't hurt, it will not create any duplicates in AD


08 December 2021

Get all subnets in you Active Directory

 To get all subnets in your Active Directory, use this PowerShell oneliner:

[System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Sites.Subnets.Name

16 July 2021

Get Windows 10 saved WiFi passwords using PowerShell

I wrote an under-100-character oneliner for getting all saved WiFi passwords on Windows 10 computer:


netsh wl sh p|%{($_-split': ')[1]}|%{$_;netsh wl sh p n=$_ k=clear|%{($_-split'nt\s+: ')[1]};''}


Ouput will be in the format:


SSID (network name)
WPA key (network password)


Each combo will be separated with an empty line. If some WiFi networks haven't got password, then only SSID will be shown.



09 April 2021

Shrink WSUS database

I discovered, that WSUS database and log file are very huge (WSUS.mdf and WSUS_log.ldf). Here's what I did to reduce the size:

Facts:
1. WSUS uses Windows Internal Database
2. To do some command against this database engine, you need SQL server management studio
3. It's very easy to install SSMS using Chocolatey
4. It's very easy to install Chocolatey using powershell
5. PowerShell might need some adjustments

Actual To-Do list:
1a. install Chocolatey (open PowerShell as an administrator)
iwr https://chocolatey.org/install.ps1 -UseBasicParsing | iex
1b. install Chocolatey (open cmd as an administrator)
powershell -exe unrestricted "iwr https://chocolatey.org/install.ps1 -UseBasicParsing | iex"
2. If this command throws certificate error, please use his command before (also in PowerShell)
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12


3. Now open cmd.exe as Administrator and install SSMS
cinst sql-server-management-studio -y


4. Next thing is to open SSMS as system (you might need to restart computer before opening SSMS).
psexec -s -i cmd
and in the opened windows paste the path to the ssms executable
"C:\Program Files (x86)\Microsoft SQL Server Management Studio 18\Common7\IDE\Ssms.exe"


5. If you don't have psexec installed, use this line to install PSTools
cinst pstools -y


6. To connect to WID, you need to connect to this "server":
np:\\.\pipe\MICROSOFT##WID\tsql\query


7. To shrink all databases, use this query in the New Query window
EXEC sp_MSForEachDB 'select ''?'' as [Database]; ALTER DATABASE [?] SET RECOVERY SIMPLE; DBCC SHRINKDATABASE (''?'' , 0)'


Profit. 40GB more free disk space