1

i have this Script which is checking if a named service is running. In the actual case its checking for "RSCDsvc" service.

if(!(Get-ScheduledTask | Where-Object {$_.TaskName -like "restart_RSCDsvc" })){
New-Item -Path C:\Admin\scripts -Name check-service.cmd -Force -ItemType "file" -Value '
@ECHO OFF
SET SvcName=%~1

SC QUERYEX "%SvcName%" | FIND "STATE" | FIND /v "RUNNING" > NUL && (
ECHO %SvcName% is not running
ECHO START %SvcName%

NET START "%SvcName%" > NUL || (
ECHO "%SvcName%" wont start
EXIT /B 1
)
ECHO "%SvcName%" is started
EXIT /B 0
) || (
ECHO "%SvcName%" is running
EXIT /B 0
)
'
$dt= ([DateTime]::Now)
$timespan = $dt.AddYears(33) -$dt;
$principal = New-ScheduledTaskPrincipal -UserID "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest
$Action = New-ScheduledTaskAction -Execute C:\Admin\scripts\check-service.cmd -Argument "RSCDsvc"
$Trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).Date -RandomDelay (New-TimeSpan -Minutes 5) -RepetitionDuration $timespan -RepetitionInterval (New-TimeSpan -Minutes 10)
$Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings ($Stset = New-ScheduledTaskSettingsSet -ExecutionTimeLimit (New-TimeSpan -Minutes 3)) -Principal $principal
$Task | Register-ScheduledTask -TaskName ‘restart_RSCDsvc’
}

Now my question would be if i can work with more arguments so i dont have to enter the specific Service name so often in the script. Now i have to set it at 3 different points

if(!(Get-ScheduledTask | Where-Object {$_.TaskName -like "restart_RSCDsvc" }))

$Action = New-ScheduledTaskAction -Execute C:\Admin\scripts\check-service.cmd -Argument "RSCDsvc"

$Task | Register-ScheduledTask -TaskName ‘restart_RSCDsvc’

Like ive said my goal would be to have something like "$Servicename = RSCDSVC" and then its set for this three lines. I have tried it already but im already proud i got this script finish as im not very good in scripting :(

0

You can just replace all instances of RSCDSVC with the variable name just like you theorized

$Servicename = "RSCDSVC"
if(!(Get-ScheduledTask | Where-Object {$_.TaskName -like "restart_$Servicename" })){
New-Item -Path C:\Admin\scripts -Name check-service.cmd -Force -ItemType "file" -Value '
@ECHO OFF
SET SvcName=%~1

SC QUERYEX "%SvcName%" | FIND "STATE" | FIND /v "RUNNING" > NUL && (
ECHO %SvcName% is not running
ECHO START %SvcName%

NET START "%SvcName%" > NUL || (
ECHO "%SvcName%" wont start
EXIT /B 1
)
ECHO "%SvcName%" is started
EXIT /B 0
) || (
ECHO "%SvcName%" is running
EXIT /B 0
)
'
$dt= ([DateTime]::Now)
$timespan = $dt.AddYears(33) -$dt;
$principal = New-ScheduledTaskPrincipal -UserID "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest
$Action = New-ScheduledTaskAction -Execute C:\Admin\scripts\check-service.cmd -Argument "$Servicename"
$Trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).Date -RandomDelay (New-TimeSpan -Minutes 5) -RepetitionDuration $timespan -RepetitionInterval (New-TimeSpan -Minutes 10)
$Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings ($Stset = New-ScheduledTaskSettingsSet -ExecutionTimeLimit (New-TimeSpan -Minutes 3)) -Principal $principal
$Task | Register-ScheduledTask -TaskName "restart_$Servicename"
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.