简体   繁体   English

csv powershell cmd output

[英]csv powershell cmd output

I use this script for check on multiple machine if I have installed some apps and now the script return in txt file result.如果我已经安装了一些应用程序并且现在脚本以 txt 文件结果返回,我将使用此脚本在多台机器上进行检查。 (format hostname\nYes/No). (格式主机名\n是/否)。 How can I change return result in csv file in 2 columns;如何更改 csv 文件中 2 列的返回结果; 1.Hostname; 1.主机名; 2. Result? 2、结果?

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,谢谢,

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:(回答 2:对于多个命令和捕获结果,但又没有经过测试:(

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM