参考: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.

MDT部署中命令行脚本的使用。相关推荐

  1. strace命令_在软件部署中使用 strace 进行调试

    我最喜欢的用来解决"为什么这个软件无法在这台机器上运行?"这类问题的工具就是 strace. -- Simon Arneaud(作者) 我的大部分工作都涉及到部署软件系统,这意味着 ...

  2. 【转】如何在命令行脚本中启动带参数的Windows服务

    我们有一个自己编写的Windows服务,我们希望该服务在启动时可以根据用户输入的参数实现不同的功能. 要实现这样的需求并不是很难,下面这个例子我用来示范如何编写该服务 1 using System; ...

  3. 保障K8s部署中的安全性

    开发人员需要了解如何在容器化应用程序中嵌入控件,以及如何启用运行时保护机制以阻止黑客访问容器化系统. Kubernetes是当前流行和常用的容器编排工具之一.Kubernetes工作负载就像简单的ng ...

  4. xcode 中无法进行虚拟机调试_在软件部署中使用 strace 进行调试

    我最喜欢的用来解决"为什么这个软件无法在这台机器上运行?"这类问题的工具就是 strace. -- Simon Arneaud(作者) 我的大部分工作都涉及到部署软件系统,这意味着 ...

  5. .NET Core部署中你不了解的框架依赖与独立部署

    作者:依乐祝 原文地址:https://www.cnblogs.com/yilezhu/p/9703460.html NET Core项目发布的时候你有没有注意到这两个选项呢?有没有纠结过框架依赖与独 ...

  6. 命令行_Laravel-admin artisan 命令行脚本使用

    本文内容 laravel-admin创建命令行脚本 通过screen管理脚本执行 我们在使用laravel-admin做管理后台开始时难免会遇到需要使用脚本在后台跑一些任务的情况.laravel框架给 ...

  7. 速度与压缩比如何兼得?压缩算法在构建部署中的优化

    压缩在数据传输和存储过程中经常扮演着十分重要的角色,因此提高压缩的效率可以帮助我们节省时间和降低存储成本.本文介绍了压缩算法的优化在构建部署平台的应用,能够帮助研发团队提高研发和交付效率. 背景 通常 ...

  8. Zookeeper安装部署调试命令

    Zookeeper安装部署调试命令 ZooKeeper 常用四字命令: 下载地址: http://www.apache.org/dyn/closer.cgi/zookeeper/ 我选用清华的镜像 h ...

  9. 在软件部署中使用 strace 进行调试 | Linux 中国

    很明显,我们能看到 CPU 主要被 clone 操作消耗了,还可以单独跟踪一下 clone: shell> strace -T -e clone -p <PID> 通过「T」选项可以 ...

最新文章

  1. 漫画 | 一台 Linux 服务器最多能支撑多少个 TCP 连接?
  2. Spring接入RabbitMQ
  3. 【Java12】tomcatservlet(nginx,web.xml,生命周期,适配器优化),requestresponse(请求转发,登陆案例(1),重定向,文件下载)
  4. jsp文件的请求是如何被服务器的JSP容器转换成静态网页的
  5. 【渝粤教育】国家开放大学2018年秋季 0734-22T出纳实务 参考试题
  6. Vue 中使用watch监听$route 无效问题
  7. 题目1003:A+B(字符串转数字)
  8. 一个代码都不用敲的可视化大屏,一口气把工具和模板都送给你
  9. CodeForces 632C The Smallest String Concatenation(水)
  10. 购物网页设计php实验报告,购物网站设计-Web实验报告
  11. Protues 仿真器件
  12. 互联网 年龄歧视_太老了,无法玩:年龄歧视和游戏
  13. 局部替换算法最小生成树
  14. MATLAB 自动控制原理 传递函数单位阶跃响应曲线绘制
  15. keil工具中fromelf生成汇编代码lst文件(armcc)
  16. STM32串口导致电脑鼠标乱飞的解决办法
  17. 【MySQL笔记】视图的创建与使用(VIEW)
  18. pyaudio安装失败的解决方法
  19. unity安卓应用名称多语言本地化
  20. Response to reviewer 回复审稿人

热门文章

  1. ADB原理,Wi-Fi连接,常用命令及拓展
  2. PC端和移动APP端CSS样式初始化
  3. C#摄像头实现拍照功能的简单代码示例
  4. 将 iOS 应用的体积控制在 20MB 以内对于其下载量有很明显的影响吗?
  5. PHP 发送Email的几种方法
  6. 智能云改-docker云迁移实战
  7. 前端JavaScript规范
  8. python作业高级FTP(第八周)
  9. 转仁兄:Binary search and its variation
  10. 【啊哈!算法】之二、插入排序