0

I am working on my first powershell script. With this, I want to pass a ServiceController to a function but get an error that I can't solve. The script stops service, zip folder and then starts service.

I get the following error that I can't solve:

Service: Apache24_x86_php56 Running
Set-Service-Status : Cannot process argument transformation on parameter 'svc'. Cannot convert the
"System.Object[]" value of type "System.Object[]" to type "System.ServiceProcess.ServiceController".
At C:\temp\services-change-status.ps1:39 char:26
+ $rv = Set-Service-Status $svc, "Stopped"
+                          ~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Set-Service-Status], ParameterBindingArgumentTransformationExceptio
   n
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-Service-Status

Script:

Add-Type -assembly "system.io.compression.filesystem"

$servicecode = "Apache24_x86_php56"
$servicestatusstart = "Running"
$zipsource = "C:\datafolder"
$zipdestination = "C:\backup\datafolder.zip"
$exitcode = 0

function Set-Service-Status {
    param ([System.ServiceProcess.ServiceController]$svc, $status)

    Write-Host "Service: $($svc.ServiceName) $status..."
    Set-Service -InputObject $svc -Status $status
    $svc.WaitForStatus($status, "00:02:00")
    $svc.Refresh()
    
    # if service status does not match, return error
    if ($svc.Status -ne $status) {
        return 1
    }

    return 0
}

$svcl = Get-Service -Name $servicecode -ErrorAction Stop
if ($svcl.Count -ne 1) {
    Write-Host "Invalid service code. $($servicecode)"
    exit 1
}
$svc = $svcl[0]
Write-Host "Service: $($svc.ServiceName) $($svc.Status)"

# if service status is Stopped, don't start service at end
if ($svc.Status -eq "Stopped") {
    $servicestatusstart = "Stopped"
}

# stop service
$rv = Set-Service-Status $svc, "Stopped"
if ($rv -ne 0) {
    Write-Host "Service $($svc.ServiceName) is not Stopped. Status=$($svc.Status)"
    exit 2
}

# if zip file exists, delete it
if (Test-Path $zipdestination) {
    Remove-Item $zipdestination
}

# zip folder while service stopped
Write-Host "Compressing $($zipsource) to $($zipdestination)..."
[io.compression.zipfile]::CreateFromDirectory($zipsource, $zipdestination)
if (!$?) {
    Write-Host "Error compressing folder."
    $exitcode = 3
}

# restart service
$rv = Set-Service-Status $svc, $servicestatusstart
if ($rv -ne 0) {
    Write-Host "Service $($svc.ServiceName) is not $($servicestatusstart). Status=$($svc.Status)"
    $exitcode = 4
}

Write-Host "Service: $($svc.ServiceName) $($svc.Status)"

exit $exitcode
user3720435
  • 1,421
  • 1
  • 17
  • 27
  • 1
    `Set-Service-Status $svc, "Stopped"` -> `Set-Service-Status -svc $svc -status "Stopped"` – Mathias R. Jessen Sep 07 '20 at 22:35
  • Does this answer your question? [passing objects to powershell functions](https://stackoverflow.com/questions/34452949/passing-objects-to-powershell-functions) – codersl Sep 07 '20 at 23:17

1 Answers1

3

Arguments in powershell are separated by a space and not commas. Not to be confused with the parameter definition, which IS comma separated. See this example.

Function Test-Params {
    Param($first,$second)
    Write-Host "First argument is $first" -ForegroundColor Cyan
    Write-Host "Second argument is $second" -ForegroundColor Cyan
}

Test-Params a,b

Output

First argument is a b
Second argument is 

Now if we call the function with the arguments split by a space

Test-Params a b

Output

First argument is a
Second argument is b

I recommend reading through these help topics.

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parameters?view=powershell-7

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-7

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions?view=powershell-7

The links for for powershell version 7, if you are needing 5.1 just change the dropdown in the upper left hand corner of the page.

Doug Maurer
  • 8,090
  • 3
  • 12
  • 13