Avatar

Links

michael-sabrnak-swi (corporate)
MyKEms (personal)
@rss
  • If you need to export all available Windows Event Logs, especially in the Windows Server Core edition (without GUI), you can utilize this PowerShell script:

    Created Mon, 05 Jun 2023 11:41:24 +0000
  • Ansible/Packer over WinRM can experience UAC Elevated rights issues if logged over the network.

    To fix the issue:

    # Set LocalAccountTokenFilterPolicy to 1
    $token_path = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System"
    $token_prop_name = "LocalAccountTokenFilterPolicy"
    $token_key = Get-Item -Path $token_path
    $token_value = $token_key.GetValue($token_prop_name, $null)
    if ($token_value -ne 1) {
        Write-Host "Setting LocalAccountTokenFilterPolicy to 1"
        if ($null -ne $token_value) {
            Remove-ItemProperty -Path $token_path -Name $token_prop_name
        }
        New-ItemProperty -Path $token_path -Name $token_prop_name -Value 1 -PropertyType DWORD > $null
    }
    

    How UAC remote restrictions work

    To better protect those users who are members of the local Administrators group, we implement UAC restrictions on the network. This mechanism helps prevent against loopback attacks. This mechanism also helps prevent local malicious software from running remotely with administrative rights.

    Created Tue, 01 Jun 2021 11:12:30 +0000
  • In case you want to get maximal value without rounding/flooring – to be exact you should not use Measure-Object. You need to enumerate array (which is also faster).

    Created Tue, 25 Feb 2020 19:52:08 +0000
  • In case you need to determine what language was Windows OS/Server installed run following script in powershell:

    Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Nls\Language" | Select-Object InstallLanguage
    

    Compare returned HEX value from powershell with this table:

    Created Wed, 26 Jun 2019 13:27:17 +0000
  • Example of the Powershell script which can modify IE LAN Proxy Exception List to keep current settings (for example pulled by GPO) and extend with custom exception list.Most important is to modify first line.

    Created Tue, 26 Feb 2019 11:39:02 +0000
  • This guide explains how to download latest camera record available on UniFi NVR server via Powershell through the REST API with SSL self-signed certificate.

    Created Tue, 19 Feb 2019 09:37:58 +0000
  • Script which will scan all DHCP scopes and report in which is MAC address located (if any).

    # MAC Address to search:
    $MacAddress = "E4-A4-71-4E-21-77"
    
    Import-Module DhcpServer -ErrorAction Stop
    
    Get-DhcpServerv4Scope | ForEach-Object {
    
        # To make static lease uncomment "#" in "#| Add-DhcpServerv4Reservation"
        Get-DhcpServerv4Lease -ScopeId $_.ScopeId | Where-Object { $_.ClientId -eq $MacAddress } #| Add-DhcpServerv4Reservation
    
    }
    

    If you wish to make static lease for MAC address, uncomment the “ | Add-DhcpServerv4Reservation”.

    Created Wed, 14 Mar 2018 11:29:38 +0000
  • Example how to split text into 2 part with Powershell version 2+.

    Example 1:

    #Simple text split based on delimiter
    $text = "Left Part;Right Part"
    $pos = $text.IndexOf(";")
    $leftPart = $text.Substring(0, $pos)
    $rightPart = $text.Substring($pos+1)
    Write-Output $leftPart
    Write-Output $rightPart
    

    Example 2:

    Created Wed, 13 Dec 2017 14:36:57 +0000
  • This article contains Powershell script which will backup Active Directory Group Policy Objects.

    #-----User variables to modify-----#
    $Path = "C:\Backups\GPO-Backup" #Destination backup folder
    $Days = "30" #How many days to keep in backup $Path folder
    #----------------------------------#
    
    #--Execution code (do not touch)---#
    $ActualDate = (Get-Date -Format d.M.yyyy__H_mm_s) #Actual date for folder creation
    $sw = [system.diagnostics.stopwatch]::startNew()
    
    If ((Test-Path -Path $Path)) {
        If (Get-Module -ListAvailable -Name GroupPolicy) {
            $sw.Start()
            Import-Module GroupPolicy -ErrorAction Stop
            New-Item -Path "$Path\$ActualDate" -ItemType directory
            Start-Transcript -Path "$Path\$ActualDate\TranscriptLog.txt" #To Keep GpoId and Id history to better identify GPOs
            Backup-GPO -All -Path "$Path\$ActualDate"
            $DeleteFolders = (Get-ChildItem -Path $Path | Where-Object PSIsContainer -eq "True" | Where-Object CreationTime -le (Get-Date).AddDays(-$Days) ) | Remove-Item -Force -Recurse
            $sw.Stop()
            Write-Output "Finished! Removed backup GPO folders based on '$Days' days retention period. Elapsed time: $($sw.Elapsed.Seconds) seconds."
            Stop-Transcript
        }#EndIf
        
        Else {
            Write-Warning "Script stopped! GroupPolicy module is not installed on this machine!"
        }#EndElse
    
    }#EndIf
    Else {
        Write-Warning "Script stopped! Provided path '$Path' does not exist!"
    }#EndElse
    #----------------------------------#
    
    Created Thu, 06 Jul 2017 17:00:25 +0000
  • Tip how to schedule with Tasks Scheduler Powershell script which contains function we need to run and parameters.

    Scenario:

    • We have Powershell script called MyFunctions.ps1 saved in C:\Scripts
    • We want to run function called Get-MyFiles
    • We have 2 mandatory parameters: Path, TranscriptPath
    • We use 1 global parameter: Verbose

    Our scheduled task will contain in tab Actions following action:

    Created Thu, 06 Jul 2017 16:51:52 +0000
Next