Kelly Garcia
asked on
Add Log to powershell Script
HI All,
I have this powershell script to install patches on our machines continuously, I wanted the output of the results to be on the powershell window as well as log file how do I add this?
I have this powershell script to install patches on our machines continuously, I wanted the output of the results to be on the powershell window as well as log file how do I add this?
# This file contains the list of servers you want to copy files/folders to
$computers = gc "C:\Users\kelly.garcia\Documents\Scripts\Windows7_64\servers_1.txt"
# This is the file/folder(s) you want to copy to the servers in the $computer variable
$Win764bitPatch = "C:\Users\kelly.garcia\Documents\Scripts\Windows7_64\WIN764windows6.1-kb4012212-x64.msu"
$Win732bitPatch = "C:\Users\kelly.garcia\Documents\Scripts\Windows7_64\WIN732windows6.1-kb4012212-x86.msu"
$Windows10_14393 = "C:\Users\kelly.garcia\Documents\Scripts\Windows7_64\Win10-1607-windows10.0-kb4013429-x64.msu"
$windows10_1511 = "C:\Users\kelly.garcia\Documents\Scripts\Windows7_64\Win10-1511-windows10.0-kb4013198-x64.msu"
$windows10_1507 = "C:\Users\kelly.garcia\Documents\Scripts\Windows7_64\Win10-windows10.0-kb4012606-x64.msu"
# The destination location you want the file/folder(s) to be copied to
$destination = "C$\"
#The command below pulls all the variables above and performs the file copy
ForEach ($computer in $computers) {
if ( Test-Connection -ComputerName $Computer -Count 2 -Quiet )
{
write-Host "$computer is Alive and Pinging " -ForegroundColor Green
Write-Host "Processing $($Computer) ..." -ForegroundColor Green
If (!(Get-HotFix -ComputerName $computer -Id KB4012215, KB4012212, KB4012606, KB4013429, KB4013198 -ErrorAction SilentlyContinue)){
$OS = Get-WmiObject -Query "select * from Win32_OperatingSystem" -ComputerName $Computer
Echo $OS | select PSComputerName, Caption, OSArchitecture, BuildNumber
If (($OS.Caption -like '*Windows 7*') -and ($OS.OSArchitecture -eq '64-bit')) {
if ($x = Copy-Item $Win764bitPatch -Destination "\\$computer\$destination" -PassThru -ErrorAction silentlyContinue)
{
write-Host "Copied $Win764bitPatch to \\$computer\$destination" -ForegroundColor Green
.\PsExec.exe -s -accepteula \\$computer wusa.exe "c:\$(($Win764bitPatch -split "\\")[-1])" /quiet
}
<# If (0, 3010 -contains $LASTEXITCODE) {
Write-Host " OK." -ForegroundColor Green
} Else {
Write-Host " FAILED." -ForegroundColor Red
} #>
} #endif$OS
ElseIf (($OS.Caption -like '*Windows 7*') -and ($OS.OSArchitecture -eq '32-bit'))
{
if ($x = Copy-Item $Win732bitPatch -Destination "\\$computer\$destination" -Recurse -PassThru -ErrorAction silentlyContinue)
{
write-Host "Copied $Win764bitPatch to \\$computer\$destination" -ForegroundColor Green
.\PsExec.exe -s -accepteula \\$computer wusa.exe "c:\WIN732windows6.1-kb4012212-x86.msu" /quiet
}
}
Elseif (($OS.Caption -like '*Windows 10*') -and ($OS.BuildNumber -eq '14393'))
{
if ($x = Copy-Item $Windows10_14393 -Destination "\\$computer\$destination" -Recurse -PassThru -ErrorAction silentlyContinue)
{
write-Host "Copied $Windows10_14393 to \\$computer\$destination" -ForegroundColor Green
.\PsExec.exe -s -accepteula \\$computer wusa.exe "c:\$(($Windows10_14393 -split "\\")[-1])" /quiet
}
}
Elseif (($OS.Caption -like '*Windows 10*') -and ($OS.BuildNumber -eq '1511'))
{
if ($x = Copy-Item $windows10_1511 -Destination "\\$computer\$destination" -Recurse -PassThru -ErrorAction silentlyContinue )
{
write-Host "Copied $windows10_1511 to \\$computer\$destination" -ForegroundColor Green
.\PsExec.exe -s -accepteula \\$computer wusa.exe "c:\$(($windows10_1511 -split "\\")[-1])" /quiet
}
}
Elseif (($OS.Caption -like '*Windows 10*') -and ($OS.BuildNumber -eq '1507'))
{
if ($x=Copy-Item $windows10_1507 -Destination "\\$computer\$destination" -Recurse -PassThru -ErrorAction silentlyContinue)
{
write-Host "Copied $windows10_1507 to \\$computer\$destination" -ForegroundColor Green
.\PsExec.exe -s -accepteula \\$computer wusa.exe "c:\$(($windows10_1507 -split "\\")[-1])" /quiet
}
}
Elseif (($OS.Caption -like '*Windows 10*') -and ($OS.BuildNumber -eq '10586'))
{
if ($x = Copy-Item $windows10_1511 -Destination "\\$computer\$destination" -Recurse -PassThru -ErrorAction silentlyContinue)
{
write-Host "Copied $windows10_1511 to \\$computer\$destination" -ForegroundColor Green
.\PsExec.exe -s -accepteula \\$computer wusa.exe "c:\$(($windows10_1511 -split "\\")[-1])" /quiet
}
}
}#endifhotfix
Else { Write-Host "Patch Already Exists on $Computer" -ForegroundColor White
}
}#endiftestconnection
Else {
Write-Host "Couldn't Connect to $Computer" -ForegroundColor Red
}
}#EndForeach
.\InstallPatchWin7Win83264_4.ps1
Use Tee-Object instead of Write-Host. That can be used to send output to the console and to a file.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
should I put this after the foreach:
.............
}#endiftestconnection
Else {
Write-Host "Couldn't Connect to $Computer" -ForegroundColor Red
}
}#EndForeach | Tee-Object -FilePath "C:\Test1\testfile2.txt"
No, because Write-Host does not send output to StdOut. If you don't want to change the body of the script use Start-Transcript as oBdA suggests. I'm endlessly seeking a good use for Tee-Object :)
ASKER
start-transcript will go on top of the powershell script?
Start-Transcript -Path "C:\Temp\Patch.log"
# This file contains the list of servers you want to copy files/folders to
$computers = gc "C:\Users\kelly.garcia\Documents\Scripts\Windows7_64\servers_1.txt"
Oh and I would be remiss if I didn't point out that you used a command after a line comment started. It's just a slightly longer comment as it is, nothing more.
Yep, right at the top (after Requires, any help blocks, and param, if used).
ASKER
.............
}#endiftestconnection
Else {
Write-Host "Couldn't Connect to $Computer" -ForegroundColor Red
}
} | Tee-Object -FilePath "C:\Test1\testfile2.txt"
is that better?
}#endiftestconnection
Else {
Write-Host "Couldn't Connect to $Computer" -ForegroundColor Red
}
} | Tee-Object -FilePath "C:\Test1\testfile2.txt"
is that better?
It doesn't get a lot easier than with the Start-/Stop-Transcript cmdlets - what you see is what you get in the log.
ASKER
At line:136 char:3
+ } | Tee-Object -FilePath "log.txt"
+ ~
An empty pipe element is not allowed.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordE xception
+ FullyQualifiedErrorId : EmptyPipeElement
+ } | Tee-Object -FilePath "log.txt"
+ ~
An empty pipe element is not allowed.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordE
+ FullyQualifiedErrorId : EmptyPipeElement
Yep, but the point about Write-Host not sending output to StdOut applies. Tee-Object consumes a standard input pipeline, it won't at all be aware of anything you create with Write-Host.
Write-Host is special, it's largely shunned these days except for thing that are fully aware they're creating output which cannot be captured / redirected.
PowerShell 5 changes Write-Host to be an extended wrapper of Write-Information. The information stream can be captured (has a stream number, has preference variables, and so on).
Write-Host is special, it's largely shunned these days except for thing that are fully aware they're creating output which cannot be captured / redirected.
PowerShell 5 changes Write-Host to be an extended wrapper of Write-Information. The information stream can be captured (has a stream number, has preference variables, and so on).
Anyway, Tee-Object is fun, but use Start-Transcript.
Write-Host is fun and useful.
If used correctly.
That is, for eye candy only.
And for that, I embrace it with all of my heart. Booya, Shunners!
Just avoid it for Orchestrator scripts, the host doesn't support it.
If used correctly.
That is, for eye candy only.
And for that, I embrace it with all of my heart. Booya, Shunners!
Just avoid it for Orchestrator scripts, the host doesn't support it.
PSScriptAnalyzer rightly warns about its use too :)
Like I said, if your intent is to write the eye-candy that becomes an acceptable use case. I have a couple of commands where I simply suppress warnings from PSScriptAnalyzer because the use of the command is entirely right and proper.
Outside of that, I only use Write-Host when writing demonstrative code because it's rather convenient. For logging... never. Still, it's a free world and a free language, diversity is good :)
Like I said, if your intent is to write the eye-candy that becomes an acceptable use case. I have a couple of commands where I simply suppress warnings from PSScriptAnalyzer because the use of the command is entirely right and proper.
Outside of that, I only use Write-Host when writing demonstrative code because it's rather convenient. For logging... never. Still, it's a free world and a free language, diversity is good :)