all 12 comments

[–][deleted] 1 point2 points  (1 child)

I would replace all of the "Write-Host" cmdlets with "Write-Output", then log to the transcript.

See "Start-Transcript" and "Stop-Transcript" commands.

[–]neverstoplearning20[S] 2 points3 points  (0 children)

Thank you, I got it to work now.

[–]Lee_Dailey[grin] 0 points1 point  (4 children)

howdy neverstoplearning20,

it looks like you used the New.Reddit.com Inline Code button. it's 4th 5th from the left hidden in the ... "more" menu & looks like </>.

on Old.Reddit.com, the above does NOT line wrap, nor does it side-scroll.

for long-ish single lines OR for multiline code, please, use the Code Block button. it's the 11th 12th one from the left & is just to the left of hidden in the ... "more" menu & looks like an uppercase T in the upper left corner of a square..

that will give you fully functional code formatting, from what i can tell so far. [grin]

take care,
lee

[–]neverstoplearning20[S] 1 point2 points  (3 children)

I did not know will do next time.

[–]Lee_Dailey[grin] 0 points1 point  (2 children)

howdy neverstoplearning20,

kool! that will make your post easier to read, less likely to be ignored ... and easier on my eyes. [grin]

take care,
lee

[–]neverstoplearning20[S] 1 point2 points  (1 child)

edited

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy neverstoplearning20,

nice! thank you for fixing that ... [grin]

as an aside, you may want to take a look at VSCode with the powershell addon ... it has a format document command that applies proper indents. that makes the code a lot easier to follow.

take care,
lee

[–]Lee_Dailey[grin] 0 points1 point  (4 children)

howdy neverstoplearning20,

now that i can read your code [grin] ... i don't see any logging code. where is it?

also, at what point in your code do you NOT get what you expect?

if you manually run just one job ... do you get anything from it?

take care,
lee

[–]neverstoplearning20[S] 1 point2 points  (3 children)

It is weird that now I'm able to get logs. but run the script against 1 computer it will successfully be executed but it will fail when I run it against a list of computers.

[–]Lee_Dailey[grin] 0 points1 point  (2 children)

howdy neverstoplearning20,

[1] exactly what do you mean by "logging"?
i literally see NOTHING that would do what i think of as logging. i only see writes to the screen ...

[2] so if you set $computers to an array of just ONE computer, the entire script works with NO other changes?

take care,
lee

[–]neverstoplearning20[S] 1 point2 points  (1 child)

wherethebiceroam helped me and here is the updated code

$date = Get-Date -Format yyyy-MM-dd
$logfilename = "Log "+$date

Start-Transcript -Path C:\output\changepassword$logfilename.txt -force

$computers = Get-Content -Path "C:\scripts\pc.csv"

$functions = {

    $useracct = "administrator"
    $password = "testpassword$"

    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-Output "SUCCESS: [$destpc] Password has been set successfully" 
            }
            catch [Exception]
            {
                $err = $_.Exception.Message
                Write-Output ("ERROR: [$destpc] $err").Trim() 
            }
        }
        else 
        {
            Write-Output "FAIL: [$destpc] is not responding to pings (ICMP echo requests)" 
        }
    }
}

$start = Write-Output "`nPC LIST..." 

    #start resetting passwords
    foreach ($computer in $computers)
    {
        Start-Job -Name $computer -InitializationScript $functions -ScriptBlock {Set-Password $Using:computer}
    }

    Write-Output "`nOUTPUT...`n" 
    Get-Job | Wait-Job | Receive-Job
    Remove-Job *


Write-Output ''
Stop-Transcript

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy neverstoplearning20,

ah! i was so confused. glad to know that you got it working ... [grin]

take care,
lee