Author Archive

Windows – Export list of ACL Permission on folder and files to a csv

dir D:\Shares\Shared | ForEach-Object {
    # Try/catch here would let you save the path to files/folders that you can't view...
    $_ | Get-Acl | select @{N="Path"; E={Convert-Path $_.Path}} -ExpandProperty Access
} | Export-Csv D:\Shares\Shared\ntfs_perms.csv -NoTypeInformation

Send WOL (wake on LAN) with powershell

This powershell function is not mine, but I kept it as I found it useful from time to time.

function Send-WOL
{
<# 
  .SYNOPSIS  
    Send a WOL packet to a broadcast address
  .PARAMETER mac
   The MAC address of the device that need to wake up
  .PARAMETER ip
   The IP address where the WOL packet will be sent to
  .EXAMPLE 
   Send-WOL -mac 00:11:32:21:2D:11 -ip 192.168.8.255 
#>
 
[CmdletBinding()]
param(
[Parameter(Mandatory=$True,Position=1)]
[string]$mac,
[string]$ip="255.255.255.255", 
[int]$port=9
)
$broadcast = [Net.IPAddress]::Parse($ip)
  
$mac=(($mac.replace(":","")).replace("-","")).replace(".","")
$target=0,2,4,6,8,10 | % {[convert]::ToByte($mac.substring($_,2),16)}
$packet = (,[byte]255 * 6) + ($target * 16)
  
$UDPclient = new-Object System.Net.Sockets.UdpClient
$UDPclient.Connect($broadcast,$port)
[void]$UDPclient.Send($packet, 102) 
 
}

Windows AD – Get list of users, enabled status, and time of last logon. (PowerShell)

Get-ADUser -Filter * -Properties * | `
    Select-Object Name,Enabled, @{Name="Last Successful Logon";Expression={[datetime]::FromFileTime($_.'lastLogonTimeStamp')}} | `
    Sort-Object "Last Successful Logon"

Office 365 – Get distribution group members and output to csv (PowerShell)

#View a list of groups
Get-Group
#Set GroupName before running
Get-DistributionGroupMember -Identity "GroupName"| Export-Csv C:\Users\username\Desktop\groups.csv

Remove unused PNP drivers from system.

This will remove drivers of working, but disconnected devices. (e.g. USB Drives Printers, old video devices, etc).

FOR /L %G in (1,1,999) do PnPutil.exe -D oem%G.inf

For further cleanup: I recommend Device Cleanup Tool from Uwe Sieber. You can get it from their website.
https://www.uwe-sieber.de/english.html

Virtualbox – Set EFI Resolution to 1920×1080. Useful for MacOS, and other EFI based Guest VMs

VBoxManage setextradata “Name of Guest VM” VBoxInternal2/EfiGraphicsResolution 1920x1080

Send windows event log with HTML formatting (PowerShell)

This example sends Windows Backup logs

$Yesterday = (Get-Date) - (New-TimeSpan -Day 1)
$logs = Get-WinEvent -logname Microsoft-Windows-Backup | Where-Object {$_.TimeCreated -ge $Yesterday} 
$username = "user@domain.com"
$password = "mypassword"
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
$errorCount = 0
foreach($Event in $logs)
{
$Errorlevel=$Event.LevelDisplayName
    if ($Errorlevel -eq "Error")
        {
        $errorCount++
        }
}


$body = "<html>"
$style = "<style>BODY{background-color:white;}.error{color: red;font-weight: bold;}table, th, td{border: 1px solid black;border-collapse: collapse;padding: 5px;}</style>"
$head = "<head>$style</head>"
$body += $head
$body += "<body>"
$body += "<h2>$env:computername Backup Logs</h2>"
if ($errorCount -gt 0)
    {
    $body += "<h3 class=""error"">$errorCount Errors</h3>"
    }
$body += "<table><tr><td>Time</td><td>Error Level</td><td>EventId</td><td>Message</td></tr>"
        # Get the event Log Details
        foreach($Event in $logs)    
        {
            $Time=$Event.TimeCreated
            $Errorlevel=$Event.LevelDisplayName
            $EventId=$Event.Id
            $Message=$Event.Message
            $TableLine = "<tr><td>$Time</td><td>$Errorlevel</td><td>$EventId</td><td>$Message</td></tr>"
            if($Errorlevel -eq "Error")
            {
                 
                $TableLine = "<tr class=""error""><td>$Time</td><td>$Errorlevel</td><td>$EventId</td><td>$Message</td></tr>"
            }
                                  
            $body += $TableLine
        }
$body += "</table>"
$body += "</body>"
$body += "</html>"
#echo $body
 
$subject = "$env:computername Backup Logs"
if ($errorCount -gt 0)
    {
    $subject += " - $errorCount Errors"
    }
 
Send-MailMessage -BodyAsHTML $body `
-From me@domain.com `
-SmtpServer mail.server.com`
-Subject $subject `
-To "me@domain.com" `
-UseSsl -Credential $cred

Send Windows Event Logs as an e-mail. (PowerShell)

This script can be used to send windows event logs on period basis to specific e-mail. Below example sending last 24 hours of events in the Windows Essential Licensing section.

$Yesterday = (Get-Date) - (New-TimeSpan -Day 1)
$logs = Get-WinEvent -LogName "Microsoft-Windows-Server Infrastructure Licensing/Operational" | Where-Object {$_.TimeCreated -ge $Yesterday} | Format-List | Out-String
$username = "user@domain.com"
$password = "mypassword"
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
#Send-MailMessage [-Attachments ] [-Bcc ] [[-Body] ] [-BodyAsHtml]
# [-Encoding ] [-Cc ] [-DeliveryNotificationOption ]
# -From  [[-SmtpServer] ] [-Priority ] [-Subject]  [-To] 
# [-Credential ] [-UseSsl] [-Port ] []
Send-MailMessage -Body $logs `
-From me@domain.com `
-SmtpServer mail.server.com `
-Subject "Licensing Logs" `
-To "user@domain.com" `
-UseSsl -Credential $cred

Disable Windows Defender

Reboot will be required after adding this to registry
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows Defender" /f /v DisableAntiSpyware /t REG_DWORD /d 0x00000001

Mapping iSCSI disks in VirtualBox

To add iSCSI disk to already made VirtualBox machine do the following from a terminal/Command Prompt
VBoxManage storageattach MyVirtualMachineNameOrUUID --storagectl "SATA" --port 0 --device 0 --type hdd --medium iscsi --server 10.10.10.7 --target "iqn.2004-04.com.nas:target-name" --tport 3260

MyVirtualMachineNameOrUUID = The Name of your Virtual Machine. Use quotes if your VM name has spaces
–storagectl “SATA” = Name of the controller under the storage tab in VirtualBox
–port 0 = Port Number. If you already created a virtual disk connected to the SATA controller, probably want to select a different virtual sata port.
–server 10.10.10.7 = ip address of the iSCSI server
–target “iqn.2004-04.com.nas:target-name” = Full target name
–tport 3260 = iSCSI server port number.