/build/static/layout/Breadcrumb_cap_w.png

Service Stop and file removal

Hi, I need a powershell script that would stop a windows service ,wait for2 mins,delete a particular file from C:\documents&settings andthen start the service again.Isthis possible ?Thanks

0 Comments   [ + ] Show comments

Answers (2)

Posted by: SMal.tmcc 9 years ago
Red Belt
1

you can create a batch file and use psexec to push it out.

net stop service

PING 1.1.1.1 -n 1 -w 120000 >NUL

del c:\path\file /q /y

net start service

Posted by: rileyz 9 years ago
Red Belt
0

Yes, it is possible.

Why dont you research it? It will only take you about 30mins from start to finish.

  1. Google how to stop and start a service in PS
  2. Look up the sleep PS equivalent
  3. Lookup how to delete a file
    If your fancy you could use Test-Path with a If statement.

    Example Pseudocode
    Test-Path (hey does this file exsit?)
       Yes, lets delete it!
       No, Oh no file, im doing nothing.
     
  4. Since you know how to stop the service, you'll already know how to start it...
Thats it! 

Comments:
  • Hi rileyz,

    Would this be correct ? I will save and run this from the management server on remote computers.I need all in one script.

    $service = "*****"
    Import-CSV D:\FIxmachines\input.csv | % {
    $computer = $_.ComputerName

    #$result = (gwmi win32_service -computername $computer -filter "name='$service'").stopservice()
    Start-Sleep -s 120
    function delete-remotefile {
    PROCESS {
    $file = "\\$_\c$\Documents&Settings\*******.exe"
    if (test-path $file)
    {
    echo "$_ ********.exe exists"
    Remove-Item $file -force
    echo "$_ install.exe file deleted"
    }
    }
    } - ninjaguy 9 years ago
  • }

    Get-Content D:\Fixmachines\machines.txt | delete-remotefile
    $result = (gwmi win32_service -computername $computer -filter "name='$service'").startservice()} - ninjaguy 9 years ago
    • Right I've given this some thought and you didnt give us all the information!

      What is the target OS?
      Does it have powershell, if so what version?
      -can it be accessed remotely?
      Can the remote system take remote WMI calls? - rileyz 9 years ago
      • XP. Don't think the workstations have powershell.They can be accessed remotely and and can take WMI calls - ninjaguy 9 years ago
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