簡體   English   中英

csv powershell cmd output

[英]csv powershell cmd output

如果我已經安裝了一些應用程序並且現在腳本以 txt 文件結果返回,我將使用此腳本在多台機器上進行檢查。 (格式主機名\n是/否)。 如何更改 csv 文件中 2 列的返回結果; 1.主機名; 2、結果?

actual result.txt

hostname
YES / NO

desired csv export

Hostname | Result
hostname | YES / NO / OFFLINE

腳本

$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!"}
}

謝謝,

我修改了你的代碼,但沒有測試它。 我會做這樣的事情

$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

回答 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