Search code examples
windowspowershellpasswords

Find local password with PowerShell


I have to set up Scan To Folder. To avoid having to: Create a Scanner user with the password "example123" on Windows Switch the network from public to private Create a folder in the documents and manage sharing permissions Enable SMB 1.0 on Windows Access the web interface of my printer and navigate to the address book to add a new user and paste the path of the folder in my documents

I have done all of this from a PowerShell script. To prevent error messages when creating a Scanner user if one already exists, I want to check if the already created user has the correct password. If not, create Scanner2.

Write-Host @"
 ____  ____  ____                  ____  _____     _     _           
' ////////////////////////////////////////////////////////////////////////////
' //                                                                        //
' //  ____  ____  ____                  ____  _____     _     _             //
' // |  _ \|  _ \/ ___|  ___ __ _ _ __ |___ \|  ___|__ | | __| | ___ _ __   //
' // | | | | | | \___ \ / __/ _` | '_ \  __) | |_ / _ \| |/ _` |/ _ \ '__|  //
' // | |_| | |_| |___) | (_| (_| | | | |/ __/|  _| (_) | | (_| |  __/ |     //
' // |____/|____/|____/ \___\__,_|_| |_|_____|_|  \___/|_|\__,_|\___|_|     //
' //                                                                        //
' ////////////////////////////////////////////////////////////////////////////
"@
                
Set-ExecutionPolicy Bypass -Force

$userName = Read-Host "Display name in address book: "

Write-Host "Here are the available printers with their IP addresses"
$printers = Get-Printer
$printerInfo = @()
foreach ($printer in $printers) {
    $printerName = $printer.Name
    $portName = $printer.PortName
    if ($portName -match 'IP_(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})') {
        $ipAddress = $Matches[1]
        $printerInfo += New-Object PSObject -Property @{
            "Printer Name" = $printerName
            "IP Address" = $ipAddress
        }
    }
}
$printerInfo | Format-Table -AutoSize
Write-Host "If your printer is not listed, please go to google.com."
$printerIP = Read-Host "Enter the IP address of the printer where you want to add ${userName} to the address book: "

if ($printerIP -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}') {
    $userExists = Get-LocalUser -Name "Scanner" -ErrorAction SilentlyContinue

    if ($userExists -and (Test-Path "C:\Users\Scanner") -and (net user Scanner | Select-String -SimpleMatch "Example123")) {
        Write-Host "The 'Scanner' user already exists, and its password is 'Example123'."
    } elseif ($userExists -and (Test-Path "C:\Users\Scanner")) {
        Write-Host "The 'Scanner' user already exists, but its password is not 'Example123'."
        $password = ConvertTo-SecureString "Example123" -AsPlainText -Force
        New-LocalUser -Name "Scanner2" -Password $password -FullName "Scanner2" -Description "Backup account for the scanner"
        $folderScanAccount = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "Scanner2", $password
    } elseif (!$userExists) {
        $password = ConvertTo-SecureString "Example123" -AsPlainText -Force
        New-LocalUser -Name "Scanner" -Password $password -FullName "Scanner" -Description "Account for the scanner"
        $folderScanAccount = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "Scanner", $password
    }

    if ($folderScanAccount) {
        Enable-WindowsOptionalFeature -Online -FeatureName SMB1Protocol -NoRestart
        Disable-WindowsOptionalFeature -Online -FeatureName SMB1Protocol-Client -NoRestart
        Disable-WindowsOptionalFeature -Online -FeatureName SMB1Protocol-Deprecation -NoRestart
        Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update" -Name "RebootRequired" -Value 0 -Force

        $connectionProfiles = Get-NetConnectionProfile

        foreach ($profile in $connectionProfiles) {
            if ($profile.NetworkCategory -eq "Public" -and ($profile.InterfaceAlias -like "*Wi-Fi*" -or $profile.InterfaceAlias -like "*Ethernet*")) {
                Set-NetConnectionProfile -InterfaceIndex $profile.InterfaceIndex -NetworkCategory Private
                Write-Host "The $($profile.InterfaceAlias) profile has been changed to private."
            }
        }

        Write-Host "All Wi-Fi and Ethernet profiles have been changed to private, if applicable."

        $share_name = "Scanner"
        $user_documents = [Environment]::GetFolderPath("MyDocuments")
        $path = Join-Path $user_documents "Scanner"

        if (!(Get-SmbShare | Where-Object { $_.Name -eq $share_name })) {
            if (!(Test-Path -Path $path -PathType Container)) {
                New-Item -Path $path -ItemType Directory
            }

            net share $share_name=$path /grant:"Scanner,FULL" /remark:"Shared for Scanner"
            icacls $path /grant "Scanner:(OI)(CI)RW" /inheritance:e /t
        }

        if (Get-SmbShare | Where-Object { $_.Name -eq $share_name }) {
            Write-Output "$share_name has been successfully shared."

            $UNCPath = "\\$env:COMPUTERNAME\$share_name"

            Install-Module -Name RicohAddressBook

            Add-AddressBookEntry -Hostname $printerIP -Credential $credential -Name $userName -KeyDisplay $userName -Title1 "AB" -FolderScanPath $UNCPath -FolderScanAccount $folderScanAccount
        } else {
            Write-Output "Sharing $share_name failed."
        }
    }
} else {
    Write-Host "The entered IP address is not valid."
}

Read-Host "Press Enter t


When a Scanner user with the good password is already created. It creates Scanner2 anyway

Solution

  • Instead of trying to start a big process such as PowerShell as in the example of @T-Me, I always go for rdpclip.exe instead as it's very light weight and then catches any errors.

    try {
      start-process rdpclip.exe -Credential $Credentials
    }
    catch {
      throw $_.Exception.Message
    }