简体   繁体   English

离线调试 mdt powershell 脚本

[英]offline debugging mdt powershell scripts

I'm trying to create a way to offline debug PowerShell scripts that are intended for workstation deployment Task Sequences in Microsoft Deployment Toolkit (MDT).我正在尝试创建一种脱机调试 PowerShell 脚本的方法,这些脚本旨在用于 Microsoft 部署工具包 (MDT) 中的工作站部署任务序列。 The scripts use the task sequence environment variables that are retrieved using a ComObject, $tsenv.这些脚本使用使用 ComObject、$tsenv 检索的任务序列环境变量。 The ComObject is only available while a task sequence is running so I would like to create a fake object that works the same as the ComObject returned from the Task Sequence environment and fill it with values that I capture from a previously running task sequence. ComObject 仅在任务序列运行时可用,因此我想创建一个与从任务序列环境返回的 ComObject 相同的假对象,并用我从先前运行的任务序列中捕获的值填充它。 Then I could debug the scripts offline with the values that actually existed in the deployment environment.然后我可以使用部署环境中实际存在的值离线调试脚本。 The values in the running task sequence are returned by name like this,正在运行的任务序列中的值按名称返回,如下所示,

$script_var1 = $tsenv.Value("OSDISK")
$script_var2 = $tsenv.Value("APPLYGPOPACK")
$script_var3 = $tsenv.Value("ASSETTAG")

There are over 150 values.有超过 150 个值。 I was trying to build an array object that would work exactly the same way by outputting all the values from the running task sequence like this,我试图通过从运行的任务序列中输出所有值来构建一个以完全相同的方式工作的数组对象,如下所示,

$tsenv.GetVariables() | Where-Object {$_ -NotLike "_SMSTSTaskSequence"} | % {Write-Output "`$tsenv += New-Object -TypeName psobject -Property @{Name=`"$_`"; Value=`"$($tsenv.Value($_))`"}" >> vars.txt}

This gives me all the values with the names and if I create an empty $tsenv array then include the contents of this vars.txt file, it nearly works.这给了我所有带有名称的值,如果我创建一个空的 $tsenv 数组然后包含这个 vars.txt 文件的内容,它几乎可以工作。 I can see all the values in the $tsenv object, but I can't access them by Name as I can with the ComObject.我可以看到 $tsenv 对象中的所有值,但我不能像使用 ComObject 那样按名称访问它们。 I've tried all the different ways I could think of, hashtable, array, dictionary, etc., but nothing get's me as close as this and it still isn't quite there.我已经尝试了所有我能想到的不同方式,哈希表、数组、字典等,但没有什么比这更接近我了,它仍然不存在。 Anyone have a better way of doing this or a way to get to what I want?任何人都有更好的方法来做到这一点或获得我想要的东西?

First, if you want to export objects to be imported later I highly recommend the Export-CliXml cmdlet.首先,如果您想导出稍后要导入的对象,我强烈推荐Export-CliXml cmdlet。 It will retain things like nested arrays and what not, so if "APPLYGPOPACK" returns an array of strings of what GPOs to apply, it will keep it as an array of strings when you import it later.它将保留嵌套数组等内容,因此如果“APPLYGPOPACK”返回一个包含要应用的 GPO 的字符串数组,它会在您稍后导入时将其保留为字符串数组。 What I would do here is make an empty hashtable, add all of your variables to it, and export as xml it for later reference.我在这里要做的是创建一个空的哈希表,将所有变量添加到其中,然后将其导出为 xml 以供以后参考。

$tsenvHT = @{}
$tsenv.GetVariables() | Where-Object {$_ -NotLike "_SMSTSTaskSequence"} | % {$tsenvHT.Add($_,$tsenv.Value($_))}
$tsenvHT | Export-Clixml .\vars.xml

Then you just use Import-CliXml to restore that data later when you want to test.然后,您只需在以后想要测试时使用Import-CliXml恢复该数据。 I'm going to convert it to a [PSCustomObject] to work well with the next step.我将把它转换为[PSCustomObject]以便在下一步中正常工作。

$tsenv = [pscustomobject](Import-Clixml .\vars.xml)

Next, to reference things like you do with your $tsenv.value() method, you can create a custom object and give it a .Value() method to emulate what your ComObject is doing.接下来,要引用您使用$tsenv.value()方法所做的事情,您可以创建一个自定义对象并为其提供一个.Value()方法来模拟您的 ComObject 正在做什么。

$tsenv | Add-Member -MemberType ScriptMethod -Value {param($VariableName);$This.$VariableName} -Name 'Value'

Then you can call it just like the ComObject to get your variable values:然后你可以像 ComObject 一样调用它来获取你的变量值:

$tsenv.Value("OSDISK")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM