繁体   English   中英

如何通过 csv 文件读取输入并将 output 写入 output 文件中的电源 Z2591C98B701124B562E?

[英]How to read input through a csv file and write the output in an output file in power shell script?

分享输入csv文件的截图 [分享输入csv文件的截图] 这是 powershell 脚本,用于获取服务器的选定服务状态,其中服务器列表通过输入 csv 文件给出,这些服务器的状态应存储在 output 文件中。

------------下面是脚本----------

$Servers = Get-Content "C:\temp\input.csv"
$Log = @()
foreach($Server in $Servers)
{
    $Services = Get-Service *HRRA*, "SQL Server Reporting Services" -ComputerName $COMPUTERNAME
    foreach ($Service in $Services)
    {
    $properties = @(
                  @{n='ServiceName';e={$Service.Name}}
                  @{n='Status';e={$Service.Status}}
                  @{n='ServerName';e={$Server}}
                  )
    $Log += "" | select $properties
    }

}
$Log  | Format-Table -AutoSize | Out-File "D:\temp\test.txt" -Force

------------------------------------新脚本------------ ----------------------

$Computers = Import-Csv "C:\Temp\Input.csv"
#$mailboxdata =  Get-Service *HRRA*,"SQL Server Reporting Services" -ComputerName $ComputerName| select machinename,name, status | sort machinename | 
#format-table -AutoSize |Out-File "D:\Temp\RRR.txt"
#LogWrite "$ComputerName"
foreach($row in $computers)
{
{
Add-Content -Path D:\Temp\SSRS.txt -Value $mailboxdata }
Get-Content -Path D:\Temp\SSRS.txt  
                 $ComputerName= $row.ComputerName;
$mailboxdata =  Get-Service *HRRA*,"SQL Server Reporting Services" -ComputerName $ComputerName| select machinename,name, status | sort machinename | 
format-table -AutoSize |Out-File "D:\Temp\SSR.txt"
$fromaddress = "Reporting.Services@accenture.com" 
$toaddress = "aditi.m.singh@accenture.Com"
#$toaddress1 = "s.ab.balakrishnan@accenture.com"
$Subject = "SSRS Services Status" 
$body = "Please find attached - test"
$attachment = "D:\Temp\SSR.txt" 
$smtpserver = "AMRINT.SMTP.ACCENTURE.COM"
$message = new-object System.Net.Mail.MailMessage 
$message.From = $fromaddress 
$message.To.Add($toaddress)
#$message.To.Add($toaddress1)
$message.IsBodyHtml = $True 
$message.Subject = $Subject 
$attach = new-object Net.Mail.Attachment($attachment) 
$message.Attachments.Add($attach) 
$message.body = $body 
$smtp = new-object Net.Mail.SmtpClient($smtpserver) 
$smtp.Send($message)

}

If i am running the script with static value its giving me output for both the servers---Below is the script----
Get-Service *HRRA*,"SQL Server Reporting Services" -ComputerName VW118627, VW118623 | select name, status, machinename | sort machinename | format-table -AutoSize |
Out-File "D:\Temp\Report.txt"

这是我在将服务器名称作为静态值给出后得到的输出

看截图,可以看到输入 csv 真的是一个C omma S分隔值文件。 对于这些,您可以使用Import-Csv cmdlet 从文件中检索计算机名称数组。

$outputFile = "D:\temp\test.csv"

# make sure the field name matches what is in the CSV header
$Servers = (Import-Csv -Path "C:\temp\input.csv").ComputerName  

$Log = foreach($Server in $Servers) {
    # add a test to see if we can reach the server
    if (Test-Connection -ComputerName $Server -Count 1 -Quiet -ErrorAction SilentlyContinue) {
        Get-Service -Name *HRRA*, "SQL Server Reporting Services" -ComputerName $Server | 
        Select-Object @{Name = 'MachineName'; Expression = {$Server}},
                      @{Name = 'ServiceName'; Expression = {$_.Name}},
                      @{Name = 'Status';      Expression = {$_.Status}}
    }
    else {
        Write-Warning "Server '$Server' is unreachable"
    }
}

# if you want the log sorted, do
$Log = $Log | Sort-Object MachineName

# output on screen
$Log | Format-Table -AutoSize 

# output to CSV file
$Log | Export-Csv -Path $outputFile -Force -NoTypeInformation

# mail the output csv file using Send-MailMessage
# collect the parameters in a hashtable
$mailParams = @{
    SmtpServer  = "AMRINT.SMTP.ACCENTURE.COM"
    From        = "Reporting.Services@accenture.com"
    To          = "aditi.m.singh@accenture.com"
    Subject     = "SSRS Services Status"
    Body        = "Please find attached - test"
    BodyAsHtml  = $true
    Attachments = $outputFile
    # add other parameters if needed
    # see https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/send-mailmessage
} 
# use 'splatting' to send the email 
# see https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting
Send-MailMessage @mailParams

PS Format-Table cmdlet 仅用于在屏幕上显示对象。

暂无
暂无

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

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