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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM