# Invoke-Command for SQL Installation

**URL:** https://forums.powershell.org/t/invoke-command-for-sql-installation/12676
**Category:** PowerShell Help
**Created:** [June 6, 2019, 11:57am UTC](https://forums.powershell.org/t/invoke-command-for-sql-installation/12676 "2019-06-06T11:57:25Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![psdarwin](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/psdarwin/32/2063_2.png) [@psdarwin](https://forums.powershell.org/u/psdarwin)
#### Post date: [June 6, 2019, 11:57am UTC](https://forums.powershell.org/t/invoke-command-for-sql-installation/12676/1 "2019-06-06T11:57:25Z")

</div>

Can anyone provide any insight on how to make this work?

```
$ADSession = New-PSSession -ComputerName $ComputerName -Credential $DomainAdminCred
$Script = "Start-Process -FilePath M:\Setup.exe -Wait -Verb RunAs -ArgumentList '/Q /ConfigurationFile=C:\SQLConfigurationFile.ini'"
Invoke-Command -Session $ADSession -ScriptBlock {$Script} -Verbose
```

The invoke-command immediately returns, and SQL is not installed.

If I run the same Start-Process locally, it will run (with a UAC prompt).

---

<div class="post-metadata">

### Author: ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)
#### Post date: [June 6, 2019, 2:29pm UTC](https://forums.powershell.org/t/invoke-command-for-sql-installation/12676/2 "2019-06-06T14:29:47Z")

</div>

You have 1. quotes around your command sequence, rendering it just string data, and 2. scriptblock around that. If you execute the scriptblock you’re passing to `-ScriptBlock` parameter with `& {$Script}` you’ll see it just outputs the string to the console. That’s exactly what’s happening on the remote machine.

Replace your quotes in the `$Script =` definition line with the braces you’re using in the `Invoke-Command` line and drop the braces from that line:

```
$Script = { Start-Process -FilePath M:\Setup.exe -Wait -Verb RunAs -ArgumentList '/Q /ConfigurationFile=C:\SQLConfigurationFile.ini' }
Invoke-Command -Session $ADSession -ScriptBlock $Script -Verbose
```

---

<div class="post-metadata">

### Author: ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)
#### Post date: [June 6, 2019, 3:22pm UTC](https://forums.powershell.org/t/invoke-command-for-sql-installation/12676/3 "2019-06-06T15:22:35Z")

</div>

This doesn’t work?

```
Invoke-Command -Session $ADSession { M:\Setup.exe /Q /ConfigurationFile=C:\SQLConfigurationFile.ini }
```

---

<div class="post-metadata">

### Author: ![an0nemus](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@an0nemus](https://forums.powershell.org/u/an0nemus)
#### Post date: [June 6, 2019, 4:37pm UTC](https://forums.powershell.org/t/invoke-command-for-sql-installation/12676/4 "2019-06-06T16:37:03Z")

</div>

[quote quote=160010]Can anyone provide any insight on how to make this work?

PowerShell

3 lines

&nbsp;

1

2

3

$ADSession = New-PSSession -ComputerName $ComputerName -Credential $DomainAdminCred

$Script = "Start-Process -FilePath M:\Setup.exe -Wait -Verb RunAs -ArgumentList '/Q /ConfigurationFile=C:\SQLConfigurationFile.ini'"

Invoke-Command -Session $ADSession -ScriptBlock {$Script} -Verbose

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

The invoke-command immediately returns, and SQL is not installed.

If I run the same Start-Process locally, it will run (with a UAC prompt).

[/quote]

The first problem I see is that you define $script locally, but the invoke-command has no way of knowing what $script means.

You can pass that as $using:script inside the scriptblock.

I had to do something similar (sorta kinda…) I ended up doing this:

```
$arglist="/quiet /log log.txt"

foreach ($s in $servers){
$session=New-PSSession -cn $S
if (!(test-path \\$S\G$\core)){copy-item -ToSession $session -path G:\core -destination G:\core -force -recurse}
invoke-command -session $Session -Scriptblock {
set-location G:\core
start-process -filepath dotnet-hosting-2.2.3-win.exe -ArgumentList $using:ArgList -wait
}
}
```

without the -wait, control will pass back to the command line. If the session were to end, then the install won’t finish. I wish I wasn’t speaking from experience. What I needed was much simpler than what you’re doing, I’m not sure if the UAC will cause the remote install to hang or not.

---

<div class="post-metadata">

### Author: ![psdarwin](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/psdarwin/32/2063_2.png) [@psdarwin](https://forums.powershell.org/u/psdarwin)
#### Post date: [June 7, 2019, 9:29am UTC](https://forums.powershell.org/t/invoke-command-for-sql-installation/12676/5 "2019-06-07T09:29:50Z")

</div>

I tried this - same result.

```
Invoke-Command -Session $ADSession -ScriptBlock {Start-Process -FilePath M:\Setup.exe -Wait -Verb RunAs -ArgumentList '/Q /ConfigurationFile=C:\SQLConfigurationFile.ini'} -Verbose
```

I believe the issue has to do with the SQL installation EXE itself. When I run it locally, it does pop open another window when it runs. I’m guessing that it can’t do that when using invoke-command, or at least not the way I’m doing it.

---

<div class="post-metadata">

### Author: ![an0nemus](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@an0nemus](https://forums.powershell.org/u/an0nemus)
#### Post date: [June 7, 2019, 1:14pm UTC](https://forums.powershell.org/t/invoke-command-for-sql-installation/12676/6 "2019-06-07T13:14:06Z")

</div>

Have you checked out this link:

[https://docs.microsoft.com/en-us/sql/database-engine/install-windows/install-sql-server-from-the-command-prompt?view=sql-server-2017](https://docs.microsoft.com/en-us/sql/database-engine/install-windows/install-sql-server-from-the-command-prompt?view=sql-server-2017)

&nbsp;

---

<div class="post-metadata">

### Author: ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)
#### Post date: [June 7, 2019, 1:31pm UTC](https://forums.powershell.org/t/invoke-command-for-sql-installation/12676/7 "2019-06-07T13:31:55Z")

</div>

Uh, when you want to wait for a command to end with start -wait, you get into quoting issues. Makes me go back to start /wait in .bat files sometimes.

---

<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:32pm UTC](https://forums.powershell.org/t/invoke-command-for-sql-installation/12676/8 "2024-05-16T20:32:11Z")

</div>


