组合powershell命令
伙计们,我有一个独特的问题,一直在绞尽脑汁寻找答案。我基本上有两个很好的运行脚本,但我需要将它们结合起来。由于某些原因,这不起作用,并且我得到了所有类型的语法powershell错误 第一个脚本工作正常。请参阅下面的第二个脚本组合powershell命令,powershell,powershell-2.0,powershell-remoting,Powershell,Powershell 2.0,Powershell Remoting,伙计们,我有一个独特的问题,一直在绞尽脑汁寻找答案。我基本上有两个很好的运行脚本,但我需要将它们结合起来。由于某些原因,这不起作用,并且我得到了所有类型的语法powershell错误 第一个脚本工作正常。请参阅下面的第二个脚本 Get-Content c:\list.txt | foreach { Get-Mailboxstatistics -id $_ | foreach{ $mbx = $_ | select DisplayName, @{Label=’Mailbox
Get-Content c:\list.txt | foreach {
Get-Mailboxstatistics -id $_ | foreach{
$mbx = $_ | select DisplayName, @{Label=’MailboxSize("MB")’;Expression={$_.TotalItemSize/1MB}}, ItemCount
$date_captured=get-date | select datetime
Get-Mailbox -id $_ | foreach{
$mbx | add-member -type noteProperty -name Alias -value $_.Alias
$mbx | add-member -type noteProperty -name ServerName -value $_.ServerName
$mbx | add-member -type noteProperty -name ProhibitSendReceiveQuota -value $.ProhibitSendReceiveQuota
$mbx | add-member -type noteProperty -name UseDatabaseQuotaDefaults -value $.UseDatabaseQuotaDefaults
$mbx | add-member -type noteProperty -name IssueWarningQuota -value $_.IssueWarningQuota
} $mbx, $date_captured
}
}
下面是运行的第二个命令。这本身非常有效,并且再次尝试
将此命令与上面的命令结合使用将失败
get-mailboxfolderstatistics -id "alias" | select name, foldersize, itemsinfolder
现在我要做的是把我的输出变成下面这样
DisplayName MailboxSize(“MB”)ItemCount别名ServerName
ProhibitSendReceiveQuota使用数据库QuotadeFaults发行WarningQuota 日期时间:2012年4月10日星期二下午4:04:28 Name Foldersize Itemsinfolder topofinfromationstore 0 3日历 1234 54收件箱1024785 241已发送邮件14745 54已删除 项目5414745 875
您可以使用
Out File
记录两个命令的输出,例如:
<first-command> | Out-File c:\log.txt
<second-command> | Out-File c:\log.txt -Append
|输出文件c:\log.txt
|输出文件c:\log.txt-追加
或者,如果您更喜欢重定向操作符:
<first-command> > c:\log.txt
<second-command> >> c:\log.txt
>c:\log.txt
>>c:\log.txt
如果我按照您的问题进行操作,我认为您要做的是合并两个命令的结果,以便最终得到一个新对象。这段代码将向管道中写入一个自定义对象,其中包含您似乎要查找的值。要保存到文件,请运行脚本并将其导出到文件或其他任何内容
Get-Content c:\list.txt | foreach {
Get-Mailboxstatistics -id $_ | foreach-object {
#define a hash table of properties
$size=($_.TotalItemSize)/1MB
$props=@{
MailboxSizeMB=$size;
Displayname=$_.DisplayName;
ItemCount=$_.ItemCount;
DateCaptured=Get-Date;
} #close hash
Get-Mailbox -id $_ | foreach-object {
$props.Add("Alias",$_.Alias)
$props.Add("ServerName",$_.ServerName)
$props.Add("ProhibitSendReceiveQuota",$_.ProhibitSendReceiveQuota)
$props.Add("UseDatabaseQuotaDefaults",$_.UseDatabaseQuotaDefaults)
$props.Add("IssueWarningQuota",$_.IssueWarningQuota)
} #foreach mailbox
#write a custom object
New-Object -TypeName PSObject -Property $props
} #foreach mailboxstatistic
}#foreach content不清楚你在问什么。另外,您的第一个“工作”脚本有语法错误。我有一种奇怪的感觉,我错过了}。当我这样做的时候,我正在工作,不幸的是,我没有在机器上运行这个。我几乎可以肯定它缺少一个“}”括号。另外,我只是尝试将顶部的脚本与下面的命令结合起来。我希望输出到一个文件。很抱歉回来这么晚。我喜欢您对命令所做的操作,并将它们变成散列。这让我思考了很多其他我可以使用它的事情。然而,从您的示例来看,我仍然无法在上面的命令中输入get-mailboxfolderstatistics命令。这实际上非常有效。我现在正试图将其输入excel。我一直在阅读我可以在哪里删除此选项卡。@user1324990您可以使用类似这样的内容作为TSV导出:
$output=@()$输出+=@()$输出|导出Csv C:\log.Csv-分隔符“`t”