mail@pastecode.io avatar
2 months ago
1.8 kB
# Load the assembly
Add-Type -AssemblyName System.DirectoryServices.AccountManagement

# Specify the path to the Excel file
$excelFilePath = "C:\test\Input2.xlsx"

# Specify the path for the results file
$resultsFilePath = "C:\Users\dpapadopoulos\Desktop\resultsdpapadopoulos.txt"

# Create an Excel object
$excel = New-Object -ComObject Excel.Application

# Open the Excel file
$workbook = $excel.Workbooks.Open($excelFilePath)

# Get the first worksheet
$worksheet = $workbook.Worksheets.Item(1)

# Get the last used row in column A
$maxRow = $worksheet.Cells.Item($worksheet.Rows.Count, 1).End(-4162).Row

# Create or overwrite the results file
$resultsFile = New-Item -ItemType File -Path $resultsFilePath -Force

# Iterate through each row in the Excel file
for ($row = 1; $row -le $maxRow; $row++) {
    # Read username and password from Excel
    $username = $worksheet.Cells.Item($row, 1).Text
    $password = $worksheet.Cells.Item($row, 1).Text  # Assuming username and password are in the same column

    # Create a context to connect to Active Directory
    $context = New-Object System.DirectoryServices.AccountManagement.PrincipalContext('Domain', 'vionet')  # Replace 'vionet' with your actual domain name

    # Check if the credentials are valid
    $isValid = $context.ValidateCredentials($username, $password)

    # Output result to console
    if ($isValid) {
        Write-Host "Right - $username"
        # Append the result to the results file
        Add-Content -Path $resultsFile.FullName -Value "Right - $username"
    } else {
        Write-Host "Wrong - $username"

# Close Excel
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
Leave a Comment