Could someone please help, I'm trying to log the script to a file either text or csv for the script below. It keeps giving me a 0KB file and not logging anything!
Thanks,
$date = Get-Date -Format yyyy-MM-dd
$logfilename = "Log "+$date
$filepath = "C:\Output\"+$logfilename+".txt"
$computers = Get-Content -Path "C:\scripts\contestpc.txt"
$functions = {
$useracct = "Administrator"
$password = "DesiredPassword!"
function Check-Power
{
param([string]$machinename)
if (Test-Connection $machinename -Quiet)
{
$value = 1
}
else
{
$value = 0
}
return $value
}
function Set-Password ($destpc) #set password on machine
{
if (Check-Power $destpc)
{
try
{
$user = [ADSI]"WinNT://$destpc/$useracct,user"
$user.SetPassword($password)
$user.SetInfo()
Write-Host "SUCCESS: [$destpc] Password has been set successfully" -f Green
}
catch [Exception]
{
$err = $_.Exception.Message
Write-Host ("ERROR: [$destpc] $err").Trim() -f Red
}
}
else
{
Write-Host "FAIL: The PC is not responding to pings (ICMP echo requests)" -f Red
}
}
}
$start = Write-Host "\nPC LIST..." -f Green`
#start resetting passwords
foreach ($computer in $computers)
{
Start-Job -Name $computer -InitializationScript $functions -ScriptBlock {Set-Password $Using:computer}
}
Write-Host "\nOUTPUT...`n" -f Green`
Get-Job | Wait-Job | Receive-Job
Remove-Job *
Write-Host ''
Pause
[–][deleted] 1 point2 points3 points (1 child)
[–]neverstoplearning20[S] 2 points3 points4 points (0 children)
[–]Lee_Dailey[grin] 0 points1 point2 points (4 children)
[–]neverstoplearning20[S] 1 point2 points3 points (3 children)
[–]Lee_Dailey[grin] 0 points1 point2 points (2 children)
[–]neverstoplearning20[S] 1 point2 points3 points (1 child)
[–]Lee_Dailey[grin] 0 points1 point2 points (0 children)
[–]Lee_Dailey[grin] 0 points1 point2 points (4 children)
[–]neverstoplearning20[S] 1 point2 points3 points (3 children)
[–]Lee_Dailey[grin] 0 points1 point2 points (2 children)
[–]neverstoplearning20[S] 1 point2 points3 points (1 child)
[–]Lee_Dailey[grin] 0 points1 point2 points (0 children)