patternshellMinor
Stop, Start, or Restart a SSAS Tabular service instance in PowerShell with Invoke-Command and $variables
Viewed 0 times
restartwithpowershelltabularstopinstancecommandservicevariablesstart
Problem
I'm working on some PowerShell commands / scripts to stop, or start, or restart a SSAS Tabular service instance using
I'm using PowerShell 5.0 on my source (local) machine, and trying to stop / start / restart a remote SSAS 2012 Tabular server instance (11.0.6540.0).
First, I start my script with
Then, using the hard coded method just to get the syntax right and watching the SQL Server Configuration Manager (Config Mgr) while RDP'd into the server to verify the command is working, I use this PowerShell code:
This works great. I verify in Config Mgr that the service stops.
I also verify that the 2 below PowerShell code scripts work:
and
Excellent. Works great.
Now I start putting things into variables like so:
I then show the values of the variables to verify expected results:
And I get the following results returned in the PowerShell console:
OK, so things seem good.
I then run the below PowerShell command, expecting it to work, but nothing happe
Invoke-Command and some $variables. The end goal is to eventually put this code into an Agent Job for easy service restarting, but that's beyond the scope of this post -- I need to get this working with the $variables first.I'm using PowerShell 5.0 on my source (local) machine, and trying to stop / start / restart a remote SSAS 2012 Tabular server instance (11.0.6540.0).
First, I start my script with
Set-ExecutionPolicy.Set-ExecutionPolicy -Scope CurrentUser RemoteSigned -ForceThen, using the hard coded method just to get the syntax right and watching the SQL Server Configuration Manager (Config Mgr) while RDP'd into the server to verify the command is working, I use this PowerShell code:
Invoke-Command -ComputerName DevServer { stop-service 'MSOLAP$TABULAR' }This works great. I verify in Config Mgr that the service stops.
I also verify that the 2 below PowerShell code scripts work:
Invoke-Command -ComputerName DevServer { start-service 'MSOLAP$TABULAR' }and
Invoke-Command -ComputerName DevServer { restart-service 'MSOLAP$TABULAR' }Excellent. Works great.
Now I start putting things into variables like so:
$tabularinstance = 'MSOLAP$TABULAR'
$server = "DevServer"
$stop = "stop-service $tabularinstance"
$start = "start-service $tabularinstance"
$restart = "restart-service $tabularinstance"I then show the values of the variables to verify expected results:
# show values
$tabularinstance
$stop
$start
$restart
$serverAnd I get the following results returned in the PowerShell console:
PS H:\> # show values
$tabularinstance
$stop
$start
$restart
$server
MSOLAP$TABULAR
stop-service MSOLAP$TABULAR
start-service MSOLAP$TABULAR
restart-service MSOLAP$TABULAR
DevServerOK, so things seem good.
I then run the below PowerShell command, expecting it to work, but nothing happe
Solution
The script block works a bit different when it comes to passing variables. You have to tell the command what the variable contains on the remote computer.
As well if you want to pass the full command as a variable you will need at the type of the variable as
This needs to be changed to something like this (changed variable names to suite my example):
The other way:
As well if you want to pass the full command as a variable you will need at the type of the variable as
[scriptblock]. $stop = "stop-service 'MSOLAP$TABULAR'"
Invoke-Command -ComputerName $server { $stop }This needs to be changed to something like this (changed variable names to suite my example):
$MyService = 'W32Time'
$get = [scriptblock]::create( "param(`$servicename) Stop-Service `$servicename")
Invoke-Command -ComputerName sql2014-01 -ScriptBlock $get -ArgumentList $MyServiceThe other way:
$MyService = 'W32Time'
Invoke-Command -ComputerName SQL2014-01 -ScriptBlock {param($servicename); Stop-Service $servicename } -ArgumentList $MyServiceCode Snippets
$stop = "stop-service 'MSOLAP$TABULAR'"
Invoke-Command -ComputerName $server { $stop }$MyService = 'W32Time'
$get = [scriptblock]::create( "param(`$servicename) Stop-Service `$servicename")
Invoke-Command -ComputerName sql2014-01 -ScriptBlock $get -ArgumentList $MyService$MyService = 'W32Time'
Invoke-Command -ComputerName SQL2014-01 -ScriptBlock {param($servicename); Stop-Service $servicename } -ArgumentList $MyServiceContext
StackExchange Database Administrators Q#159898, answer score: 2
Revisions (0)
No revisions yet.