是NLTEST输出无效,新的对象来使用? [英] Is nltest output not valid to use with new-object?

查看:150
本文介绍了是NLTEST输出无效,新的对象来使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行NLTEST命令时,NLTEST /dsgetdc:contoso.com,我得到以下的输出:

DC:\ DC1​​.contoso.com
地址:\ 192.168.0.100
大教堂GUID:1234567a-abc1-4d66-a421-153f0116d8c7
大教堂名称:contoso.com
森林名称:contoso.com
DC站点名称:纽约州
我们的网站名称:纽约州
标志:PDC GC DS LDAP KDC TIMESERV ....等。

我想要做的就是捕捉到直流的输出,最终的地址栏。我尝试以下,但它似乎不喜欢它:

  $ SVC = NLTEST /dsgetdc:contoso.com
$ OBJ =新的对象psobject -property {DC = $ svc.DC}
 

  

新的对象:无法绑定参数'财产'。不能转换   DC = $ svc.DC类型的值System.Management.Automation.ScriptBlock   键入System.Collections.IDictionary。在行:1字符:36   + $ OBJ =新的对象psobject -property {DC = $ svc.DC}   + ~~~~~~~~~~~~   + CategoryInfo:InvalidArgument:(:) [新-对象],ParameterBindingException   + FullyQualifiedErrorId:CannotConvertArgumentNoMessage,Microsoft.P​​owerShell.Commands.NewObjectCommand

所以,很明显,我做了正确的呼叫/使用命令不正确。应我甚至使用新对象我想要做什么?

解决方案

Briantist有一个伟大的答案与细节你是如何对待 NLTEST 的输出问题。由于从 NLTEST 已经是格式化名称输出:值,我建议使用的 ConvertFrom-StringData是

$结果=((NLTEST /dsgetdc:contoso.com)-replace:,=|     位置对象{$ _ -match=})-join`r`n|     ConvertFrom-StringData是 新对象-TypeName PSCustomObject -Property $结果

我们用 -replace:,=所有冒号转换成等号。然后,我们筛选出与不具有等号线。也就是说,以消除任何状态信息,如命令成功完成。 ConvertFrom-StringData是只需要一个字符串,所以我们加入了新线的线条,使该单一字符串。 ConvertFrom-StringData是让我们存储在 $结果的哈希表

接着,就像你的问题和 briantists回答,我们做一个 PsCustomObject 。输出看起来像下面这样。

  DC站点名称:CON
我们的网站名称:CON
DC:\ DCCONTOSO.COM
标志:GC DS LDAP KDC TIMESERV WRITABLE DNS_DC dns_domain的DNS_FOREST CLOSE_SITE FULL_SECRET WS
地址:\ 1254.885.1.478
大教堂名称:CONTOSO.COM
大教堂GUID:0a3be324-f399-4b44-a3f1-b9c5aa6e7c5b
森林名称:CONTOSO.COM
 

就在对象创建的结果保存到一个变量,那么你可以访问喜欢你的属性会在PowerShell中的任何对象。

when running the nltest command "nltest /dsgetdc:contoso.com", I get the following output:

DC: \DC1.contoso.com
Address: \192.168.0.100
Dom Guid: 1234567a-abc1-4d66-a421-153f0116d8c7
Dom Name: contoso.com
Forest Name: contoso.com
DC Site Name: New York
Our Site Name: New York
Flags: PDC GC DS LDAP KDC TIMESERV....etc..

What I'm trying to do is capture the output of the DC and eventually the Address fields. I tried the following but it doesn't seem to like it:

$svc=nltest /dsgetdc:contoso.com 
$obj=new-object psobject -property {DC=$svc.DC}

New-Object : Cannot bind parameter 'Property'. Cannot convert the "DC=$svc.DC" value of type "System.Management.Automation.ScriptBlock" to type "System.Collections.IDictionary". At line:1 char:36 + $obj=new-object psobject -property {DC=$svc.DC} + ~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [New-Object], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.NewObjectCommand

So obviously i'm making the call incorrectly/using the command incorrectly. Should I even use new-object for what I'm trying to do?

解决方案

Briantist has a great answer detailing the issues with how you are treating the output of nltest. Since the output from nltest is already well formatted with name:value I propose the use of ConvertFrom-StringData.

$results = ((nltest /dsgetdc:contoso.com) -replace ":","=" | 
    Where-Object{$_ -match "="}) -join "`r`n" | 
    ConvertFrom-StringData
new-object -TypeName PSCustomObject -Property $results

We use -replace ":","=" to convert all the colons to equal signs. Then we filter out and lines that do not have an equal sign. That is to remove any status message like "The command completed successfully". ConvertFrom-StringData expects a single string so we join the lines with new lines to make that single string. ConvertFrom-StringData makes a hashtable that we store in $results.

Then, just like your question and briantists answer, we make a PsCustomObject. The output would look like the following.

Dc Site Name  : CON
Our Site Name : CON
DC            : \DCCONTOSO.COM
Flags         : GC DS LDAP KDC TIMESERV WRITABLE DNS_DC DNS_DOMAIN DNS_FOREST CLOSE_SITE FULL_SECRET WS
Address       : \1254.885.1.478
Dom Name      : CONTOSO.COM
Dom Guid      : 0a3be324-f399-4b44-a3f1-b9c5aa6e7c5b
Forest Name   : CONTOSO.COM

Just save the results of the object creation into a variable then you can access the properties like you would any object in PowerShell.

这篇关于是NLTEST输出无效,新的对象来使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