Compare two folders with Powershell

This script checks first that folders exist, then that filename are the same, and then that file Hash is the same

#$folder1 = "C:\Temp\test"
#$folder2 = "C:\Temp\test2"


if ((Test-Path -Path $folder1) -and (Test-Path -Path $folder2)) {
    echo "The folders exist"
    echo "Folder1: $folder1"
    echo "Folder2: $folder2"
    $sourceFiles = Get-ChildItem $folder1 -Recurse
    $destFiles = Get-ChildItem $folder2 -Recurse
    if (Compare-Object $sourceFiles.Name $destFiles.Name) {
        echo "The folders is not the same"
    } else {
        echo "Check of the folders show us that there have the same content - OK"
        $SourceDocs = Get-ChildItem –Path $folder1 -Recurse | foreach  {Get-FileHash –Path $_.FullName}
        $DestDocs = Get-ChildItem –Path $folder2 -Recurse | foreach  {Get-FileHash –Path $_.FullName}
        if ($SourceDocs.Hash -ne $destDocs.Hash) {
            echo "There are difference in the the files"
            echo "folder1 Hash: $SourceDocs.hash"
            echo "folder2 Hash: $DestDocs.hash"
        } else  {
            echo "The folders are the same!"
        }
    }
} else {
    Echo "One of the folders or both, dosn't exist"
}

MSSQL Backup History Query

To see MSSQL history Query, run this SQL query. It will show all backups between the 1 january 2021 and 1 february 2021, order by Database and then Last backup time.

It will also show if the backup is a CopyOnly job.

;WITH CTE_Backup AS
(
SELECT  database_name,is_copy_only,backup_start_date,type,physical_device_name
       ,Row_Number() OVER(PARTITION BY database_name,BS.type
        ORDER BY backup_start_date DESC) AS RowNum
FROM    msdb..backupset BS
JOIN    msdb.dbo.backupmediafamily BMF
ON      BS.media_set_id=BMF.media_set_id
AND		BS.backup_start_date >= '01/01/2021'
AND		BS.backup_start_date <= '02/02/2021'
)
SELECT      D.name
           ,ISNULL(CONVERT(VARCHAR,backup_start_date),'No backups') AS last_backup_time
           ,D.recovery_model_desc
           ,state_desc,
            CASE WHEN type ='D' THEN 'Full database'
            WHEN type ='I' THEN 'Differential database'
            WHEN type ='L' THEN 'Log'
            WHEN type ='F' THEN 'File or filegroup'
            WHEN type ='G' THEN 'Differential file'
            WHEN type ='P' THEN 'Partial'
            WHEN type ='Q' THEN 'Differential partial'
            ELSE 'Unknown' END AS backup_type
           ,physical_device_name,CTE.is_copy_only
FROM        sys.databases D
LEFT JOIN   CTE_Backup CTE
ON          D.name = CTE.database_name

ORDER BY    D.name,last_backup_time

Problems with running WMI Request from domain member to workgroup machine

Today, I have the problem that I could not run WMI request from a Domain join member server, to a standalone workgroup server.

It keeps telling me Access Denied.

It turn out that:
In a workgroup, the account connecting to the remote computer is a local user on that computer. Even if the account is in the Administrators group, UAC filtering means that a script runs as a standard user.

That is a good link to refer to. It is about Remote UAC and WMI. The solution is to go to the following registry key – HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System – and create a new name called LocalAccountTokenFilterPolicy (of type DWORD_32) with a value of 1. (Check the Serverfault post for one more solution if you are using a Server 2012).

VMWare – Certificate Status Trigger alarm Alert

Back in December i replaced my Certificate on my VMWare Appliance with a our new Wildcard certificate.
But here in January, i got an alarm saying “Certificate Alarm” and it trigger it as an Alert.

Searching around for a solution, on what certificate that expire soon, i found this command:

for i in $(/usr/lib/vmware-vmafd/bin/vecs-cli store list); do echo STORE $i; /usr/lib/vmware-vmafd/bin/vecs-cli entry list --store $i --text | egrep "Alias|Not After"; done

It showing me that a backup mackine certificate is expiring

STORE MACHINE_SSL_CERT
Alias : __MACHINE_CERT
            Not After : Jan 15 14:20:36 2022 GMT
STORE TRUSTED_ROOTS
Alias : b4a1550d09628bfe628d2685fe24f2b67afb9855
            Not After : Jan 31 10:55:48 2025 GMT
Alias : 7a1f6edf1e16498c4eff9190fa5ef6742bbdde0e
            Not After : Dec  4 14:03:36 2026 GMT
Alias : 909c4328a37dcff3d72de88c51df6f63600fc3f3
            Not After : Dec  4 14:03:36 2026 GMT
Alias : 98c6a8dc887963ba3cf9c2731cbdd3f7de05ac2d
            Not After : Nov  6 12:23:33 2027 GMT
Alias : 91c489ecb94b64e2eeffe438a0ad3fc1465959a2
            Not After : Jan 15 14:20:36 2022 GMT
Alias : 4c27431717565a3a07f3e6d0032c4258949cf9ec
            Not After : Feb 20 10:00:00 2024 GMT
Alias : b1bc968bd4f49d622aa89a81f2150152a41d829c
            Not After : Jan 28 12:00:00 2028 GMT
STORE TRUSTED_ROOT_CRLS
Alias : bcd1699f713eabf1dbd8a21618ea6b0487adbdc8
STORE STS_INTERNAL_SSL_CERT
Alias : __MACHINE_CERT
            Not After : Dec  4 14:03:36 2026 GMT
