venerdì 14 gennaio 2022

SQL SERVER - Aggiungere un assembly CLR

Per aggiungere un assembly CLR a SQL Server, bisogna seguire questa procedura:
  1. aprire il database appropriato (esempio, DWH_praticoWeb)
  2. andare su "Programmabilità" > "Assembly"
  3. tasto destro, poi "Nuovo Assembly..."
  4. cliccare su "Sfoglia..." e cercare l'assembly CLR da collegare
  5. cliccare su OK
A questo punto è possibile controllare l'avvenuto caricamento con questa query:
SELECT  *
FROM    sys.assembly_modules

SQL SERVER - Abilitare l'integrazione CLR

Per abilitare l'integrazione CLR (che permette di caricare dll e aggiungere funzionalità a SQL Server, bisogna lanciare questa query:
EXEC sp_configure 'clr enabled', 1;
RECONFIGURE;
GO
A questo punto, per controllare che tutto sia andato a buon fine, si utilizza questa query:
SELECT  *
FROM    sys.configurations
WHERE   name = 'clr enabled'

WINDOWS - Recupero del seriale di Windows

Questo script va salvato come productkey.vbs e permette di recuperare il codice seriale di Windows sulla macchina su cui viene eseguito:

Set WshShell = CreateObject("WScript.Shell")
MsgBox ConvertToKey(WshShell.RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\DigitalProductId"))

Function ConvertToKey(Key)
    Const KeyOffset = 52
    i = 28
    Chars = "BCDFGHJKMPQRTVWXY2346789"
    Do
        Cur = 0
        x = 14
        Do
            Cur = Cur * 256
            Cur = Key(x + KeyOffset) + Cur
            Key(x + KeyOffset) = (Cur \ 24) And 255
            Cur = Cur Mod 24
            x = x -1
        Loop While x >= 0
        i = i -1
        KeyOutput = Mid(Chars, Cur + 1, 1) & KeyOutput
        If (((29 - i) Mod 6) = 0) And (i <> -1) Then
            i = i -1
            KeyOutput = "-" & KeyOutput
        End If
    Loop While i >= 0

    ConvertToKey = KeyOutput
End Function

giovedì 13 gennaio 2022

POWERSHELL - Elenco delle utenze su Active Directory

Questo comando powershell permette di recuperare l'elenco delle utenze da Active Directory:

 Get-ADUser -Filter * -SearchBase "DC=dominio,DC=it" | Select UserPrincipalName,DistinguishedName,AccountExpirationDate,Enabled,Created,LastLogonDate,Name,Surname | Export-CSV "C:\UserList.csv" -Encoding UTF8

POWERSHELL - Script di estrazione audio da MKV (con uso di FFMPEG)

Questo script permette di estrarre la traccia audio (in MP3) da un file MKV:
# caricamento e controllo parametri
param([string]$filePath)

# variabili di appoggio
# NOTA: questo va aggiornato con il path effettivo della posizione di FFMPEG (vedi nota sotto nel comando)
$ffmpegPath = "[PATH_DI_FFMPEG]"

try
{
    if (($args.length -eq 0) -and ($filePath.length -eq 0))
    {
        Write-Host "ERRORE: il path del file è obbligatorio." -ForegroundColor Red

        Return
    }
    else
    {
        if ($filePath.length -eq 0)
        {
            if ($args.length -eq 1)
            {
                # stampa dell'help
                if (($args[0] -eq "-h") -or ($args[0] -eq "-help"))
                {
                    Write-Host "DESCRIPTION"
                    Write-Host "    Estrae un file audio in formato MP3 da un file MKV tramite FFMPEG."
                    Write-Host ""
                    Write-Host "PARAMETERS"
                    Write-Host "    -fp, -filePath <String>"
                    Write-Host "        Percorso del file da elaborare."
                    Write-Host ""
                    Write-Host "    -h, -help"
                    Write-Host "        Stampa la guida all'utilizzo."
                }
                else
                {
                    Write-Host "ERRORE: parametro '$($args[0])' senza un valore specificato." -ForegroundColor Red
                }

                Return
            }
            elseif (($args.length -eq 2) -and ($args[0] -ne "-fp"))
            {
                Write-Host "ERRORE: parametro '$($args[0])' non riconosciuto." -ForegroundColor Red

                Return

            }
            elseif (($args.length -eq 2) -and ($args[0] -eq "-fp"))
            {
                $filePath = $args[1]
            }
            else
            {
                Write-Host "ERRORE: troppi parametri specificati." -ForegroundColor Red

                Return
            }
        }

        # controllo presenza di FFMPEG
	$ffmpegExists = Test-Path -Path "$($ffmpegPath)\ffmpeg.exe" -PathType Leaf

        # controllo la correttezza del path passato come parametro e se esiste il file
        $fileExists = Test-Path -Path $filePath -PathType Leaf

        # errore
	if ($ffmpegExists -ne $true)
	{
            Write-Host "ERRORE: FFMPEG non trovato. Controllare il path dell'eseguibile." -ForegroundColor Red

            Return
	}
        elseif ($fileExists -ne $true)
        {
            Write-Host "ERRORE: il file '$($filePath)' non esiste." -ForegroundColor Red

            Return
        }
        else
        {
            # recupero le informazioni sul file (basename + extension)
            $inputFile = Get-ChildItem $filePath

            # controllo l'estensione MKV
            if ($inputFile.Extension -ne "mkv" - $inputFile.Extension -ne ".mkv")
            {
                Write-Host "ERRORE: il file '$($inputFile.Name)' non ha estensione MKV." -ForegroundColor Red
            }
            else
            {
                # conversione del file
                Write-Host "Conversione del file '$($inputFile.Name)' in corso..."

                $cmd = "$($ffmpegPath)\ffmpeg.exe -i ""$($inputFile.Name)"" -ac 2 -ab 192000 -ar 44100 ""$($inputFile.BaseName).mp3"""

                Write-Host "$($cmd)" -ForegroundColor Green

                Invoke-Expression $cmd

                Write-Host ".. conversione del file completata."
            }
        }
    }
}
catch
{
    Write-Host "CATCH: errore generico." -ForegroundColor Red
}