使用 PowerShell 查找本地密码

问题描述 投票:0回答:2

我必须设置“扫描到文件夹”。 为了避免必须: 在 Windows 上使用密码“example123”创建扫描仪用户 将网络从公共网络切换为私有网络 在文档中创建文件夹并管理共享权限 在 Windows 上启用 SMB 1.0 访问我的打印机的 Web 界面并导航到地址簿以添加新用户并将文件夹的路径粘贴到我的文档中

我已经通过 PowerShell 脚本完成了所有这些工作。为了防止在创建扫描仪用户(如果已存在)时出现错误消息,我想检查已创建的用户是否具有正确的密码。如果没有,请创建 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
windows powershell passwords
2个回答
1
投票

我不知道他们为什么这么恨你。但是,您的问题可以使用一些点和逗号。

您不会从

net user Scanner
获得密码。它不是以纯文本形式存储的。相反,您可以尝试以用户身份启动一个进程,以检查它是否具有您知道的密码。

从您已有的

[SecureString]
创建一个凭证对象,并在
Start-Process
中使用它。如果失败,则密码不正确(或者您没有启动进程的权限。先测试一下,不要
-ErrorAction SilentlyContinue

$credentials = [System.Management.Automation.PSCredential]::new("$env:COMPUTERNAME\Scanner",$password)    
$TestShell = Start-Process powershell.exe -Credential $credentials -ArgumentList '-command "exit"' -PassThru -ErrorAction SilentlyContinue
if($TestShell){'yay I know the password'}

0
投票

我总是选择

rdpclip.exe
,而不是尝试启动一个大进程,例如@T-Me 示例中的 PowerShell,因为它的重量非常轻,然后可以捕获任何错误。

try {
  start-process rdpclip.exe -Credential $Credentials
}
catch {
  throw $_.Exception.Message
}
© www.soinside.com 2019 - 2024. All rights reserved.