MDT部署中命令行脚本的使用。

参考:http://blogs.technet.com/b/deploymentguys/archive/2010/07/07/using-command-shell-scripts-with-mdt.aspx

OK… I’ll admit it, I like to write Command Shell (CMD) scripts when I can.  For me it’s like putting on an old broken-in pair of sneakers… so comfortable and familiar.  Unfortunately, using them in MDT task sequences can be challenging.  I this post I’ll demonstrate some techniques and helper scripts that can bring Command Shell scripts into the MDT era.  There are four main tasks that I’ll address today: calling other scripts and executables, getting Task Sequence Variables, setting Task Sequence Variables, and logging.

CALLING SCRIPTS AND EXECUTABLES

A big issue with running scripts and executables from CMD scripts in MDT is calling them from the correct path.  The command shell does not support a UNC path as the current directory and defaults to %SystemRoot%\System32 when running a CMD script in this way.  In order to call scripts and executables in the same folder as the CMD script itself you should always use the full path to the scripts and executables.  To prevent having to hard code these paths, I use the following code to set the folder where the CMD script resides as an environment variable:

set SCRIPTDIR=%~dp0
set SCRIPTDIR=%SCRIPTDIR:~0,-1%

This allows you “variablize” the paths to scripts and executables in the same folder (or subfolders) as the CMD script.  For example, to run foo.exe in the same folder as the CMD script you would now use this syntax:

“%SCRIPTDIR%\foo.exe”

GETTING TASK SEQUENCE VARIABLES

It would obviously be very convenient if you could access Task Sequence Variables as environment variables.  To that end I created a script, EchoTSVariables.wsf, that will echo variable=value lines for all Task Sequence Variables (plus the current MDT log path) to the console.  This can then be called from a FOR statement to set these as environment variables:

