lunes, 23 de junio de 2025

 Purpose

Search my shared resources for the words "contraseña|password|contrasenya" and report it to me in a file.

I'm looking for words to ask the user to save their passwords in a secure place like Keepass.

Steps

# Función para obtener el propietario de un archivo o carpeta
function Get-Owner {
    param (
        [string]$Path
    )
    $acl = Get-Acl -Path $Path
    $owner = $acl.Owner
    return $owner
}
# Función para obtener los permisos de escritura de un archivo o carpeta
function Get-WritePermissions {
    param (
        [string]$Path
    )
    $acl = Get-Acl -Path $Path
    $permissions = @()
    foreach ($access in $acl.Access) {
        if ($access.FileSystemRights -match "Write") {
            $permissions += $access.IdentityReference
        }
    }
    return $permissions -join ", "
}
# Crear el archivo CSV y añadir encabezados
$csvPath = "resultado.csv"
"Nombre,RutaCompleta,RutaRelativa,Propietario,PermisosDeEscritura" | Out-File -FilePath $csvPath -Encoding UTF8
# Función para recorrer la estructura de directorios de una ruta de red
function Search-Path {
    param (
        [string]$NetworkPath
    )
    Get-ChildItem -Path $NetworkPath -Recurse | ForEach-Object {
        if ($_ -match "contraseña|password|contrasenya") {
            $owner = Get-Owner -Path $_.FullName
            $writePermissions = Get-WritePermissions -Path $_.FullName
            $relativePath = $_.FullName.Substring($NetworkPath.Length)  # Obtener la ruta relativa
            $result = "$($_.Name),$($_.FullName),$relativePath,$owner,$writePermissions"
            $result | Out-File -FilePath $csvPath -Append -Encoding UTF8
        }
    }
}
# Recorrer las rutas de red
Search-Path -NetworkPath "\\server1\d$"
Search-Path -NetworkPath "\\server2\r$"
Search-Path -NetworkPath "\\server4\r$"
Write-Host "El archivo resultado.csv ha sido creado con éxito."

 By GoN | Published: Jun 2025 | Last Updated:

No hay comentarios: