Reputation: 5584
Trying to streamline logging inside of a URL health-checking PowerShell script. It works the way I want except Start-Transcript
gives output which is too verbose and repeating itself. Based on this article, the default Debug/Verbose Preferences are set to SilentlyContinue at initialSessionState which prevents the output to the host. But it's showing as the script output which is behavior I'm trying to eliminate.
The Script:
#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()
}
}
Current output:
**********************
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
**********************
The simplified output I'm looking for:
200 - https://ems.alpha.local:1443/
200 - https://hpna.alpha.local:4443/
200 - http://login.microsoftonline.com/
Not sure that it matters but I'm calling this script from a one-line command file like this (which I'm not trying to change):
powershell -executionpolicy bypass -File "C:\Users\Admin\Documents\Scripts\URL_Check\URL_Check.ps1"
Upvotes: 0
Views: 122
Reputation: 61013
You could simply remove the Start-Transcript
and Stop-Transcript
calls and capture an array of formatted strings from the foreach loop.
Then display that on screen and write to file:
#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
Upvotes: 1