簡體   English   中英

需要使用powershell登錄多個本地用戶/密碼相同的IP no Prompt

[英]Need to use powershell to login to multiple IP with the same local user / password no Prompt

我的任務是檢查多台服務器上的用戶名和密碼並報告結果。 所以基本上我有一個 IP 列表,我希望我有相同的用戶和密碼,需要查看哪些身份驗證和哪些不。

這是我到目前為止發現的,但它似乎不起作用,它每次都提示我輸入密碼。

$listofServers = Import-Csv '.\Windows Servers.csv'
$username = username
$password = password
foreach($server in $listofServers.ip)
            {

            try{
            $Credentials = Get-Credential $server\$username $password
                }
                Catch
                {
                $errorMsg = $_.Exception.Message
                }
            }

由於我們下面的線程,更新刪除以前的答案。

這是你的問題。

由於 Windows 安全邊界的正常工作方式,您無法按照您嘗試的方式做事,而且您還必須正確設置 PowerShell 遠程處理。

您還沒有說明您是在域中還是在工作組場景中。 因此,之前關於正確設置 PSRemoting 的聲明。

因為您使用的是本地帳戶,這就像在 PSRemoting 中使用工作組模式一樣,需要額外的設置。 請參閱有關“TrustedHosts”的詳細信息。

about_Remote_Troubleshooting - PowerShell | 微軟文檔

在嘗試登錄遠程主機時,會按設計彈出登錄對話框 (Gina)。

在您的管理機器上,以這種方式嘗試......(測試並驗證為有效)

### PSRemoting - using local machine accounts
$Creds = Get-Credential -Credential $env:USERNAME

Enable-PSRemoting -SkipNetworkProfileCheck -Force
Get-Item -Path 'WSMan:\localhost\Client\TrustedHosts'
Set-Item -Path 'WSMan:\localhost\Client\TrustedHosts' -Value '*' -Force
Get-Item -Path 'WSMan:\localhost\Client\TrustedHosts'

$TargetList = 'Lab01','172.0.0.10'

$TargetList | 
ForEach-Object {
    "Validating logon for $($Creds.Username) on target $PSItem"
    Try 
    {
        New-PSSession -ComputerName $PSItem -Credential $Creds
        Get-PSSession | Remove-PSSession
    }
    Catch {$PSItem.Exception.Message}
}

Set-Item -Path 'WSMan:\localhost\Client\TrustedHosts' -Value '' -Force
# Results
<#
Validating logon for postanote on target Lab01

 Id Name            ComputerName    ComputerType    State         ConfigurationName     Availability
 -- ----            ------------    ------------    -----         -----------------     ------------
 26 WinRM26         Lab01           RemoteMachine   Opened        Microsoft.PowerShell     Available...

Validating logon for postanote on target 172.0.0.10
 27 WinRM27         172.0.0.10      RemoteMachine   Opened        Microsoft.PowerShell     Available...
#>

更新

這是對 Doug 和我的方法的調整,並且完全不需要讓 PSRemoting 適當參與。 再次經過測試和驗證。 除了一次性提示憑據外,無需嵌入純文本密碼/文件/注冊表/CredMan 存儲和檢索等。

$Creds = Get-Credential -Credential $env:USERNAME
$TargetList | 
ForEach-Object {
    "Processing hostname $PSItem"
    Try 
    {
        $cred = New-Object System.Management.Automation.PSCredential ("$PSItem\$($Creds.UserName)", $Creds.Password)
        (Get-HotFix -ComputerName $PSItem -Credential $cred)[0]
    }
    Catch {$PSItem.Exception.Message}
}
# Results
<#
Processing hostname Lab01

Source        Description      HotFixID      InstalledBy          InstalledOn               
------        -----------      --------      -----------          -----------               
Lab01          Update           KB4576478     NT AUTHORITY\SYSTEM  9/10/2020 12:00:00 AM     
Processing hostname 172.0.0.10
Lab01          Update           KB4576478     NT AUTHORITY\SYSTEM  9/10/2020 12:00:00 AM
#>

• 提示:使用 Windows PowerShell 遠程工作,無需使用 Remoting 或 WinRM https://technet.microsoft.com/en-us/library/ff699046.aspx

 Some cmdlets have a –ComputerName parameter that lets you work with a remote computer without using Windows PowerShell remoting. This means you can use the cmdlet on any computer that is running Windows PowerShell, even if the computer is not configured for Windows PowerShell remoting. These cmdlets include the following: • Get-WinEvent • Get-Counter • Get-EventLog • Clear-EventLog • Write-EventLog • Limit-EventLog • Show-EventLog • New-EventLog • Remove-EventLog • Get-WmiObject • Get-Process • Get-Service • Set-Service • Get-HotFix • Restart-Computer • Stop-Computer • Add-Computer • Remove-Computer • Rename-Computer • Reset-ComputerMachinePassword

正如不止一次說過的,但不要太多,你不應該以純文本形式存儲密碼。 可能是將其存儲為由特定帳戶加密的 clixml 文件的多種方法中最簡單的一種。 您可以將對其保存位置的訪問權限限制為可以對其進行解密的同一帳戶。

您需要將相同的用戶名/密碼與計算機名稱/IP 結合使用來構建實際的 PSCredential 對象。 我假設 IP 作為您的代碼顯示$listofservers.ip - 如果 CSV 標頭不是 IP,則相應地更新代碼。 我添加了一些反饋來向您顯示連接了哪台計算機和用戶,並且它能夠遠程運行命令的事實證明連接成功。

$username = 'localadmin'
$password = ConvertTo-SecureString 'PlaintextIsaNoNo' -AsPlainText -Force

Import-Csv '.\Windows Servers.csv' |   ForEach-Object {
        $cred = New-Object System.Management.Automation.PSCredential ("$($_.ip)\$username", $password)
        Try
        {
            Invoke-Command -ComputerName $_.ip -ScriptBlock {
                Write-Host Sucessfully connected to $env:computername as $(whoami) -ForegroundColor Green
            } -Credential $cred
        }
        Catch
        {
            $PSItem.Exception.Message
        }
    }

上面代碼的問題是它一次只能運行一台 PC。 如果你只是給它一個計算機列表(在 PS5.1 上默認最多 32 個), Invoke-Command異步運行。如果你改變結構,它會運行得更快。 這需要一個眾所周知的技巧,即提供用戶名作為.\\username ,指定它是本地帳戶。 我已經測試過了,它對我有用,所以如果你看到其他情況,請告訴我。

$username = 'localadmin'
$password = ConvertTo-SecureString 'PlaintextIsaNoNo' -AsPlainText -Force

$serverlist = Import-Csv '.\Windows Servers.csv'

$cred = New-Object System.Management.Automation.PSCredential (".\$username", $password)

Try
{
    Invoke-Command -ComputerName $serverlist.ip -ScriptBlock {
        Write-Host Sucessfully connected to $env:computername as $(whoami) -ForegroundColor Green
    } -Credential $cred
}
Catch
{
    $PSItem.Exception.Message
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM