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)
Please log in to answer
Posted by:
SMal.tmcc
10 years ago
Posted by:
rileyz
10 years ago
Yes, it is possible.
Why dont you research it? It will only take you about 30mins from start to finish.
- Google how to stop and start a service in PS
- Look up the sleep PS equivalent
- 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.
- 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 10 years ago -
}
Get-Content D:\Fixmachines\machines.txt | delete-remotefile
$result = (gwmi win32_service -computername $computer -filter "name='$service'").startservice()} - ninjaguy 10 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 10 years ago-
XP. Don't think the workstations have powershell.They can be accessed remotely and and can take WMI calls - ninjaguy 10 years ago