Share

This topic contains 3 replies, has 3 voices, and was last updated by  Karim Buzdar 2 weeks, 2 days ago.

  • Author
    Posts
  • #373127
     David Otika 
    Participant
    • Topics: 1
    • Replies: 1
    Post count: 3
    Member Points: 82
    Rank: Level 1

    I could figure out where to post this questions, so I am hopping I am in the right spot. First off, I am new to powershell just like many IT Admin who are suddenly find them self having to automate thus, learning PS. I have a script that I want to do the following; and when I run these lines one by one, it does what it supposed to do:

    1. Look into a text file with computer names and run a script
    2. The script uses psexe to run, windrm.cmd on the remote computer.
    3. Using “Enter-PSsession the script access remote computer.
    4. It then stops wuauserv  service
    5. It deletes all file contents of “SoftwareDistribution ” content.
    6. Restarts the Wuauserv and reports the samething over and over again.

    Here is the script I have, please help:

     

    ForEach($computer in (Get-Content c:\\wsus\failedpc.txt ))

    { psexec \\$computer -s winrm.cmd quickconfig -q

    Enter-PSSession -ComputerName $computer

    stop-service -Name wuauserv -force

    do {(Write-host “the service is still stopping”), (Start-sleep -seconds 30)}
    Until ((get-Service wuauserv).status -eq “stopped”)

    Get-childitem -path “C:\Windows\SoftwareDistribution” -Recurse -force | Remove-item -Recurse

    Start-Service -Name wuauserv -force
    do {(Write-host “the service is still starting”), (Start-sleep -seconds 30)}
    Until ((get-Service wuauserv).status -eq “started”)
    }
    Else
    {Break}

     

    1+
  • #373129
     David Otika 
    Participant
    • Topics: 1
    • Replies: 1
    Post count: 3
    Member Points: 82
    Rank: Level 1

    right now when I run this script, it does the following. It able to enter into the New-PSession, it gives me message message that the service is still stopping, then instead of running the Get-ChildItem  line, it skips to the “start-service and then it proceeds to spit out “the service is still starting” to no end. Any help would be appreciated. Thanks.

     

    David

    1+
  • #373150
     Michael Pietroforte 
    Keymaster
    • Topics: 142
    • Replies: 345
    Post count: 1093
    Member Points: 8,065
    Rank: Level 1

    David, thanks for posting! You found the right spot. 🙂

    The main problem with your script is the usage of Enter-PSSession. This cmdlet is for interactive sessions where an admin manually connects to a single remote computer and then types some commands on the remote console. In your example, stop-service will stop the service on the local computer, not on the remote machine.

    What you need instead of Enter-PSSession is Invoke-Command. You can execute a script on multiple remote computers like this:

    Of course, you can also read the computer names from a text file. I explained Invoke-Command here.

    You probably also don’t need psexec to enable PowerShell remoting. If the computers are domain members, I would do it with Group Policy. If you really want to do it with psexec, you also have to open the firewall ports. The articles here explain all possible ways of how to enable remoting

    Let me know if you figured it out.

    1+
  • #378852
     Karim Buzdar 
    Moderator
    • Topics: 26
    • Replies: 90
    Post count: 285
    Member Points: 6,677
    Rank: Level 1

    Hi David,

    Can we assume that your issue has been resolved?

    Thank you,

    Karim

    1+

You must be logged in to reply to this topic.

CONTACT US

Please ask IT administration questions in the forum. Any other messages are welcome.

Sending
© 4sysops 2006 - 2018

Log in with your credentials

or    

Forgot your details?

Create Account