powershell - 简化 URL 健康检查 PowerShell 脚本中的日志记录
问题描述
尝试简化 URL 健康检查 PowerShell 脚本中的日志记录。它以我想要的方式工作,除了Start-Transcript
给出过于冗长和重复的输出。根据这篇文章,默认的 Debug/Verbose Preferences 在 initialSessionState 设置为 SilentlyContinue,这会阻止输出到主机。但它显示为脚本输出,这是我试图消除的行为。
剧本:
#Place URL list file in the below path
$URLListFile = "C:\Users\Admin\Documents\Scripts\URL Check\URL_Check.txt"
$URLList = Get-Content $URLListFile -ErrorAction SilentlyContinue
#For every URL in the list
Foreach($Uri in $URLList) {
try{
#For proxy systems
[System.Net.WebRequest]::DefaultWebProxy = [System.Net.WebRequest]::GetSystemWebProxy()
[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
#Web request
$req = [system.Net.WebRequest]::Create($uri)
$res = $req.GetResponse()
}catch {
#Err handling
$res = $_.Exception.Response
}
$req = $null
#Getting HTTP status code
$int = [int]$res.StatusCode
#Writing to Log file
Start-Transcript -append -path "C:\Users\Admin\Documents\Scripts\z_Logs\URL Check\URL_Check_log.txt"
#Writing on the screen
Write-Host "$int - $uri"
#Stopping logging
Stop-Transcript
#Disposing response if available
if($res){
$res.Dispose()
}
}
电流输出:
**********************
Windows PowerShell transcript start
Start time: 20201229205017
Username: ALPHA\Admin
RunAs User: ALPHA\Admin
Configuration Name:
Machine: OPTIPLEX (Microsoft Windows NT 10.0.18363.0)
Host Application: powershell -executionpolicy bypass -File C:\Users\Admin\Documents\Scripts\URL_Check\URL_Check.ps1
Process ID: 520
PSVersion: 5.1.18362.1171
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.18362.1171
BuildVersion: 10.0.18362.1171
CLRVersion: 4.0.30319.42000
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
**********************
Transcript started, output file is C:\Users\Admin\Documents\Scripts\z_Logs\URL_Check\URL_Check_log.txt
200 - https://ems.alpha.local:1443/
**********************
Windows PowerShell transcript end
End time: 20201229205017
**********************
**********************
Windows PowerShell transcript start
Start time: 20201229205017
Username: ALPHA\Admin
RunAs User: ALPHA\Admin
Configuration Name:
Machine: OPTIPLEX (Microsoft Windows NT 10.0.18363.0)
Host Application: powershell -executionpolicy bypass -File C:\Users\Admin\Documents\Scripts\URL_Check\URL_Check.ps1
Process ID: 520
PSVersion: 5.1.18362.1171
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.18362.1171
BuildVersion: 10.0.18362.1171
CLRVersion: 4.0.30319.42000
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
**********************
Transcript started, output file is C:\Users\Admin\Documents\Scripts\z_Logs\URL_Check\URL_Check_log.txt
200 - https://hpna.alpha.local:4443/
**********************
Windows PowerShell transcript end
End time: 20201229205017
**********************
**********************
Windows PowerShell transcript start
Start time: 20201229205018
Username: ALPHA\Admin
RunAs User: ALPHA\Admin
Configuration Name:
Machine: OPTIPLEX (Microsoft Windows NT 10.0.18363.0)
Host Application: powershell -executionpolicy bypass -File C:\Users\Admin\Documents\Scripts\URL_Check\URL_Check.ps1
Process ID: 520
PSVersion: 5.1.18362.1171
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.18362.1171
BuildVersion: 10.0.18362.1171
CLRVersion: 4.0.30319.42000
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
**********************
Transcript started, output file is C:\Users\Admin\Documents\Scripts\z_Logs\URL_Check\URL_Check_log.txt
200 - http://login.microsoftonline.com/
**********************
Windows PowerShell transcript end
End time: 20201229205018
**********************
我正在寻找的简化输出:
200 - https://ems.alpha.local:1443/
200 - https://hpna.alpha.local:4443/
200 - http://login.microsoftonline.com/
不确定这是否重要,但我正在从这样的单行命令文件中调用此脚本(我不想更改):
powershell -executionpolicy bypass -File "C:\Users\Admin\Documents\Scripts\URL_Check\URL_Check.ps1"
解决方案
您可以简单地删除Start-Transcript
andStop-Transcript
调用并从 foreach 循环中捕获格式化字符串数组。
然后在屏幕上显示并写入文件:
#Place URL list file in the below path
$URLListFile = "C:\Users\Admin\Documents\Scripts\URL Check\URL_Check.txt"
$URLList = Get-Content $URLListFile -ErrorAction SilentlyContinue
#For every URL in the list
$result = foreach($Uri in $URLList) {
try{
#For proxy systems
[System.Net.WebRequest]::DefaultWebProxy = [System.Net.WebRequest]::GetSystemWebProxy()
[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
#Web request
$req = [system.Net.WebRequest]::Create($uri)
$res = $req.GetResponse()
}
catch {
#Err handling
$res = $_.Exception.Response
}
$req = $null
#Getting HTTP status code
$int = [int]$res.StatusCode
# output a formatted string to capture in variable $result
"$int - $uri"
#Disposing response if available
if($res){
$res.Dispose()
}
}
# output on screen
$result
#output to log file
$result | Set-Content -Path "C:\Users\Admin\Documents\Scripts\z_Logs\URL Check\URL_Check_log.txt" -Force
推荐阅读
- python - 当我在 powershell 或 cmd 中键入 python 时,它会打开 microsoft store 而不是启动 python shell。如何解决这个问题?
- ios - Swift KeyChain 存储自定义对象
- unit-testing - 单元测试 RawQuery 时必须提供错误 DataSource.Factory
- multithreading - 如何更好地控制 CPU 上每个 BLAS 内核调用使用的线程数?
- c - 函数中使用的结构的存储位置
- video-indexer - 视频上传成功后无法索引
- c# - 试图创建一个继承父类并调用的控件模板?
- reactjs - 我在 mapStateToProps 中的状态未定义
- javascript - socket.ip expressjs 监听不是一个函数
- r - ggplot2 中的可选美学