STORE machine
Alias : machine
            Not After : Jan 31 10:55:48 2025 GMT
STORE vsphere-webclient
Alias : vsphere-webclient
            Not After : Jan 31 10:55:48 2025 GMT
STORE vpxd
Alias : vpxd
            Not After : Jan 31 10:55:48 2025 GMT
STORE vpxd-extension
Alias : vpxd-extension
            Not After : Jan 31 10:55:48 2025 GMT
STORE SMS
Alias : sms_self_signed
            Not After : Feb  3 11:51:48 2025 GMT
STORE BACKUP_STORE
Alias : bkp___MACHINE_CERT
            Not After : Jan 20 12:00:00 2021 GMT
Alias : bkp_machine
            Not After : Jan 31 10:55:48 2025 GMT
Alias : bkp_vsphere-webclient
            Not After : Jan 31 10:55:48 2025 GMT
Alias : bkp_vpxd
            Not After : Jan 31 10:55:48 2025 GMT
Alias : bkp_vpxd-extension
            Not After : Jan 31 10:55:48 2025 GMT

So i exported this Machine certificate to a CRT file, and then deleted it with these commands

mkdir /certificates

/usr/lib/vmware-vmafd/bin/vecs-cli entry getcert --store BACKUP_STORE --alias bkp___MACHINE_CERT --output /certificates/old_machine.crt

/usr/lib/vmware-vmafd/bin/vecs-cli entry delete --store BACKUP_STORE --alias bkp___MACHINE_CERT -y

It then tell me that the certificate have been deleted

Deleted entry with alias [bkp___MACHINE_CERT] in store [BACKUP_STORE] successfully

Also running this command again

for i in $(/usr/lib/vmware-vmafd/bin/vecs-cli store list); do echo STORE $i; /usr/lib/vmware-vmafd/bin/vecs-cli entry list --store $i --text | egrep "Alias|Not After"; done

Show that the certificate have now gone.

Intune Exchange Connector version is not supported

Microsoft have public this article: https://support.microsoft.com/da-dk/help/4464186/microsoft-intune-exchange-connector-version-not-supported, that tell you to just download a new version from Microsoft Intune Admin portal.

But this warning messages could also happen if you allready have a connector installed on another server, even if microsoft says that the new connector will just replace the old one.
So if you get this warning, just after you have downloaded the software, and you have an old connector, delete the old one first.
Remember to check notice of what User Notification and Unmanaged device access where set to before deleting the old one

List all shares folders and thier permissions

<#
            .SYNOPSIS
            This script will list all shares on a computer, and list all the share permissions for each share.

            .DESCRIPTION
            The script will take a list all shares on a local or remote computer.

            .PARAMETER Computer
            Specifies the computer or array of computers to process

            .INPUTS
            Get-SharePermissions accepts pipeline of computer name(s)

            .OUTPUTS
            Produces an array object for each share found.

            .EXAMPLE
            C:\PS> .\Get-SharePermissions # Operates against local computer.

            .EXAMPLE
            C:\PS> 'computerName' | .\Get-SharePermissions

            .EXAMPLE
            C:\PS> Get-Content 'computerlist.txt' | .\Get-SharePermissions | Out-File 'SharePermissions.txt'

            .EXAMPLE
            Get-Help .\Get-SharePermissions -Full
#>

# Written by BigTeddy November 15, 2011
# Last updated 9 September 2012
# Ver. 2.0
# Thanks to Michal Gajda for input with the ACE handling.
[cmdletbinding()]

param([Parameter(ValueFromPipeline=$True,
    ValueFromPipelineByPropertyName=$True)]$Computer = '.')

$shares = gwmi -Class win32_share -ComputerName $computer | select -ExpandProperty Name
foreach ($share in $shares) {
    $acl = $null
    Write-Host $share -ForegroundColor Green
    Write-Host $('-' * $share.Length) -ForegroundColor Green
    $objShareSec = Get-WMIObject -Class Win32_LogicalShareSecuritySetting -Filter "name='$Share'"  -ComputerName $computer
    try {
        $SD = $objShareSec.GetSecurityDescriptor().Descriptor
        foreach($ace in $SD.DACL){
            $UserName = $ace.Trustee.Name
            If ($ace.Trustee.Domain -ne $Null) {$UserName = "$($ace.Trustee.Domain)\$UserName"}
            If ($ace.Trustee.Name -eq $Null) {$UserName = $ace.Trustee.SIDString }
            [Array]$ACL += New-Object Security.AccessControl.FileSystemAccessRule($UserName, $ace.AccessMask, $ace.AceType)
            } #end foreach ACE
        } # end try
    catch
        { Write-Host "Unable to obtain permissions for $share" }
    $ACL
    Write-Host $('=' * 50)
} # end foreach $share

MSSQL SP_WHO2 order by db / databases

Order sp_who2, after database with this commands:

declare @tempTable table (SPID INT,Status VARCHAR(255),
Login VARCHAR(255),HostName VARCHAR(255),
BlkBy VARCHAR(255),DBName VARCHAR(255),
Command VARCHAR(255),CPUTime INT,
DiskIO INT,LastBatch VARCHAR(255),
ProgramName VARCHAR(255),SPID2 INT,
REQUESTID INT);

 INSERT INTO @tempTable
EXEC sp_who2 

select *
from @tempTable order by DBName