VB.net和powershell变量

时间:2016-11-08 15:56:37

标签: vb.net powershell

我有一个运行VB.net的Visual Studio表单,我正在收集设置AD用户所需的信息。最后,这些信息需要简单地传递给Powershell,而不需要返回信息。在此之前,我需要它来检查是否已经将某个打印机代码分配给某人,然后再将其提交给其他用户。我为它编写了一个简单的PowerShell脚本。 (我们使用Pager字段存储打印机代码。)

Import-Module ActiveDirectory
$Page = $args[0]
Get-ADUser -Filter { Pager -like $Page } | FT Name

我设置了我找到的代码{{3}},并尝试将其修改为我的脚本,但它一直在崩溃

Dim results As Collection(Of PSObject) = MyPipeline.Invoke()

它给了我:System.Management.Automation.dll中发生了'System.Management.Automation.ParseException'类型的未处理异常

如果我运行他的小6 + 5基本示例脚本,它可以工作,但是当我尝试检索信息并返回一个名称时,它不喜欢它。如果它找到了,我怎么能让它返回该人的名字?由于它不会运行,我甚至不确定是否传递打印机代码,因为$ args [0]即将运行。

1 个答案:

答案 0 :(得分:0)

您的results期待PowerShell对象的集合。将Get-ADUser命令传递给Format-Table时,它会将对象有效地剥离为字符串流。尝试不使用| FT Name

 Import-Module ActiveDirectory  #if you're using powershell 3 or later, this may be redundant
 # $Page = $args[0] # don't need to do this
 $results = Get-ADUser -Filter { Pager -like $args[0] }
 Write-Verbose $results
 #Write-Verbose $results.Name  #try this if the above one works

更新

Write-Verbose可能会导致问题。

试试这个:

 Get-ADUser -Filter { Pager -like $args[0] }

只有一行作为总PS码。 (假设你有PowerShell 3.0或更高版本,你不需要Import-Module)该行将返回TypeName: Microsoft.ActiveDirectory.Management.ADUser类型的对象(来自`Get-ADUser username | Get-Member)。

您也可以直接使用.Net对象类型,无需PowerShell。除了我选择使用PowerShell之外,我对.NET知之甚少。

使用.NET info from MSDN访问AD。