0

I use this script for check on multiple machine if I have installed some apps and now the script return in txt file result. (format hostname\nYes/No). How can I change return result in csv file in 2 columns ; 1.Hostname ; 2. Result ?

actual result.txt

hostname
YES / NO

desired csv export

Hostname | Result
hostname | YES / NO / OFFLINE

script

$Computers = Get-Content -Path C:\Users\m\Desktop\ip.txt | Where-Object { $_ -match '\S' }

foreach($Computer in $Computers){
Write-Host $Computer

    $User = "ussr"
    $Password = "pssd"
    $Command = 'hostname && if exist "C:\LdApp.exe" (echo YES) else (echo NO)'

    $secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force
    $Credentials = New-Object System.Management.Automation.PSCredential($User, $secpasswd)

Get-SSHTrustedHost | Remove-SSHTrustedHost

try{

$SessionID = New-SSHSession -ComputerName $Computer -Credential $Credentials -AcceptKey:$true

Invoke-SSHCommand -Index $sessionid.sessionid -Command $Command | Select -Expand Output | Add-Content -Path result.txt}

catch {Add-Content -Path result.txt -Value "$Computer conectare esuata!"}
}

Thank you,

1 Answer 1

1

I have modified your code and not tested it. I will do some thing like this

$result = Get-Content -Path C:\Users\m\Desktop\ip.txt | ForEach-Object {
    $computer = $_
    try {
        Write-Host $Computer
        $User = "ussr"
        $Password = "pssd"
        $Command = 'if exist "C:\LdApp.exe" (echo YES) else (echo NO)'
        $secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force
        $Credentials = New-Object System.Management.Automation.PSCredential($User, $secpasswd)
        Get-SSHTrustedHost | Remove-SSHTrustedHost
        $SessionID = New-SSHSession -ComputerName $Computer -Credential $Credentials -AcceptKey:$true
        $output = if ($SessionID) {
            (Invoke-SSHCommand -Index $sessionid.sessionid -Command $Command).Output
        }
        else {
            "Offline" 
        }
    }
    catch {
        { 
            Write-Host "$Computer conectare esuata!"
            $output = "conectare esuata!"
        }
    }
    [PsCustomObject]@{
        'Hostname' = $computer
        'Status'   = $output
    }
}
$result | Export-csv -Path "C:\Users\m\Desktop\result.csv" -NoTypeInformation

Answer2: for multiple commands and capturing results, and yet again not tested :(

Get-Content -Path C:\Users\m\Desktop\ip.txt | ForEach-Object {
    $computer = $_
    try {
        Write-Host $Computer
        $User = "ussr"
        $Password = "pssd"
        $Command = 'hostname && if exist "C:\LdApp.exe" (echo YES) else (echo NO)'
        $secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force
        $Credentials = New-Object System.Management.Automation.PSCredential($User, $secpasswd)
        Get-SSHTrustedHost | Remove-SSHTrustedHost
        $SessionID = New-SSHSession -ComputerName $Computer -Credential $Credentials -AcceptKey:$true
        if ($SessionID) {
            $Output = (Invoke-SSHCommand -Index $sessionid.sessionid -Command $Command).Output
            $result = $Output.split("`n")
            [PSCustomObject]@{
                "HostName" = $result[0]
                "IPAddress" = $result[1] # Print $result to verify the exact index of this value.
                "Status"   = $result[2]
            }
        }
        else {
            [PSCustomObject]@{
                "HostName"  = $computer
                "IPAddress" = "NA"
                "Status"    = "offline"
            }
        }
    }
    catch {
        { 
            Write-Host "$Computer conectare esuata!"
        }
    }

} | Export-csv -Path "C:\Users\m\Desktop\result.csv" -NoTypeInformation
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you Dilly, it works good but can I add in table +1 column with remote machine %hostname% ; $command includes hostname ; For me it's more easy to handle if I can have remote computername (%hostname%). (Hostname(remote %hostname%);IP($computer);Status)
I think 'yes', but it will be little bit complicated in storing results in PSCustomobject if session not established. Reason: We have to split the $output by a new line and pass the results and i dont have an env to test it.
I add one more output and hostname : <...> $output = if ($SessionID) { (Invoke-SSHCommand -Index $sessionid.sessionid -Command $Command).Output $output2 = if ($SessionID) { (Invoke-SSHCommand -Index $sessionid.sessionid -Command $Command2).Output} <...> [PsCustomObject]@{ 'Hostname' = $output2 'IP' = $Computer 'Status' = $output <...> It looks works fine, but i don;t know if i have machine offline or with other problem how reacts.
I have updated with another answer, if you're interested! Thanks. Please test it before using it.
it's not works very well. If machine is Online , 'status machine' is printed in IPaddress tab, and when is Offline in Hostname is printed IP (here need 'NA'), in IPAdress is NA (ok) and in status Offline (again OK).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.