# script block & parameter

**URL:** <https://forums.powershell.org/t/script-block-parameter/12349>\
**Category:** PowerShell Help\
**Created:** [April 5, 2019, 12:45pm UTC](https://forums.powershell.org/t/script-block-parameter/12349 "2019-04-05T12:45:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kardock](https://avatars.discourse-cdn.com/v4/letter/k/779978/32.png) [@kardock](https://forums.powershell.org/u/kardock)\
**Post date:** [April 5, 2019, 12:45pm UTC](https://forums.powershell.org/t/script-block-parameter/12349/1 "2019-04-05T12:45:57Z")

</div>

hi everyone,

seeing that I do not know how to correctly post a bit of code, I do have 2 questions;

- how to I insert my bit as code in here?
- how do I correctly pass a parameter to a script?

I need to start a service on a remote computer as a job so it is done in the background instead of waiting for the result. her's my attempt;
```
$pcname = 'pcname'

$scriptBlock = {
param($pc)
$strQuery = "Select * from Win32_Service where Name='RemoteRegistry'"
$service = Get-WmiObject -Query $strQuery -Namespace root\cimv2 -ComputerName $pc
if ($service.StartMode -ne 'Automatic')
{ $service.ChangeStartMode('Automatic') }
if ($service.State -ne 'Running')
{ $service.StartService() }
}

Start-Job -ScriptBlock $ScriptBlock -ArgumentList $pcname
```

thank you!

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [April 5, 2019, 1:29pm UTC](https://forums.powershell.org/t/script-block-parameter/12349/2 "2019-04-05T13:29:17Z")

</div>

You already have the code to pass arguments to the scriptblock. Get-Service returns the starttype and status of the service. Hence you don’t need to go with WMI.

```
$ScriptBlock = {
    Param($Name)
    $Param = @{Name = $Name}
    $Service = Get-Service @Param
    if($Service.StartType -ne 'Automatic'){$Param.StartType = 'Automatic'}
    if($Service.Status -ne 'Running'){$Param.Status = 'Running'}
    if($Param.StartType -or $Param.Status){Set-Service @Param}
}
Start-Job -ScriptBlock $ScriptBlock -ArgumentList 'ServiceName'
```

Here we create a hashtable with the service name then add starttype and status if required and use splating with Set-Service cmdlet to set both together.  
Splating will help us to use a hashtable with key as Parameter name and value as its arguments and will be passed using ‘@’ to a cmdlet.

Targeting this to PCs with jobs is much simpler using Invoke-Command.

```
Invoke-Command -ComputerName Server1,Server2 -ScriptBlock $ScriptBlock -ArgumentList 'ServiceName' -AsJob
```

---

<div class="post-metadata">

**Author:** ![kardock](https://avatars.discourse-cdn.com/v4/letter/k/779978/32.png) [@kardock](https://forums.powershell.org/u/kardock)\
**Post date:** [April 5, 2019, 2:01pm UTC](https://forums.powershell.org/t/script-block-parameter/12349/3 "2019-04-05T14:01:48Z")

</div>

the parameter is not the service name but the pc name 🙂

I’m doing something wrong because I’m unable to change the service state to running.

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [April 5, 2019, 2:04pm UTC](https://forums.powershell.org/t/script-block-parameter/12349/4 "2019-04-05T14:04:24Z")

</div>

Actual solution is using Invoke-Command where Server1,2 is same as PCs.

---

<div class="post-metadata">

**Author:** ![kardock](https://avatars.discourse-cdn.com/v4/letter/k/779978/32.png) [@kardock](https://forums.powershell.org/u/kardock)\
**Post date:** [April 8, 2019, 11:57am UTC](https://forums.powershell.org/t/script-block-parameter/12349/5 "2019-04-08T11:57:01Z")

</div>

[quote quote=149772]Actual solution is using Invoke-Command where Server1,2 is same as PCs.

[/quote]  
well sadly it ain’t working for me.

the service’s status doesn’t change on my test computer.

thanks for trying!

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [April 8, 2019, 12:32pm UTC](https://forums.powershell.org/t/script-block-parameter/12349/6 "2019-04-08T12:32:35Z")

</div>

You may debug this by adding some print statements in the if statements making sure it enters in neccessary if statements.

---

<div class="post-metadata">

**Author:** ![pwshliquori](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/pwshliquori/32/222_2.png) [@pwshliquori](https://forums.powershell.org/u/pwshliquori)\
**Post date:** [April 8, 2019, 1:43pm UTC](https://forums.powershell.org/t/script-block-parameter/12349/7 "2019-04-08T13:43:41Z")

</div>

Hi Kardock,

Give this a try

```
$ScriptBlock = {
    $Name = 'RemoteRegistry'
    $Service = Get-Service -Name $Name
    if ($Service.StartupType -ne 'Automatic') {
        Set-Service -Name $Name -StartUpType Automatic -Passthru |Select-Object Name,StartType |Format-Table
    }
    if ($Service.Status -eq 'Stopped') {
        Start-Service $Name -Passthru |Select-Object Name,Status |Format-Table
    }
}

Invoke-Command -ComputerName pc1 -ScriptBlock $ScriptBlock -AsJob
```

pwshliquori

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:33pm UTC](https://forums.powershell.org/t/script-block-parameter/12349/8 "2024-05-16T20:33:04Z")

</div>


