Gunter
Gunter

Reputation: 137

I want to get a log file of exactly the things my Powershell script does?

I've been trying to get the steps that the script does into a txt file, but I just can't get my head around it

$days = -2
$date = (get-date).adddays($days)
$lastboot = (Get-WmiObject Win32_OperatingSystem).LastBootUpTime
$Computer = Get-ADComputer -SearchBase 'OU=TEST-OU,OU=EDV,DC=Kolbe-Druck,DC=de' -Filter '*' | Select -EXP Name

$lastbootconverted = ([WMI]'').ConvertToDateTime($lastboot)

write-host $date

write-host $lastboot

write-host $lastbootconverted

if($date -gt $lastbootconverted)
{
    write-host Need to reboot
    (Stop-Computer $Computer -Force)
}
else
{
    write-host no need to reboot
}

any tips?

Upvotes: 0

Views: 83

Answers (1)

guiwhatsthat
guiwhatsthat

Reputation: 2434

You can write your text into a txt with this code:

"Test" | Out-File -FilePath "Your Path to the txtfile" -Append

I would make a function, so you dont have to write this line more than once

EDIT: You can also add a timestamp:

"Test " + $(Get-Date -Format g) | out-file....

Upvotes: 1

Related Questions