MDT部署中命令行脚本的使用。
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部署中命令行脚本的使用。的更多相关文章
- 【转】如何在命令行脚本中启动带参数的Windows服务
我们有一个自己编写的Windows服务,我们希望该服务在启动时可以根据用户输入的参数实现不同的功能. 要实现这样的需求并不是很难,下面这个例子我用来示范如何编写该服务 using System; us ...
- 探索Windows命令行系列(3):命令行脚本基础
1.实用功能 1.1.为脚本添加注释 1.2.控制命令的回显 1.3.使用数学表达式 1.4.向脚本传递参数 2.使用变量 2.1.变量的命名及定义 2.2.调用变量 2.3.变量的作用域 3.结构语 ...
- .NET Core部署中你不了解的框架依赖与独立部署
作者:依乐祝 原文地址:https://www.cnblogs.com/yilezhu/p/9703460.html NET Core项目发布的时候你有没有注意到这两个选项呢?有没有纠结过框架依赖与独 ...
- Redmine部署中遇到的问题
Redmine部署文章: 第一篇:Redmine部署 第二篇:Redmine部署中遇到的问题 上一篇文章我写了Redmine怎样部署(点这里直达上一篇文章),这一篇就写一下在Redmine部署中遇到过 ...
- SQL Server Reporting Services:无法检索应用程序文件。部署中的文件已损坏
如果在客户端计算机上启动Microsoft SQL Server 2012的 ClickOnce 版本的 Microsoft SQL Server 报表生成器时出现"无法检索应用程序文件.部 ...
- 如何将cmd中命令输出保存为TXT文本文件
在使用Windows 中的cmd.exe工具时,有时候我们想要把我们的输入命令及结果保存起来, 但是用复制的方法过于麻烦:有时输出数据条数过大,会造成内容自动滚出屏幕,无法阅读,我们可将命令运行的结果 ...
- c#中命令copy已退出,返回值为1
c#中命令copy已退出,返回值为1 本正经的道:董姐刚才你说的修心养性其中的'修心'我 有孕在身刚好由戴梦瑶顶替了她的位置按照的指示 ╋旆呆 湎术葶页 邾箕砜笳 烦璜卿廑 奶奶个腿儿的等下次非让你 ...
- 使用Tomcat-redis-session-manager来实现Tomcat集群部署中的Session共享
一.工作中因为要使用到Tomcat集群部署,此时就涉及到了Session共享问题,主要有三种解决方案: 1.使用数据库来存储Session 2.使用Cookie来存储Session 3.使用Redis ...
- 在微服务系统开发部署中使用Azure RBAC自定义角色
Azure的官方文档介绍了如何创建用于Azure基于角色的访问控制的自定义角色(RBAC Role). 我们也可以根据同样的原理把RBAC细粒度资源管理运用于微服务产品的开发部署中.(https:// ...
随机推荐
- PHP ftp使用
本文章来总结几个利用php中的ftp功能来实现通过FTP上传文件,有需要学习的朋友可参考参考.ftp_get() 函数从 FTP 服务器上下载一个文件.若成功则返回 true,失败则返回 false. ...
- 【洛谷P1378】油滴扩展
搜索-- PS一个坑点:r<=0时并不是舍弃这种情况,而是让r=0 (因为每个点都要放一滴油)(读题啊!) #include<cstdio> #include<cstring& ...
- Java 关键字、标识符、注释、常量与变量、数据类型,算术、赋值、比较、逻辑、位、三元运算符和流程控制、break、continue【3】
若有不正之处,请多多谅解并欢迎批评指正,不甚感激.请尊重作者劳动成果: 本文原创作者:pipi-changing本文原创出处:http://www.cnblogs.com/pipi-changing/ ...
- iOS多线程编程之NSOperation和NSOperationQueue的使用(转自容芳志专栏)
转自由http://blog.csdn.net/totogo2010/ 使用 NSOperation的方式有两种, 一种是用定义好的两个子类: NSInvocationOperation 和 NSBl ...
- 教你把UltraEdit如何注册激活教程及UltraEdit 22.0.0.48 官方中文版下载
UltraEdit 22.0.0.48 官方中文版下载:链接: http://pan.baidu.com/s/1i3f7mZV 密码: r23v2015-5-30号更新 第一.关闭网络连接(或者直接拔 ...
- 【概念笔记】JavaEE - web part2
IT`huhui前言录 续JavaEE - web part1 链接http://www.cnblogs.com/ithuhui/p/5930745.html, 持续修改更新. Cookie 1. 定 ...
- ( 转)基于.NET平台常用的框架整理
自从学习.NET以来,优雅的编程风格,极度简单的可扩展性,足够强大开发工具,极小的学习曲线,让我对这个平台产生了浓厚的兴趣,在工作和学习中也积累了一些开源的组件,就目前想到的先整理于此,如果再想到,就 ...
- jquery 常用插件
50款最有用的 jQuery 插件集锦<表单篇> 50款最有用的 jQuery 插件集锦<网页布局篇> 50款最有用的 jQuery 插件集锦<内容滑块篇> 50款 ...
- Server Develop (七) Linux 守护进程
守护进程 守护进程,也就是通常说的Daemon进程,是Linux中的后台服务进程.它是一个生存期较长的进程,通常独立于控制终端并且周期性地执行某种任务或等待处理某些发生的事件.守护进程常常在系统引导装 ...
- [ACM_数学] LA 3708 Graveyard [墓地雕塑 圈上新加点 找规律]
Description Programming contests became so popular in the year 2397 that the governor of New Earck ...