Home Database Mysql Tutorial SSAS下玩转PowerShell(二)

SSAS下玩转PowerShell(二)

Jun 07, 2016 pm 03:56 PM
powershell Simple

上一篇中简单的介绍了SSAS下的PowerShell,这一篇会演示更多的操作,比如根据当前时间创建备份,使用变量去指定处理哪一个分区,以及用XMLA脚本去创建分区,和在PowerShell中调用PowerShell脚本。 原文地址: http://www.mssqltips.com/sqlservertip/2939/au

上一篇中简单的介绍了SSAS下的PowerShell,这一篇会演示更多的操作,比如根据当前时间创建备份,使用变量去指定处理哪一个分区,以及用XMLA脚本去创建分区,和在PowerShell中调用PowerShell脚本。

原文地址:

http://www.mssqltips.com/sqlservertip/2939/automate-sql-server-analysis-services-tasks-with-powershell--part-2/?utm_source=dailynewsletter&utm_medium=email&utm_content=headline&utm_campaign=20140328 

在SSAS下使用PowerShell变量

首先来看下面的代码:

$mydate="ssas powershell"

这里我们创建了一个名字为$mydate的变量,$前缀表名这是一个变量,然后给这个变量赋一个值"ssas powershell"。

如果需要显示变量$mydte的值只需要把变量的名字敲出来然后按ENTER。

$mydate

\

接下来把当前日期赋给这个变量。

$mydate=Get-Date

通过Get-Date方法获取到当前日期,并且将其赋给$mydate变量。PowerShell是不需要为变量指定类型的:

\

默认显示的日期类型是长类型的,并且格式会遵从服务器的配置格式。通过下面的命令可以将其转换成yyyyddMM的格式:

$mydate=Get-Date –format yyyyddMM

MM在这里是大写主要是为了跟分钟的小写mm区分开。

验证下格式:

$mydate

\

这样我们就获取到了一个格式为yyyyddMM的保存有当前日期的变量。

接下来需要做的就是根据这个当前时间来创建备份。

backup-asdatabase d:\awdb-$mydate.abf "AdventureWorksDW2012Multidimensional-EE" -AllowOverwrite –ApplyCompression

\

这样一个待用当前时间编辑的文件名备份就被创建在了d盘上。同样在上面的这个命令中,指派了数据库的名称,允许覆盖标记以及是否启用压缩。

\

使用PowerShell处理SSAS的一个分区

跟上一篇的不同,这里我们用一个变量名引入到命令中,来实现动态的分区处理。

首先我们来关注下SSAS的分区,查看下图,我们可以看到SSAS数据库的层级极结构:

\

接下来我们要处理Internet_Orders_2008这个分区。跟上一篇不同我们通过变量的方式$myyear传送2008这个值到命令中然后处理相应的分区。

$myyear=2008

Invoke-ProcessPartition -Name "Internet_Orders_"+$myyear -MeasureGroupname "Internet Orders" -CubeName "Adventure Works" -database "AdventureWorksDW2012Multidimensional-EE" -ProcessType "ProcessFull"

在上面的命令中,变量"Internet_Orders_$myyear"对应的就是分区名"Internet_Orders_2008"。度量值组是"Internet Orders",CubeName是"Adventure Works"。

通过PowerShell调用XMLA脚本

XMLA最主要的一个用途就是创建和处理SSAS对象。在这个示例中我们生成一个脚本来创建分区,然后在PowerShell里执行它。首先,创建一个生成分区"Internet_Orders_2008"的脚本。

\

然后,对这个XMLA脚本进行修改,找到标识分区名称为2008的地方,将其改成2009。

\

[译注]:原文这里没有提到,就是下面的QueryDefinition对应的那段SQL代码也是需要修改的,所以实际项目中,需要根据实际情况,Name和QueryDefinition都需要修改,这里的修改跟UI界面中的都能对应的上,所以这里不做详细介绍。

将这段脚本保存下来,比如:d:\powershell2\createPartition.xmla目录,然后用PowerShell的Invoke-ASCMD命令运行这个脚本。

Invoke-ASCmd -InputFile:"d:\powershell2\createPartition.xmla"

Invoke-ASCMD命令用于在PowerShell中运行SSAS命令,比如XMLA脚本,应用立方体的MDX脚本以及应用数据挖掘的DMX脚本。如果此时在SSMS中刷新一下SSAS实例,可以看到新创建的分区。

\

 

在PowerShell中运行MDX查询

首先生成一段MDX查询,在SSMS中浏览Adventure Works立方体。

\

拖拽Internet Order Count度量值到查询面板中。

\

点击设计模式图标就可以看到对应的MDX脚本。

\

把脚本拷贝出来然后保存到文本文件中,比如d:\powershell2\" directory。用如下命令运行这个mdx脚本。

Invoke-ASCmd -Database "AdventureWorksDW2012Multidimensional-EE" -InputFile:"d:\powershell2\mymdx.mdx"| Out-file d:\Results\XMLAQueryOutput.xml

这里我们所做的就是在AdventureWorksDW2012Multidimensional数据库中运行这个MDX查询,查询结果以XML的方式显示在文件XMLAQueryOutput.xml中。

