question

ANKITSINGH-8079 avatar image
0 Votes"
ANKITSINGH-8079 asked AndreasBaumgarten edited

getting error while running this powershell script.

when i run this powershell script i have to manually feed a parameters!! is there is any way toh automate that step.


this is the script


>


param (
[Parameter(Mandatory=$true)]
[string] $wlidsvc,
[string] $Action,
[string] $ServiceStatus,
)

Checks if wlidsvc exists and provides stop

function CheckMyService ($wlidsvc)
{
if (Get-Service $ wlidsvc -ErrorAction SilentlyContinue)
{
# $ServiceStatus =(Get-Service -Name $S wlidsvc).Status
Write-Host $ wlidsvc "-" $stop
}
else
{
Write-Host "$ wlidsvc not found"
}
}


 #Condition if user wants to start a service
 elseif ($Action -eq 'Start')
 {
     if ((Get-Service -Name $ wlidsvc).Status -eq 'Running')
     {
         Write-Host $ wlidsvc "already running!"
     }
     elseif ((Get-Service -Name $ wlidsvc).Status -eq 'Stopped')
     {
         Write-Host $ wlidsvc "is stopped, preparing to start..."
         Get-Service -Name $ wlidsvc | Start-Service -ErrorAction SilentlyContinue
         CheckMyService $ wlidsvc
     }
     else
     {
         Write-Host $ wlidsvc "-" $start
     }
 }


windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered AndreasBaumgarten edited

Hi @ANKITSINGH-8079 ,

I played around with the script. Please check if this is working for you. Be aware to start services you need to have administrator privileges on the computer.

CSV file (test2.csv):

  wlidsvc,action
  AxInstSV,start
  AppReadiness,start

Script:

 function FuncCheckService {
     param($wlidsvc,
         $action
     )
     $service = Get-Service -Name $wlidsvc -ErrorAction SilentlyContinue
     if ($service) {
         if ((($service).Status -ne "Running" -and ($service).StartType -ne "Disabled") -and ($action -eq "start")) {
             Start-Service $wlidsvc
             Write-Host "Starting $wlidsvc service `r`n ---------------------- " 
             Write-Host -ForegroundColor Green " $wlidsvc service is now started"
         }
         if ($service.Status -eq "running") { 
             Write-Host "$wlidsvc service is already started" -ForegroundColor Yellow
         }
         if ($action -ne "start" -and (($service).Status -ne "running")) { 
             Write-Host "$wlidsvc service does not have action start set" -ForegroundColor Magenta
         }
     }
     else {
         Write-Host "$wlidsvc service not found" -ForegroundColor Red
     }
 }
 $lines = Import-Csv -Path C:\Junk\test2.csv -Delimiter ","
 foreach ($line in $lines) {
     FuncCheckService -wlidsvc $line.wlidsvc -action $line.action
 }


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered AndreasBaumgarten commented

Hi @ANKITSINGH-8079 ,

I am not sure what you exactly mean with "automate the step with providing the parameters".

But one option is maybe to provide the parameters in a CSV file. Maybe this example helps.

Create a CSV file with parameters (test1.csv):

 wlidsvc,Action,ServiceStatus
 svc1,restart,running
 svc2,stop,running
 svc3,start,stopped


Simple script as example:

 # Define Function 
 function JustForTest {
     param (
         [string]$wlidsvc,
         [string]$Action,
         [string]$ServiceStatus
     )
     # Put your code here
     Write-Output "wlidsvc = $wlidsvc - Action = $Action - Status  = $ServiceStatus"
 }
 # Read CSV file with parameters
 $lines = Import-Csv -Path C:\Junk\test1.csv -Delimiter ","
 # Execute function for each line in CSV using the parameter values
 foreach ($line in $lines) {
     JustForTest $line.wlidsvc $line.Action $line.ServiceStatus
 }

Result will look like this:

113527-image.png


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten



image.png (7.0 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

hlo @AndreasBaumgarten

i just used this script but cannot be able to start that service.

actully i want to make a script in which first it checks weather the service is running or not, if it is not running then the script will automatically will start that service

0 Votes 0 ·

Hi @ANKITSINGH-8079 ,

you used your function in the script (Instead of the JustForTest function I used in the example)?

Any error messages when executing your function?



(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

0 Votes 0 ·