Share via

getting error while running this powershell script.

ANKIT SINGH 21 Reputation points
2021-07-11T07:25:54.087+00:00

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 for business | Windows Server | User experience | PowerShell
0 comments No comments

Answer accepted by question author

Andreas Baumgarten 132.1K Reputation points MVP Volunteer Moderator
2021-07-11T20:47:49.62+00:00

Hi @ANKIT SINGH ,

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

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Andreas Baumgarten 132.1K Reputation points MVP Volunteer Moderator
    2021-07-11T08:09:24.99+00:00

    Hi @ANKIT SINGH ,

    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

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.