查询的结果可以在XML文件中找到。

\

[译注]

原文介绍的都是很简单的操作,但是对于自动化任务来说还是很方便的,不如动态创建分区,动态处理分区等等,变量的引入可以加入更多的程序流程在里面,从而实现更复杂的功能。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
Unable to delete ISO file opened in system: How to fix it Unable to delete ISO file opened in system: How to fix it Apr 16, 2023 pm 02:25 PM

What is an ISO file? An ISO file is a file that contains an exact copy of an optical disc, such as a DVD or CD. This type of disc image can be installed on your computer and appear as if you have an optical drive connected to your computer and a CD or DVD containing data inserted into the drive. ISO files can also be used to burn new copies of the DVDs or CDs from which they were created, or simply to contain a large number of files in a single file that is easy to download. ISO files are often quite large; when you're done with them, you'll most likely want to

The easiest way to query the hard drive serial number The easiest way to query the hard drive serial number Feb 26, 2024 pm 02:24 PM

The hard disk serial number is an important identifier of the hard disk and is usually used to uniquely identify the hard disk and identify the hardware. In some cases, we may need to query the hard drive serial number, such as when installing an operating system, finding the correct device driver, or performing hard drive repairs. This article will introduce some simple methods to help you check the hard drive serial number. Method 1: Use Windows Command Prompt to open the command prompt. In Windows system, press Win+R keys, enter "cmd" and press Enter key to open the command

Fix: PowerShell cannot be opened or PowerShell has stopped working in Windows 11/10 Fix: PowerShell cannot be opened or PowerShell has stopped working in Windows 11/10 Apr 24, 2023 pm 07:49 PM

Many users have reported issues with Windows PowerShell stopping working or not opening at all. One of the most common reasons for seeing the PowerShell has stopped working message is that there is a hidden virus on your computer. This in turn will slow down your system and other processes on the system will stop functioning. The error message does not provide any information about the cause of this problem, making it difficult to resolve. If you are annoyed with PowerShell not running or opening issues on your PC, then this article is for you. In this article, we have discussed some of the solutions you can use to fix this error. Fix 1 – Disable and re-enable Windows PowerShell 1. Use Ctr

How to use Nishang, the PowerShell penetration testing tool How to use Nishang, the PowerShell penetration testing tool May 13, 2023 am 10:58 AM

Initial PowerShell, first let's understand the concept: PowerShell can be regarded as an upgraded version of cmd (bat scripting language), which is a scripting language on the Windows platform. It is object-oriented and closely related to .NetFrameWork. It can also be thought of as the bashshell on Windows. Windows PowerShell is a command line shell and scripting environment that enables command line users and script writers to take advantage of the power of the .NET Framework. It introduces a number of very useful new concepts, further extending what you get in the Windows Command Prompt and WindowsHost environments

How to enable an administrator account on Windows 11 How to enable an administrator account on Windows 11 Apr 15, 2023 pm 07:46 PM

How to Enable or Disable an Administrator Account on Windows 11 Using PowerShell One of the quickest ways to enable an administrator account on Windows 11 is to use PowerShell. You can also use the newer Windows Terminal or, if you prefer, the older Command Prompt (cmd) tool. You need to make sure you run these commands from a user account that already has administrator rights. If you are using a standard user account, you will need to switch to another user account to run this tool. To enable an administrator account using PowerShell on Windows 11: Open by clicking the Start button or pressing the Windows key

How to open PowerShell as administrator on Windows 11 How to open PowerShell as administrator on Windows 11 May 10, 2023 pm 06:40 PM

How to Open PowerShell as Administrator on Windows 11 Running PowerShell with elevated or administrator-level permissions allows you to run commands that bypass Windows file protection, such as those used for Windows installation files. You can run PowerShell by opening PowerShell directly or by opening the new Windows Terminal application, which uses PowerShell to run commands by default. Open PowerShell as administrator on Windows 11: Open the start menu and type powershell or terminal. from the right

PowerShell deployment fails with HRESULT 0x80073D02 issue fixed PowerShell deployment fails with HRESULT 0x80073D02 issue fixed May 10, 2023 am 11:02 AM

Do you see this error message "Add-AppxPackage: Deployment failed with HRESULT: 0x80073D02, The package cannot be installed because the resource it modifies is currently in use. Error 0x80073D02..." in PowerShell when you run the script? As the error message states, this does occur when the user attempts to re-register one or all WindowsShellExperienceHost applications while the previous process is running. We've got some simple solutions to fix this problem quickly. Fix 1 – Terminate the experience host process You must terminate before executing the powershell command

What is the difference between powershell and cmd What is the difference between powershell and cmd Jan 11, 2023 pm 02:23 PM

Differences: 1. When running Cmd, it only takes up less than 1M of memory; when using PowerShell, it takes up about 20M of memory. 2. Cmd does not support syntax highlighting, but PowerShell does. 3. When using Cmd to run some more complex and time-consuming commands, when you drag the command line window at will, the content in the window can still remain the same; but PowerShell cannot. 4. Cmd can only use commands in the .net library, nor can it use Linux commands; PowerShell can.

See all articles