随笔 - 911  文章 - 5  评论 - 94  阅读 - 243万

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
复制代码

 

posted on   momingliu11  阅读(965)  评论(0编辑  收藏  举报
编辑推荐:
· 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
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示