MDT通过UserExit.vbs调用PowerShell脚本获取变量
1.在D:\DeploymentShare\Scripts下新建需要通过UserExit.vbs脚本中函数调用的PowerShell脚本,名为GetUsername.ps1,该脚本最后输出值为$res
param($a) $res = $a+(gwmi win32_bios).SerialNumber.substring(0,3) write-host $res
2.在UserExit.vbs脚本中新加如下函数,该函数用来执行当前目录下的.ps1脚本,脚本执行过程中会弹出PowerShell窗口,
'通过计算机名获取域账号 Function GetUsername(ps_script_params) ps_script_name = "GetUsername.ps1" currdir = left(wscript.scriptfullname,instrrev(wscript.scriptfullname,"\")-1) '获取当前脚本所在目录 pscommand = currdir + "\" + ps_script_name + " " + ps_script_params cmd = "powershell.exe -executionpolicy bypass -noprofile -noninteractive -file " & pscommand Set shell = CreateObject("WScript.Shell") Set executor = shell.Exec(cmd) '可以接收.ps1脚本结果值,shell.run()可以隐藏窗口,但无法直接获得ps脚本结果值,需要先将值写入到文本中,然后再从文本获取值 executor.StdIn.Close GetUsername = executor.StdOut.ReadAll End Function
3.在CustomSettings.ini中,将GetUsername()函数结果值赋予Administrators001,用于将获取到的用户名添加到本地管理员组中。由于在执行UserExit.vbs脚本过程中获取不到OSDComputername值,所以此处无法实现。
函数调用变量,必须要加双引号,如果值为数字可不加双引号 "%OSDComputername%"
Administrators001=uxin\#GetUsername("%OSDComputername%")#
4.然后运行Testvariables.bat可以查看到Administrators001结果
通过在TS中Run PowerShell Script的方式,可以提前定义Administrators001变量值,如下:
# Determine where to do the logging $TSenv = New-Object -COMObject Microsoft.SMS.TSEnvironment $logPath = $TSenv.Value("LogPath") $logFile = "$logPath\$($myInvocation.MyCommand).log" # Start the logging Start-Transcript $logFile $TSenv.Value("Administrators001")="uxin\lcx6"# Stop logging
gci TSenv: |Out-File \\10.10.1.2\DeploymentShare$\test2.log -Append #输出所有变量到log查看
Stop-Transcript
分类:
系统部署
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2016-06-24 zTree使用
2014-06-24 Sysinternals Suite实用程序工具包
2014-06-24 格式化日期时间字符串 Get-Date -Uformat , -format