for /F “tokens=1,2* delims==” %%i in (‘cscript //nologo “%SCRIPTDIR%\EchoTSVariables.wsf”‘) do set %%i=%%j>nul

SETTING TASK SEQUENCE VARIABLES

Setting Task Sequence Variables from a CMD script requires a similar helper script.  Fortunately, MDT includes just such a script.  ZTISetVariable.wsf can be called directly from a CMD script:

cscript //nologo “%SCRIPTDIR%\ZTISetVariable.wsf” /VariableName:Foo /VariableValue:Bar

This will set a Task Sequence Variable named Foo to a value of Bar.  For this new variable to be available in the CMD script environment, you would have to rerun the FOR statement withEchoTSVariables.wsf again.

LOGGING

For logging the output of executables and for logging messages directly, I have used native CMD script code plus a small helper script, EchoDateTimeVars.vbs.  This script simply echoes date and time variables in a locale-independent format.  I then created CMD functions like the one below:

:LOGINFO

    set INPUTTEXT=%*
    echo %INPUTTEXT%
    for /F “tokens=*” %%i in (‘cscript //nologo “%SCRIPTDIR%\EchoDateTimeVars.vbs”‘) do set %%i>nul
    echo ^<^^![LOG[%INPUTTEXT%]LOG]^^!^>^<time=”%HOUR%:%MINUTE%:%SECOND%.000+000″ date=”%MONTH%-%DAY%-%YEAR%” component=”%COMPONENT%” context=”" type=”1″ thread=”" file=”%COMPONENT%”^>>>”%LOGFILE%”

goto :EOF

This logs an information message in a log with the same base name as the CMD script.  This function depends on having first run EchoTSVariables.wsf and then the following commands near the beginning of the script to set the COMPONENT and LOGFILE variables:

set LOGFILE=%LOGPATH%\%~n0.log
set MASTERLOG=%LOGPATH%\BDD.log
set COMPONENT=%~n0

There are also LOGWARNING and LOGERROR functions as well.  These are all called using this syntax:

call :LOGINFO Info text I want to log in SMS log format right now please
call :LOGWARNING Warning text I want to log in SMS log format right now please
call :LOGERROR Error text I want to log in SMS log format right now please

I created two functions for running executables, logging the output and checking the return code. The first, RUNANDLOG, will run the executable, log the output to a single log entry, and set the environment variable RETURNCODE based on the errorlevel after the command is executed.  The second, RUNANDLOGLINEBYLINE, log the output line by line.  This is useful when the output of a command is Unicode (like wmic) but needs to be logged as ANSI in the MDT logs.  Unfortunately, RUNANDLOGLINEBYLINE does not allow the capture of the return code of the command.

Finally, the log file created by this script is appended to the master MDT log, BDD.log, using the following command

if exist “%MASTERLOG%” copy “%MASTERLOG%” + “%LOGFILE%” “%MASTERLOG%” /y

SUMMARY

I have provided a sample script, MDTCmdScript.cmd, in the attachment to this post.  It demonstrates all of these techniques mentioned above.  When using this you should place the script commands you want to run between the Begin main script and Finish main scriptcomments.  I have also included all the scripts needed to make this work (except forZTISetVariable.wsf and ZTIUtility.vbs which come with MDT).  All the scripts should be in the same folder.

For some additional tips on using CMD script with MDT, see Michael Niehaus’ blog post here:http://blogs.technet.com/b/mniehaus/archive/2007/03/07/adding-custom-tasks-to-bdd-2007-task-sequences.aspx.

Disclaimer: The information on this site is provided “AS IS” with no warranties, confers no rights, and is not supported by the authors or Microsoft Corporation. Use of included script samples are subject to the terms specified in the Terms of Use.

This post was contributed by Michael Murgolo, a Senior Consultant with Microsoft Services – U.S. East Region.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/298893.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

CSS/DIV网页设计视频教程目录【转】

CSS/DIV网页设计视频教程目录 欢迎您来到前沿视频教室&#xff0c;下面列出的是CSS/DIV网页设计视频教程列表。这组视频教程是配合《精通CSSDIV网页样式与布局》图书制作的&#xff0c;每一课对应于图书中的每一章。 我们还会不断地推出相关的课程&#xff0c; 如果您有兴趣&am…

世界上最伟大的十大公式

▲ 点击查看英国科学期刊《物理世界》曾让读者投票评选了“最伟大的公式”&#xff0c;最终榜上有名的十个公式既有无人不知的112&#xff0c;又有著名的Emc^2&#xff1b;既有简单的-圆周公式&#xff0c;又有复杂的欧拉公式……这些公式不仅仅是数学家和物理学家的智慧结晶&a…

区块链浏览器_全球首款区块链浏览器是啥名堂?傲游6首发体验

[PConline 应用]沉寂多年的傲游浏览器终于准备发布大版本更新了&#xff0c;这次为大家带来的是傲游浏览器6&#xff0c;一个被官方称为“世界第一个区块链浏览器”的新版本&#xff0c;那么&#xff0c;有何特色呢&#xff1f;我们一起来看看吧。傲游浏览器6(后简称傲游6)在安…

ASP.NET Core官方文档+源码,这样学效率高10倍!

.NET Framework停更一年了&#xff0c;学习.NET Core跨平台是大势所趋。如何快速学习ASP.NET Core&#xff1f;在我看来&#xff0c;先看微软ASP.NET Core5.0官方中文文档&#xff0c;再大致读一遍源码&#xff0c;最后结合一些场景做些扩展封装&#xff0c;就可以很轻松的驾驭…

WPF 2D绘图(2)Geometry

Shape是对Geometry的一种封装,Shape本质上还是通过绘制Geometry的形状,然后以填充笔刷来呈现效果 如Rectangle <Path Stroke"Black" StrokeThickness"1" Fill"#CCCCFF"><Path.Data><RectangleGeometry Rect"30,55 100 30&qu…

php ip处理函数,PHP取ip地址函数

从thinkphp3.2中提取出来的取请求ip地址的函数/*** 获取客户端IP地址* param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字* param boolean $adv 是否进行高级模式获取(有可能被伪装)* return mixed*/function get_client_ip($type false, $adv true) {$type …

Windows Server 2008 R2 之三十故障转移群集(二)

五、群集的管理破坏群集&#xff1a;一个两节点的群集&#xff0c;对于第一个节点必须在“节点”中选择“退出”才能退出群集&#xff1b;对于第二个节点&#xff0c;必须在下图选择“破坏群集”才能退出群集。配置服务和应用程序&#xff1a;节点&#xff1a;存储&#xff1a;…

Oracle执行计划详解

---引用------作者&#xff1a;TTT BLOG本文地址&#xff1a;http://blog.chinaunix.net/u3/107265/showart_2192657.html---简介&#xff1a;本文全面详细介绍oracle执行计划的相关的概念&#xff0c;访问数据的存取方法&#xff0c;表之间的连接等内容。并有总结和概述&#…

提升心力---摆脱拿着锤子看啥都是钉子

从程序员到一个团队的管理者&#xff0c;这中间需要经过一次又一次的蜕变&#xff0c;最终才能变得处理任何事情都得心应手。韩非子曾说&#xff1a;下君用己之力、中君用人之力、上君用人之智。大部分的管理者可能都处在用人之力的阶段&#xff0c;并向着用人之智前进。最近看…

curd什么意思中文_每日一句英译英:She's a ten什么意思?

每日一句英译英&#xff1a;Shes a ten什么意思&#xff1f;用一句“没头没脑”的英语句子劈头盖脑就问你“中文什么意思&#xff1f;“&#xff0c;这是典型的把英语当纯粹知识学习的例子&#xff0c;也是“只求答案&#xff0c;不问过程”的应试思维模式。“知识海洋”浩瀚无…

你家猫砸东西是不是也专挑贵的砸?

1 这猫也太会了吧&#xff1f;▼2 哥哥坐地上耍赖想让爸爸抱被妹妹出手摆平了▼3 好可爱的校车▼4 也许人家只是想静静呢&#xff1f;▼5 最近很流行的【泼花成画】▼6 妹妹的表情简直学到了精髓▼7 介绍一下&#xff0c;这是一只阿拉斯猪和阿拉斯小猪崽儿▼你点的每个赞…

C++巧用do...while(0)

在C中&#xff0c;有三种类型的循环语句&#xff1a;for, while, 和do...while&#xff0c; 但是在一般应用中作循环时&#xff0c; 我们可能用for和while要多一些&#xff0c;do...while相对不受重视。 但是&#xff0c;最近在读我们项目的代码时&#xff0c;却发现了do..…

SQL Server 分区表 处理海量数据

你是不是也在苦苦寻求优化自己的SQL Server数据库的方法?如果你的数据库里有不少非常大的表格&#xff0c;分区功能能够帮到你很大的忙…… 【IT专家网独家】你是不是也在苦苦寻求优化自己的SQL Server数据库的方法?如果你的数据库里有不少非常大的表格&#xff0c;分区功能能…

php curl实现get请求,PHP中使用curl实现Get和Post请求 | 严佳冬

一、基本结构1、初始化curl_init()2、设置变量curl_setopt() 。最为重要&#xff0c;一切玄妙均在此。有一长串curl参数可供设置&#xff0c;它们能指定URL请求的各个细节。执行并获取结果3、执行curl_exec()4、释放cURL句柄curl_close()二、Get方式实现//初始化$ch curl_init…

第十一周项目3-程序的多文件组织

下面的程序中的代码分别保存在两个文件中&#xff0c;利用多文件的方式组织。 mian.cpp /**Copyright (c) 2014,烟台大学计算机学院*All gight reserved.*文件名称&#xff1a;temp.cpp*作者&#xff1a;邵帅*完成时间&#xff1a;2014年11月5日*版本号&#xff1a;v1.0 */ #in…

st link v2引脚连接_ST-Link资料02_ST-Link固件介绍,及固件命名规则

说明&#xff1a;本文原创作者『strongerHuang』本文首发于微信公众号『嵌入式专栏』&#xff0c;同时也更新在我的个人网站&#xff1a;EmbeddedDevelop一、写在前面一些人可能看了上一篇关于ST-Link的内容&#xff0c;就会产生疑惑。。。ST-Link有那么多硬件版本&#xff0c;…

Redis 使用 Lua 脚本进行原子操作

Redis 使用 Lua 脚本进行原子操作Intro之前写过一篇文章也是 Redis 使用 LUA 脚本实现分布式的 CAS 操作&#xff0c;可以参考&#xff1a;基于 Redis 实现 CAS 操作最近使用 Redis 的时候有一个需求&#xff0c;只有值发生变化的时候才更新&#xff0c;如果要更新的值和现在的…

奇妙的数学动图,美到令人窒息!

全世界只有3.14 % 的人关注了爆炸吧知识来源&#xff1a;制造科技原理&#xff0c;版权原作者所有。如有侵权联系删除&#xff01;论文查重券 限时免费领 官方授权 安全保障 真实有效点「在看」的人都变好看了哦

matlab井深测量,一种井深测量系统及方法与流程

本申请涉及钻井技术领域&#xff1a;&#xff0c;尤其涉及一种井深测量系统及方法。背景技术&#xff1a;&#xff1a;在钻井施工中过程中&#xff0c;施工人员需要实时了解井深的变换情况&#xff0c;因此&#xff0c;需要在钻井的过程中实时对井深进行测量。。目前&#xff0…

Log4Net使用指南

声明&#xff1a;本文内容主要译自Nauman Leghari的Using log4net&#xff0c;亦加入了个人的一点心得(节3.1.4)。请在这里下载示例代码 1 简介 1.1 Log4net的优点&#xff1a; 几乎所有的大型应用都会有自己的用于跟踪调试的API。因为一旦程序被部署以后…