将NoteProperty和计算列对象转换为字符串

时间:2016-02-08 03:35:24

标签: powershell powershell-v5.0

我想使用Write-ObjectToSQL模块在SQL Server表中导入文件信息和MD5哈希。如果可能的话,我试图在一行中这样做。这就是我所拥有的:

gci -file|select Directory,Name,Extension,Length,CreationTime,LastAccessTime,LastWriteTime,@{Name='MD5'; Expression={Get-FileHash $_ -Algorithm MD5|select Hash}}|Write-ObjectToSQL -Server ABC -Database PDOC -TableName Files

以下是我在SQL Server中获得的内容:enter image description here

我没有获得MD5列和目录列。以下是Get-Member信息:

PS C:\Users\pollusb> gci -file|select Directory,Name,Extension,Length,@{Name='MD5'; Expression={Get-FileHash $_ -Algorithm MD5|select Hash}}|gm

   TypeName: Selected.System.IO.FileInfo

Name        MemberType   Definition
----        ----------   ----------
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()
GetType     Method       type GetType()
ToString    Method       string ToString()
Directory   NoteProperty DirectoryInfo Directory=C:\Users\pollusb
Extension   NoteProperty string Extension=.pdoc
Length      NoteProperty long Length=65
MD5         NoteProperty Selected.System.Management.Automation.PSCustomObject MD5=@{Hash=C30CCC4447297D94FE10F54C18DBD5F9}
Name        NoteProperty string Name=345.pdoc

因此,SQL Server将只导入非对象的NoteProperties。

现在,如果我们看一下Write-ObjectToSQL之前的内容。我删除了时间列以获得更好的结果。

PS C:\Users\pollusb> gci -file|select Directory,Name,Extension,Length,@{Name='MD5'; Expression={Get-FileHash $_ -Algorithm MD5|select Hash}} |
ft -AutoSize

Directory        Name             Extension Length MD5
---------        ----             --------- ------ ---
C:\Users\pollusb 345.pdoc         .pdoc         65 @{Hash=C30CCC4447297D94FE10F54C18DBD5F9}
C:\Users\pollusb deletedRoute.txt .txt          51 @{Hash=DE585876A6249B9FDF176697F5D35EA4}
C:\Users\pollusb dir.txt          .txt       11815 @{Hash=77F3CE7F713966D4F8E39D12E1D77947}
C:\Users\pollusb open.ps1         .ps1         116 @{Hash=F5B0126D5B0A5040BE34FDC257ADD7F4}
C:\Users\pollusb pdoc.bat         .bat          34 @{Hash=E3630400A370F6D94ECE198407FDB151}

我的问题是有一种方法可以将MD5列转换为只有MD5结果而不是@ {Hash =和结束} AND来转换字符串中的Directory列吗?

1 个答案:

答案 0 :(得分:0)

将目录名转换为字符串而不是System.IO.DirectoryInfo,将Select Directory替换为自定义目录:

@{n='Directory'; e={$_.Directory -as [String]}}

获取文件哈希后,展开属性:

Get-FileHash $_ -Algorithm MD5 | select -ExpandProperty Hash

这将只返回实际的哈希值,而不是包含HASH属性的MD5对象。

添加新属性:

gci -file admin.bat | select @{n='Directory'; e={$_.Directory -as [String]}},
   Name,
   Extension,
   Length,
   @{Name='MD5'; Expression={Get-FileHash $_ -Algorithm MD5 | select -ExpandProperty Hash}
} | gm

它为您提供了一些String NoteProperty':

Name        MemberType   Definition                                        
----        ----------   ----------                                        
Equals      Method       bool Equals(System.Object obj)                    
GetHashCode Method       int GetHashCode()                                 
GetType     Method       type GetType()                                    
ToString    Method       string ToString()                                 
Directory   NoteProperty System.String Directory=D:\                       
Extension   NoteProperty System.String Extension=.bat                      
Length      NoteProperty System.Int64 Length=2901                          
MD5         NoteProperty System.String MD5=876056EB083579DFF07E85015D4C0272
Name        NoteProperty System.String Name=admin.bat