Powershell 当-NoTypeInformation*未*传递给ConvertTo Csv或Export Csv时,在什么情况下需要发出信息?
今天我突然想到,在多年来习惯性地将Powershell 当-NoTypeInformation*未*传递给ConvertTo Csv或Export Csv时,在什么情况下需要发出信息?,powershell,csv,serialization,typeinfo,export-csv,Powershell,Csv,Serialization,Typeinfo,Export Csv,今天我突然想到,在多年来习惯性地将-NoTypeInformation传递给/以防止发出不需要的注释行之后,也许/能够用其原始属性类型重建对象(而不是所有对象都是字符串)要是我没有隐藏那个类型的信息就好了。我试了一下 PS> Get-Service | ConvertTo-Csv …而且,由于在很长一段时间内没有看到通过省略-NoTypeInformation而发出的信息,因此提醒您,它只包括输入对象的类型(实际上只是第一个对象),而不包括成员的类型 #TYPE System.Servi
-NoTypeInformation
传递给/以防止发出不需要的注释行之后,也许/能够用其原始属性类型重建对象(而不是所有对象都是字符串
)要是我没有隐藏那个类型的信息就好了。我试了一下
PS> Get-Service | ConvertTo-Csv
…而且,由于在很长一段时间内没有看到通过省略-NoTypeInformation
而发出的信息,因此提醒您,它只包括输入对象的类型(实际上只是第一个对象),而不包括成员的类型
#TYPE System.ServiceProcess.ServiceController
"Name","RequiredServices","CanPauseAndContinue","CanShutdown","CanStop","DisplayName","DependentServices","MachineName","ServiceName","ServicesDependedOn","ServiceHandle","Status","ServiceType","StartType","Site","Container"
...
将序列化结果与类型信息进行比较,然后反序列化
PS> Get-Service | ConvertTo-Csv | ConvertFrom-Csv | Get-Member
TypeName: CSV:System.ServiceProcess.ServiceController
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
CanPauseAndContinue NoteProperty string CanPauseAndContinue=False
CanShutdown NoteProperty string CanShutdown=False
CanStop NoteProperty string CanStop=True
Container NoteProperty object Container=null
DependentServices NoteProperty string DependentServices=System.ServiceProcess.ServiceController[]
DisplayName NoteProperty string DisplayName=Adobe Acrobat Update Service
MachineName NoteProperty string MachineName=.
Name NoteProperty string Name=AdobeARMservice
RequiredServices NoteProperty string RequiredServices=System.ServiceProcess.ServiceController[]
ServiceHandle NoteProperty string ServiceHandle=
ServiceName NoteProperty string ServiceName=AdobeARMservice
ServicesDependedOn NoteProperty string ServicesDependedOn=System.ServiceProcess.ServiceController[]
ServiceType NoteProperty string ServiceType=Win32OwnProcess
Site NoteProperty string Site=
StartType NoteProperty string StartType=Automatic
Status NoteProperty string Status=Running
PS> Get-Service | ConvertTo-Csv -NoTypeInformation | ConvertFrom-Csv | Get-Member
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
CanPauseAndContinue NoteProperty string CanPauseAndContinue=False
CanShutdown NoteProperty string CanShutdown=False
CanStop NoteProperty string CanStop=True
Container NoteProperty object Container=null
DependentServices NoteProperty string DependentServices=System.ServiceProcess.ServiceController[]
DisplayName NoteProperty string DisplayName=Adobe Acrobat Update Service
MachineName NoteProperty string MachineName=.
Name NoteProperty string Name=AdobeARMservice
RequiredServices NoteProperty string RequiredServices=System.ServiceProcess.ServiceController[]
ServiceHandle NoteProperty string ServiceHandle=
ServiceName NoteProperty string ServiceName=AdobeARMservice
ServicesDependedOn NoteProperty string ServicesDependedOn=System.ServiceProcess.ServiceController[]
ServiceType NoteProperty string ServiceType=Win32OwnProcess
Site NoteProperty string Site=
StartType NoteProperty string StartType=Automatic
Status NoteProperty string Status=Running
…返回不带类型信息的序列化结果,然后反序列化
PS> Get-Service | ConvertTo-Csv | ConvertFrom-Csv | Get-Member
TypeName: CSV:System.ServiceProcess.ServiceController
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
CanPauseAndContinue NoteProperty string CanPauseAndContinue=False
CanShutdown NoteProperty string CanShutdown=False
CanStop NoteProperty string CanStop=True
Container NoteProperty object Container=null
DependentServices NoteProperty string DependentServices=System.ServiceProcess.ServiceController[]
DisplayName NoteProperty string DisplayName=Adobe Acrobat Update Service
MachineName NoteProperty string MachineName=.
Name NoteProperty string Name=AdobeARMservice
RequiredServices NoteProperty string RequiredServices=System.ServiceProcess.ServiceController[]
ServiceHandle NoteProperty string ServiceHandle=
ServiceName NoteProperty string ServiceName=AdobeARMservice
ServicesDependedOn NoteProperty string ServicesDependedOn=System.ServiceProcess.ServiceController[]
ServiceType NoteProperty string ServiceType=Win32OwnProcess
Site NoteProperty string Site=
StartType NoteProperty string StartType=Automatic
Status NoteProperty string Status=Running
PS> Get-Service | ConvertTo-Csv -NoTypeInformation | ConvertFrom-Csv | Get-Member
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
CanPauseAndContinue NoteProperty string CanPauseAndContinue=False
CanShutdown NoteProperty string CanShutdown=False
CanStop NoteProperty string CanStop=True
Container NoteProperty object Container=null
DependentServices NoteProperty string DependentServices=System.ServiceProcess.ServiceController[]
DisplayName NoteProperty string DisplayName=Adobe Acrobat Update Service
MachineName NoteProperty string MachineName=.
Name NoteProperty string Name=AdobeARMservice
RequiredServices NoteProperty string RequiredServices=System.ServiceProcess.ServiceController[]
ServiceHandle NoteProperty string ServiceHandle=
ServiceName NoteProperty string ServiceName=AdobeARMservice
ServicesDependedOn NoteProperty string ServicesDependedOn=System.ServiceProcess.ServiceController[]
ServiceType NoteProperty string ServiceType=Win32OwnProcess
Site NoteProperty string Site=
StartType NoteProperty string StartType=Automatic
Status NoteProperty string Status=Running
…唯一的区别是TypeName
从CSV:System.ServiceProcess.ServiceController
(与#type
注释前缀为CSV:
指定的类型相同)更改为。所有成员都是相同的,所有属性的类型都是String
,在这两种情况下,您都有反序列化的对象,这些对象不包含原始对象的方法,也不以任何方式连接到原始对象或其代理
显然,微软认为,不仅需要在CSV输出中包含这种类型的信息,而且默认情况下应该这样做。因为我不能真正问“他们为什么这么做?”,我想知道的是,这些信息怎么可能有用?*-Csv
序列化cmdlet是否将其用于除设置每个对象的TypeName
之外的任何其他用途?为什么我希望在CSV输出中“带内”传递此类型信息,而不是仅仅知道此包含服务信息的文件来自实例,或者甚至不关心原始类型是什么,只要它具有我期望的属性
我能想到的仅有两种使用情形是,如果您收到一个由未知PowerShell脚本创建的CSV文件,那么具有类型信息可以帮助确定用于生成数据的应用程序/库/模块,或者如果您具有一个可笑的通用脚本,试图根据类型信息刷新任意输入,像这样
Import-Csv ... `
| ForEach-Object -Process {
# Original type name of the first record, not necessarily this record!
$firstTypeName = $_.PSObject.TypeNames[0];
if ($firstTypeName -eq 'CSV:System.ServiceProcess.ServiceController')
{
Get-Service ...
}
elseif ('CSV:System.IO.DirectoryInfo', 'CSV:System.IO.FileInfo' -contains $firstTypeName)
{
Get-ChildItem ...
}
elseif ($firstTypeName -eq 'CSV:Microsoft.ActiveDirectory.Management.ADObject')
{
Get-ADObject ...
}
...
}
…但这些都不是很有说服力的例子,特别是考虑到,正如我所指出的,这种类型的信息被认为是如此重要,以至于默认情况下会包括在内。还有其他我没有想到的用例吗?或者这仅仅是一个“包含它而不需要它比需要它而不包含它更好(也更便宜)”的例子
相关:讨论将
-NoTypeInformation
作为未来版本(根据cmdlet文档,为6.0)的默认设置,还有一个明显的共识是,如果你有一个东西
,它能理解标题,也知道如何构造该类型的其他东西,那么省略-NoTypeInformation是没有意义的,然后,第一个事物
可以从字符串表示中创建一个或多个其他事物
s。我不是说它有用,但我是说如果.csv可以在其他类型的文件不可用的情况下使用,那么这是一个潜在的用途。我可能会也可能不会真的这样做,原因与我在这里回答你的问题时提到的类似。是的。。。10年前的怪异设计决策。我怀疑在6.1中,该开关在默认情况下是关闭的(考虑到它在当前环境中始终处于启用状态,并且只有当系统管理员忘记时,您才会看到诅咒他们导出CSV)因此,您的意思是,即使ConvertFrom Csv
/Import Csv
不能或根本不将Csv数据重新水化为原始类型的实例,您可能有一个类/脚本/“东西”,可以并使用Csv派生的数据初始化真实对象的属性?因此,“东西”能够构造类型和属性都完全由CSV数据指定的对象,而不是硬编码的对象。我看得出来,;可能是一个.csv
文件目录,表示构成应用程序数据的各种类型特定集合?或者是所有相同类型对象的集合。它有可能比XML或JSON更详细,因为您有列。。。呃。。。属性名称只列出一次,然后将所有值有序地列在一行中。如果您有许多可选属性以逗号字符串的形式出现,那么这个值会下降一点;如果您在每行上有非常不同的类型,那么这个值会下降一点。