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 2020-06-24 16:01  momingliu11  阅读(902)  评论(0编辑  收藏  举报