Powershell - 尝试将 2 个结果合并为 1 个 txt/csv
Powershell - trying to merge 2 result in 1 txt/csv
我正在尝试制作一个日常脚本来检查 URL 列表和 ping 服务器的状态。
我尝试合并 csv,但是 $status 代码的输出与 csv 中的不同
$pathIn = "C:\Users\test\Desktop\URLList.txt"
$URLList = Get-Content -Path $pathIn
$names = gc "C:\Users\test\Desktop\hostnames.txt"
#status code
$result = foreach ($uri in $URLList) {
try {
$res = Invoke-WebRequest -Uri $uri -UseBasicParsing -DisableKeepAlive -Method Head -TimeoutSec 5 -ErrorAction Stop
$status = [int]$res.StatusCode
}
catch {
$status = [int]$_.Exception.Response.StatusCode.value__
}
# output a formatted string to capture in variable $result
"$status - $uri"
}
$result
#output to log file
$result | Export-Csv "C:\Users\test\Desktop\Logs.csv"
#ping
$output = $()
foreach ($name in $names) {
$results = @{ "Host Name" = $name }
if (Test-Connection -Computername $name -Count 5 -ea 0) {
$results["Results"] = "Up"
}
else {
$results["Results"] = "Down"
}
New-Object -TypeName PSObject -Property $results -OutVariable nameStatus
$output += $nameStatus
}
$output | Export-Csv "C:\Users\test\Desktop\hostname.csv"
#combine the 2 csvs into 1 excel file
$path = "C:\Users\test\Desktop" #target folder
cd $path;
$csvs = Get-ChildItem .\*.csv
$csvCount = $csvs.Count
Write-Host "Detected the following CSV files: ($csvCount)"
foreach ($csv in $csvs) {
Write-Host " -"$csv.Name
}
Write-Host "--------------------"
$excelFileName = "daily $(get-Date -Format dd-MM-yyyy).xlsx"
Write-Host "Creating: $excelFileName"
foreach ($csv in $csvs) {
$csvPath = ".\" + $csv.Name
$worksheetName = $csv.Name.Replace(".csv", "")
Write-Host " - Adding $worksheetName to $excelFileName"
Import-Csv -Path $csvPath | Export-Excel -Path $excelFileName -WorkSheetname $worksheetName
}
Write-Host "--------------------"
cd $path;
Get-ChildItem \* -Include \*.csv -Recurse | Remove-Item
Write-Host "Cleaning up"
PowerShell 中的输出
200 - https://chargebacks911.com/play-404/
200 - https://www.google.com/
500 - httpstat.us/500/
Host Name Results
----------------
x.x.x.x Down
x.x.x.x Up
Detected the following CSV files: (2)
- daily 26-03-2022.csv
- Logs.csv
--------------------
Creating: daily26-03-2022.xlsx
- Adding daily 26-03-2022 to daily26-03-2022.xlsx
- Adding Logs to daily26-03-2022.xlsx
--------------------
Cleaning up
\----------------------------------
result in excel
\#Hostname
Host Name Results
x.x.x.x Down
x.x.x.x Up
\#Logs
Length
42
29
22
我想知道
- 如何更正“日志”中的输出sheet
- 如果有任何方法可以简化我的脚本以使其更清晰
欢迎来到 SO。您要求审查或重构您的完整脚本。我认为这不是应该使用的方式。相反,您可能会专注于一个特定问题并询问您遇到的具体问题。
我将只关注查询你们服务器状态的部分。你应该停止使用 Write-Host
。相反,您应该利用 PowerShell 的独特功能 - 使用丰富而强大的对象而不是愚蠢的文本。 ;-)
我会这样处理查询一堆计算机的任务:
$ComputernameList = Get-Content -Path 'C:\Users\test\Desktop\hostnames.txt'
$Result =
foreach ($ComputerName in $ComputernameList) {
[PSCustomObject]@{
ComputerName = $ComputerName
Online = Test-Connection -ComputerName $ComputerName -Count 1 -Quiet
}
}
$result
现在您有一个 PowerShell 对象,您可以通过管道传输到 Export-Csv
或将其用于进一步的步骤。
例如离线计算机的过滤器:
$result | Where-Object -Property Online -NE -Value $true
如果您坚持在脚本运行期间进行可视化控制,您可以使用 Write-Verbose or Write-Debug。这样您可以在需要时打开输出,但在脚本无人值守时忽略它。
我正在尝试制作一个日常脚本来检查 URL 列表和 ping 服务器的状态。
我尝试合并 csv,但是 $status 代码的输出与 csv 中的不同
$pathIn = "C:\Users\test\Desktop\URLList.txt"
$URLList = Get-Content -Path $pathIn
$names = gc "C:\Users\test\Desktop\hostnames.txt"
#status code
$result = foreach ($uri in $URLList) {
try {
$res = Invoke-WebRequest -Uri $uri -UseBasicParsing -DisableKeepAlive -Method Head -TimeoutSec 5 -ErrorAction Stop
$status = [int]$res.StatusCode
}
catch {
$status = [int]$_.Exception.Response.StatusCode.value__
}
# output a formatted string to capture in variable $result
"$status - $uri"
}
$result
#output to log file
$result | Export-Csv "C:\Users\test\Desktop\Logs.csv"
#ping
$output = $()
foreach ($name in $names) {
$results = @{ "Host Name" = $name }
if (Test-Connection -Computername $name -Count 5 -ea 0) {
$results["Results"] = "Up"
}
else {
$results["Results"] = "Down"
}
New-Object -TypeName PSObject -Property $results -OutVariable nameStatus
$output += $nameStatus
}
$output | Export-Csv "C:\Users\test\Desktop\hostname.csv"
#combine the 2 csvs into 1 excel file
$path = "C:\Users\test\Desktop" #target folder
cd $path;
$csvs = Get-ChildItem .\*.csv
$csvCount = $csvs.Count
Write-Host "Detected the following CSV files: ($csvCount)"
foreach ($csv in $csvs) {
Write-Host " -"$csv.Name
}
Write-Host "--------------------"
$excelFileName = "daily $(get-Date -Format dd-MM-yyyy).xlsx"
Write-Host "Creating: $excelFileName"
foreach ($csv in $csvs) {
$csvPath = ".\" + $csv.Name
$worksheetName = $csv.Name.Replace(".csv", "")
Write-Host " - Adding $worksheetName to $excelFileName"
Import-Csv -Path $csvPath | Export-Excel -Path $excelFileName -WorkSheetname $worksheetName
}
Write-Host "--------------------"
cd $path;
Get-ChildItem \* -Include \*.csv -Recurse | Remove-Item
Write-Host "Cleaning up"
PowerShell 中的输出
200 - https://chargebacks911.com/play-404/
200 - https://www.google.com/
500 - httpstat.us/500/
Host Name Results
----------------
x.x.x.x Down
x.x.x.x Up
Detected the following CSV files: (2)
- daily 26-03-2022.csv
- Logs.csv
--------------------
Creating: daily26-03-2022.xlsx
- Adding daily 26-03-2022 to daily26-03-2022.xlsx
- Adding Logs to daily26-03-2022.xlsx
--------------------
Cleaning up
\----------------------------------
result in excel
\#Hostname
Host Name Results
x.x.x.x Down
x.x.x.x Up
\#Logs
Length
42
29
22
我想知道
- 如何更正“日志”中的输出sheet
- 如果有任何方法可以简化我的脚本以使其更清晰
欢迎来到 SO。您要求审查或重构您的完整脚本。我认为这不是应该使用的方式。相反,您可能会专注于一个特定问题并询问您遇到的具体问题。
我将只关注查询你们服务器状态的部分。你应该停止使用 Write-Host
。相反,您应该利用 PowerShell 的独特功能 - 使用丰富而强大的对象而不是愚蠢的文本。 ;-)
我会这样处理查询一堆计算机的任务:
$ComputernameList = Get-Content -Path 'C:\Users\test\Desktop\hostnames.txt'
$Result =
foreach ($ComputerName in $ComputernameList) {
[PSCustomObject]@{
ComputerName = $ComputerName
Online = Test-Connection -ComputerName $ComputerName -Count 1 -Quiet
}
}
$result
现在您有一个 PowerShell 对象,您可以通过管道传输到 Export-Csv
或将其用于进一步的步骤。
例如离线计算机的过滤器:
$result | Where-Object -Property Online -NE -Value $true
如果您坚持在脚本运行期间进行可视化控制,您可以使用 Write-Verbose or Write-Debug。这样您可以在需要时打开输出,但在脚本无人值守时忽略它。