14 November 2023

Last reboot reason using PowerShell

 Use this PowerShell command, to obtain last restart reasons:

$x = foreach ($e in (Get-WinEvent -FilterHashtable @{logname = 'System'; id = 1074 })) {

    [PSCustomObject]@{

        Time        = $e.TimeCreated

        Type        = $e.Properties.Value[4]

        User        = $e.Properties.Value[6]

        Process     = $e.Properties.Value[0]

        Reason      = $e.Properties.Value[2]

        Description = $e.Properties.Value[5]

    }

}

$x | Out-GridView


27 October 2023

Autofit Excel sheets constantly

While your Excel sheet open, press ALT+F11


and double-click on the Sheet1 to open the blank window


copy this code there

Private Sub Worksheet_Change(ByVal Target As Range)

    Me.Cells.EntireColumn.AutoFit

End Sub


and close the Visual Basic window.

If you make a change to a single cell, all the columns will automatically adjust their width



19 October 2023

Get-WmiObject works on PowerShell 7

 Yeah, a lot of people are saying that you cannot use Get-WmiObject in PowerShell 7 and you must use instead Get-CimInstance. Well, this is true and also not true.


I have PowerShell based WMI scanner to scan all servers in domain to discover disk free space. Some are older Windows versions and Get-CimInstance does not get result back, so I want to use Get-WmiObject -Computername instead.


To enable Get-WmiObject use this command:


Import-Module Microsoft.PowerShell.Management -UseWindowsPowerShell


After that your Get-WmiObject is working

14 April 2023

Automatic update of Chocolatey packages

 If you are installed some software using https://chocolatey.org and want to keep it up to date, then open administrative Command Prompt (cmd.exe as administrator) and paste this line there:

schtasks /create /tn choco-update /tr "cup all -y" /sc daily /st 02:46 /ru SYSTEM /rl HIGHEST

This will create a scheduled task, which runs every day at 02:46 (nighttime) and updates all chocolatey packages (the command "cup all -y" does that).



To see all scheduled tasks, open taskschd.msc and see it for yourself:



To see log file, what actually has happened during last nightly sessions, open this file:

"C:\ProgramData\chocolatey\logs\chocolatey.log"



05 April 2023

Create CAA record using PowerShell in Windows DNS

 Because CAA records are not natively supported in Windows Server DNS service, then you need to do this manually. I created a PowerShell script for that:


$zone = 'yourdomain.com'

$provider = 'letsencrypt.org'

$caa = "00056973737565" + ([BitConverter]::ToString($provider.ToCharArray())).Replace("-", "")

Add-DnsServerResourceRecord -ZoneName $zone -Name '@' -Type 257 -RecordData $caa


This script must be executed in Windows Server, where DNS service is installed and contains your external DNS zone.

Your zone name must be written on the first line and your provider name must be on next line

To verify your CAA record in internet, search for CAA checker page and type there your domain name and hit the Check button.