简体   繁体   English

将长度属性添加到选择

[英]Add Length Property to Select

Newbie here...everyone's help and input is greatly appreciated! 这里的新手...非常感谢大家的帮助和投入!

30,000' view: Create Report on "stale data" that can/should be deleted from network. 30,000'视图:创建可以/应该从网络中删除的“陈旧数据”报告。

Step 1. Gather list of folders that begin with either "ST_" or "EDDS1" from a group of servers that includes the FullName, Name & Length values. 步骤1.从一组包含FullName,Name和Length值的服务器中,收集以“ ST_”或“ EDDS1”开头的文件夹列表。

$Path = 
"\\server1.domain.com\b\sqlbackups",
"\\server1.domain.com\b\sqlbackups",
"\\server1.domain.com\b\sqlbackups";

$BackupFolders = @(gci $Path -filter "ST_*" | ?{ $_.PSIsContainer}) | Select FullName,Name,Length
$BackupFolders += @(gci $Path -filter "EDDS1*" | ?{ $_.PSIsContainer}) | Select-Object FullName Name, Length

$BackupFolders | Export-CSV C:\BackupFolders.csv

This pulls in a column for length but there are no values listed in that colum... 这会拉出一列长度,但该列中没有列出任何值。

Step 2. Compare this list to SQL table. 步骤2,将此列表与SQL表进行比较。 If value exists in reference doc (Step 1) & in a specific colum in SQL, then look at "status tab" in SQL. 如果在参考文档(第1步)和SQL中的特定列中存在值,请查看SQL中的“状态标签”。 If the SQL table "status tab" indicates "inactive", output FullName, Name, Length from Step1 and Client, Project Name from SQL table into csv. 如果SQL表的“状态选项卡”指示“无效”,则将FullName,Name,Step1和Client的长度,Project Name从SQL表输出到csv。

$sqlConnection = New-Object System.Data.SqlClient.SqlConnection
$sqlConnection.ConnectionString = "Server=sqlserver\sharename;Database=database;Integrated Security=True"
$sqlConnection.Open()

$sqlCommand = New-Object System.Data.SqlClient.SqlCommand
$sqlCommand.CommandText = "SELECT * FROM [Table].[dbo].[Matters] WHERE [Status] NOT LIKE 'Active%'"
$sqlCommand.Connection = $sqlConnection
$sqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$sqlAdapter.SelectCommand = $sqlCommand
$Dataset = New-Object System.Data.DataSet
$sqlAdapter.Fill($Dataset)
$sqlConnection.Close()

$MatterStatus = @"
C:\MatterStatus.csv
"@

$Dataset.Tables[0] | Select Client,Project,MatterStatus | Export-Csv c:\MatterStatus.csv

Question1 - How do I get the Length Attribute to be listed in my csv created from Step1? 问题1-如何获取要在Step1创建的csv中列出的Length属性?

Question2 - Am I on the right track here? 问题2-我在这里的路线正确吗? Same as Step1, the last value did not propagate...MatterStatus in my csv is blank. 与Step1相同,最后一个值未传播... csv中的MattStatus为空白。

Any suggestions/hints are much appreciated! 任何建议/提示都非常感谢!

According to http://technet.microsoft.com/en-us/library/ff730945.aspx , you can use gci | Measure-Object 根据http://technet.microsoft.com/en-us/library/ff730945.aspx ,您可以使用gci | Measure-Object gci | Measure-Object or the Scripting.FileSystemObject COM object to get the folder size. gci | Measure-ObjectScripting.FileSystemObject COM对象以获取文件夹大小。

Solution 1 (Pure Powershell): 解决方案1(纯Powershell):

Function Get-ItemSize($Path) {
    (Get-ChildItem -Recurse -Path:$Path | Measure-Object -Property:Length -Sum | Select -ExpandProperty:Sum) / 1MB
}

Solution 2 (COM Object): 解决方案2(COM对象):

$objFSO = New-Object -com  Scripting.FileSystemObject
Function Get-ItemSize($Path) {
    ($objFSO.GetFolder($Path).Size) / 1MB
}

Combined with calculated properties, you can replace 结合计算的属性,您可以替换

... | Select FullName, Name, Length

With: 带有:

... | Select FullName, Name, @{Name='Length'; @Expression= {"{0:N2} MB" -f (Get-ItemSize($_) })

They provide identical output: 它们提供相同的输出:

PS> Get-item 'D:\Temp' | Select FullName, Name, @{ Name='Length'; Expression = { "{0:N2} MB" -f (Get-ItemSize($_)) } }

FullName                                 Name                                     Length                                 
--------                                 ----                                     ------                                 
D:\Temp                                  Temp                                     58.28 MB                               

I'm going to guess the COM object is technically faster, but I leave that as an exercise for the reader. 我猜想COM对象在技术上会更快,但是我将其留给读者作为练习。

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

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