zoukankan      html  css  js  c++  java
  • MDT通过UserExit.vbs调用PowerShell脚本获取变量

    1.在D:DeploymentShareScripts下新建需要通过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")="uxinlcx6"# Stop logging 
    gci TSenv: |Out-File \10.10.1.2DeploymentShare$ est2.log -Append #输出所有变量到log查看
    Stop-Transcript
  • 相关阅读:
    STM32F4xx时钟理解
    ubuntu突然卡住
    图像处理的面试问题汇总(6)
    更改IOS于UISearchBar撤消button底、搜索输入文本框背景中的内容和UISearchBar底
    PHP于DIRECTORY_SEPARATOR任务
    iOS8使用Core Graphics实现渐变效果-Swift基础教程
    iOS开发系列--打造自己的“美图秀秀”
    swiftTools
    如何把UIView转成UIImage,解决模糊失真问题
    Android清理内存
  • 原文地址:https://www.cnblogs.com/dreamer-fish/p/13187985.html
Copyright © 2011-2022 走看看