AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 159898
Accepted
John G Hohengarten
John G Hohengarten
Asked: 2017-01-04 15:47:13 +0800 CST2017-01-04 15:47:13 +0800 CST 2017-01-04 15:47:13 +0800 CST

使用 Invoke-Command 和 $variables 在 PowerShell 中停止、启动或重新启动 SSAS 表格服务实例

  • 772

我正在使用一些 PowerShell 命令/脚本来停止、启动或重新启动 SSAS 表格服务实例,使用Invoke-Command和一些$variables. 最终目标是最终将此代码放入代理作业中,以便于重新启动服务,但这超出了本文的范围——我需要$variables首先使用它。

我在我的源(本地)机器上使用 PowerShell 5.0,并尝试停止/启动/重新启动远程 SSAS 2012 表格服务器实例(11.0.6540.0)。

首先,我以Set-ExecutionPolicy.

Set-ExecutionPolicy -Scope CurrentUser RemoteSigned -Force

然后,使用硬编码方法只是为了获得正确的语法并在 RDP 进入服务器时观察 SQL Server 配置管理器 (Config Mgr) 以验证命令是否正常工作,我使用以下 PowerShell 代码:

Invoke-Command -ComputerName DevServer { stop-service 'MSOLAP$TABULAR' }

这很好用。我在 Config Mgr 中验证服务停止。

我还验证了以下 2 个 PowerShell 代码脚本是否有效:

Invoke-Command -ComputerName DevServer { start-service 'MSOLAP$TABULAR' }

和

Invoke-Command -ComputerName DevServer { restart-service 'MSOLAP$TABULAR' }

出色的。效果很好。

现在我开始将东西放入变量中,如下所示:

$tabularinstance = 'MSOLAP$TABULAR'

$server = "DevServer"
$stop = "stop-service $tabularinstance"
$start = "start-service $tabularinstance"
$restart = "restart-service $tabularinstance"

然后我显示变量的值以验证预期结果:

# show values
$tabularinstance
$stop
$start
$restart
$server

我在 PowerShell 控制台中返回以下结果:

PS H:\> # show values
$tabularinstance
$stop
$start
$restart
$server
MSOLAP$TABULAR
stop-service MSOLAP$TABULAR
start-service MSOLAP$TABULAR
restart-service MSOLAP$TABULAR
DevServer

好的,所以事情看起来不错。然后我运行下面的 PowerShell 命令,期望它可以工作,但服务器上什么也没发生;Config Mgr 没有显示服务已停止,它仍在运行:

Invoke-Command -ComputerName $server { $stop }

然后我意识到该$tabularinstance变量没有包装撇号,所以我尝试了这个:

$tabularinstance = '''MSOLAP$TABULAR''' # triple apostrophes needed to get the ' escape character and apply the apostrophes to variable

然后重新设置$stop、$start和$restart变量以使用更新后的$tabularinstance变量:

$stop = "stop-service $tabularinstance"
$start = "start-service $tabularinstance"
$restart = "restart-service $tabularinstance"

并再次重新显示它们以验证它们是否包含环绕撇号:

PS H:\> # show values
$tabularinstance
$stop
$start
$restart
$server
'MSOLAP$TABULAR'
stop-service 'MSOLAP$TABULAR'
start-service 'MSOLAP$TABULAR'
restart-service 'MSOLAP$TABULAR'
DevServer

是的,他们有。然后我InvokeCommand再试一次:

Invoke-Command -ComputerName $server { $stop }

消极的。仍然没有工作。在 ConfigMgr 中,服务仍在运行。

我什至想尝试将完整的命令分配给变量,看看它是如何解决的:

$test = "Invoke-Command -ComputerName $server { $stop }"

然后检查结果:

PS H:\> $test
Invoke-Command -ComputerName DevServer { stop-service 'MSOLAP$TABULAR' }

这对我来说也很合适。它看起来与我在硬编码所有内容而不是使用$variables.

那么为什么我的 PowerShell 不能使用该$variables方法工作呢?少了什么东西?

ssas powershell
  • 1 1 个回答
  • 1477 Views

1 个回答

  • Voted
  1. Best Answer
    user507
    2017-01-04T16:49:36+08:002017-01-04T16:49:36+08:00

    在传递变量时,脚本块的工作方式略有不同。您必须告诉命令远程计算机上的变量包含什么。

    同样,如果您想将完整命令作为变量传递,您需要将变量类型设为[scriptblock].

    $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 $MyService
    

    在此处输入图像描述

    • 2

相关问题

  • 在 Microsoft BI 中查找改进材料

  • SSAS:移动 tempdb 数据库

  • OLAP 多维数据集中的布尔字段

  • SQL Server 2008 分析服务 DSO 属性

  • SSAS 维度层次结构导致部署错误

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve