Set-Service control actions - powershell

I am using below command to start the windows service. I need stop the execution of this command if it doesn't start before 5 or 10 and continue with next commands in script. Can you please help me how do it?
Get-Service -ComputerName “$server_name” -Name “XHedge.Execution-NYTRADER” |
Set-Service -Status Running

Related

My powershell script is not stopping a process that I am opening

I am creating a basic powershell script that will open the calculator app, get its process id, and then eventually stop the process itself with confirmation. But when I run the following script on my computer (through Powershell ISE, but it also doesn't seem to work on regular powershell in admin mode), it does not kill the calculator process. I am running Windows 10 Version 21H1.
Start-Process calc.exe; Get-Process calc; Stop-Process -Name calc -whatif; Stop-Process -Name calc -Confirm -PassThru
I had to use the name Calculator instead of calc for Get-Process. I also had to add a delay between the launching of calc.exe and Stop-Process. Without the delay, it could not find the process to stop it.
Start-Process -FilePath calc.exe
Start-Sleep -Seconds 3
Get-Process -Name Calculator | Stop-Process

PowerShell - How to create a script for automatic restart of windows services?

I have PowerShell commands to restart my windows service:
$Svc = Get-Service -DisplayName 'Test' -ErrorAction Stop
$Svc | Restart-Service -ErrorAction Stop
Now I do not know how I can automatically create powershell script which will execute those PS commands and also how to schedule that new powershell script to be executed once per day (to perform that restart).
Thank you!!!

Trying to run a chkdsk on c:\ automatically

Trying to run a chkdsk on all drives of the computer. Having issues with having C:\ start at all.
Trying to use SendKeys to answer "Would you like to schedule this volume to be checked the next time the system restarts? (Y/N)" but having no luck. Where am I going wrong?
$host.ui.RawUI.WindowTitle = "BOHCdrivefix"
$FixCDrive = Start-Job -Name cDriveFix -ScriptBlock {chkdsk c:/f}
$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('BOHCdrivefix')
sleep 3
$wshell.SendKeys('y')
$wshell.SendKeys('~')
wait-job $FixCDrive
Receive-Job $FixCDrive | Out-File -FilePath D:\temp\cDriveFix.txt
shutdown -r -f -t 0
I would like to answer Y to the question then shutdown the PC and have it start the chkdsk
From Start-Job document:
The Start-Job cmdlet starts a Windows PowerShell background job on
the local computer.
A Windows PowerShell background job runs a command without
interacting with the current session.
Hence, you can't send any key from the current session to a command running inside a background job. Sending a key must be inside the background job. Fortunately, chkdsk.exe accepts use pipeline operator (which sends the results of the preceding command to the next command) as follows:
$FixCDrive = Start-Job -Name cDriveFix -ScriptBlock {Write-Output 'y'|chkdsk.exe c:/f}
or (using echo alias for Write-Output cmdlet):
$FixCDrive = Start-Job -Name cDriveFix -ScriptBlock {echo 'y'|chkdsk.exe c:/f}
Please note:
The type of the file system is NTFS.
Cannot lock current drive.
Chkdsk cannot run because the volume is in use by another
process. Would you like to schedule this volume to be
checked the next time the system restarts? (Y/N)
To answer "yes" to above question asked by chkdsk c:/f (fix file system errors on the boot partition), you must press Y, followed by Enter.
Honestly said, I'm not sure whether Write-Output cmdlet sends Enter into the pipeline. If not, force output of the new line as follows:
Write-Output "y$([System.Environment]::NewLine)"|chkdsk.exe c:/f}

Stop the Running process of taskmgr from PowerShell

I write this command for powershell. I want to stop the running process of taskmgr from PowerShell.
Get-Service | Where-Object {$_.Status -eq "Running"}
And how to stop process those running processes?
Task Manager is a process, not a service.
So, in order to stop it from PowerShell you need to run:
Stop-Process -Name Taskmgr -Force
PS: You will need to run the powershell as administrator in order to stop the process.
Best regards,

Restart server and poll for started service

If I was to create a script to restart a server and wait for a started service SERVICE123 - NODE1 to come back up, how would I do this?
I have the following:
Restart-Computer -ComputerName COMPUTER123456 wait -For SERVICE123 - NODE1 -Timeout 400 -Delay 5
If not, is there a script I could use to poll for a started service only? I would like it to sit for 20 minutes or so until this service node is up and running.
To see if a Service is running you can use the invoke Command. And execute the
cmdlet get-service on the machine.
$service = Invoke-Command -ComputerName COMPUTER123456 -ScriptBlock { Get-Service -Name $args[0] } -ArgumentList "Service123"
via $service.Status you can see if the Service is Running or Stopped.
Normaly it should work with your code and the restart-Computer cmdlet,
what is the error, is there any error given. Why does your code not work?
I have an idea, you have to put the hyphen to the parameter 'wait' = -Wait
Restart-Computer -ComputerName COMPUTER123456 -Wait -For "SERVICE123 - NODE1" -Timeout 400 -Delay 5
Have a look at MSTechnet
In example 6 you see exactly what you need.
Greets Eldo.Ob