adminst
Goto Top

Powershell PSSession Credential Prompt

Hallo zusammen
Ich stehe im Moment auf dem Schlauch. Ich möchte mit diesem Script zu mehreren Systemen connecten und ein paar Zeilen im File ändern:

# Import the hostnames from the file and connect to each system
Get-Content "hostnames.txt" | ForEach-Object {  
    $hostname = $_
    $session = New-PSSession -ComputerName $hostname -Credential $null
    
    # Out-comment the "Match Group administrators" and "AuthorizedKeysFile" lines in the sshd_config file 
    Invoke-Command -Session $session -ScriptBlock {
        $configPath = "C:\ProgramData\ssh\sshd_config"  
        $config = Get-Content $configPath
        $config | ForEach-Object {
            if ($_ -match "^Match Group administrators") {  
                Write-Output "# $_"  
            }
            elseif ($_ -match "^AuthorizedKeysFile") {  
                Write-Output "# $_"  
            }
            else {
                Write-Output $_
            }
        } | Set-Content $configPath
    }

    # Restart the sshd service
    Invoke-Command -Session $session -ScriptBlock { Restart-Service sshd }

    # Close the session and write a success message to the console
    Remove-PSSession $session

}

Es poppt für jedes System das Credential Popup auf, dabei möchte ich den User verwenden welcher das Skript ausführt.
Für einen Behebungsvorschlag wäre ich euch dankbar face-smile

Gruss
adminst

Content-Key: 6553350750

Url: https://administrator.de/contentid/6553350750

Printed on: April 27, 2024 at 08:04 o'clock

Mitglied: 3063370895
3063370895 Mar 29, 2023 updated at 06:37:55 (UTC)
Goto Top
Moin,

entferne mal das
-Credential $null
in Zeile 4
Warum hast du das da stehen?
Member: adminst
adminst Mar 29, 2023 at 06:41:11 (UTC)
Goto Top
Hallo chaot1coz
Wenn ich die Zeile entferne poppt für jedes System das Credential Popup auf.
Mitglied: 3063370895
3063370895 Mar 29, 2023 at 06:56:12 (UTC)
Goto Top
Zitat von @adminst:

Hallo chaot1coz
Wenn ich die Zeile entferne poppt für jedes System das Credential Popup auf.

Bist du dir sicher?
Wenn ich dein Skript nehme wie es ist, poppt das Popup auf. Wenn ich "-Credential $null" entferne, nicht mehr.
Mitglied: 3063370895
3063370895 Mar 29, 2023 updated at 06:58:50 (UTC)
Goto Top
# Import the hostnames from the file and connect to each system
Get-Content "c:\tmp\hostnames.txt" | ForEach-Object {  
    $hostname = $_
    $session = New-PSSession -ComputerName $hostname -Credential $null
    
    # Out-comment the "Match Group administrators" and "AuthorizedKeysFile" lines in the sshd_config file 
    Invoke-Command -Session $session -ScriptBlock {
        Write-Host $env:COMPUTERNAME
    }
    # Close the session and write a success message to the console
    Remove-PSSession $session

}
Ergibt Credential-Popups
# Import the hostnames from the file and connect to each system
Get-Content "c:\tmp\hostnames.txt" | ForEach-Object {  
    $hostname = $_
    $session = New-PSSession -ComputerName $hostname
    
    # Out-comment the "Match Group administrators" and "AuthorizedKeysFile" lines in the sshd_config file 
    Invoke-Command -Session $session -ScriptBlock {
        Write-Host $env:COMPUTERNAME
    }
    # Close the session and write a success message to the console
    Remove-PSSession $session

}

Keine Credential-Popups. Hostnamen werden wie erwartet ausgegeben.
Mitglied: 6247018886
6247018886 Mar 29, 2023 updated at 09:08:10 (UTC)
Goto Top
Dass ganze etwas komprimierter:
$configPath = "C:\ProgramData\ssh\sshd_config"  
Invoke-Command -ComputerName (Get-Content "hostnames.txt") -ScriptBlock {  
    (Get-Content $using:configPath) -replace '(?=^\s*(Match Group administrators|AuthorizedKeysFile))','#' | Set-Content $using:configPath  
    Restart-Service sshd
}
Setzt natürlich voraus das das Skript bereits mit Credentials ausgeführt wird die auf dem Remote-System entsprechende Berechtigungen haben. Wenn das nicht der Fall ist einfach noch die Credentials hinzufügen
$configPath = "C:\ProgramData\ssh\sshd_config"  
$creds = New-Object PSCredential('username',(ConvertTo-SecureString 'Password' -AsPlainText -Force))  
Invoke-Command -ComputerName (Get-Content "hostnames.txt") -Credential $creds -ScriptBlock {  
    (Get-Content $using:configPath) -replace '(?=^\s*(Match Group administrators|AuthorizedKeysFile))','#' | Set-Content $using:configPath  
    Restart-Service sshd
}
Cheers briggs
Mitglied: 3063370895
3063370895 Mar 29, 2023 updated at 08:58:28 (UTC)
Goto Top
Setzt natürlich voraus das das Skript bereits mit Credentials ausgeführt wird die auf dem Remote-System entsprechende Berechtigungen haben.
Der TE erwähnte, dass die Remote-Aktionen als der Benutzer ausgeführt werden soll, der das Skript ausführt.
Mitglied: 6247018886
6247018886 Mar 29, 2023 updated at 09:00:58 (UTC)
Goto Top
Zitat von @chaot1coz:
Der TE erwähnte, dass die Remote-Aktionen als der Benutzer ausgeführt werden soll, der das Skript ausführt.
Ja klar, wollte es nur für die anderen User die hier evt. vorbei schlendern nochmal explizit hin geschrieben haben!
Member: Dani
Dani Mar 30, 2023 at 18:28:06 (UTC)
Goto Top
Moin,
Ich möchte mit diesem Script zu mehreren Systemen connecten und ein paar Zeilen im File ändern:
sind die Systeme in einer Domäne oder Workgroup. Letztes würde das Verhalten (Abfrage der Cred) erklären.


Gruß,
Dani