使用以下脚本,我打算实现以下目标:
我的脚本的当前迭代如下:
$ems = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri `
http://exchangehubcas.mydomain.com/powershell
Import-PSSession $ems -DisableNameChecking
function getRepQ {
Get-MailboxDatabase | Get-MailboxDatabaseCopyStatus |
Sort-Object mailboxserver |
Select-Object -Property mailboxserver, name, status, copyqueuelength,
replayqueuelength, contentindexstate |
Export-Csv -Path "c:\admin\scripts\exchange\ExchangeReplication_$((Get-Date).ToString('yyyy-mm-dd_hh-mm-ss')).csv"
}
function qchk {
$nowfile = getRepQ
$data = Get-Content -Path $nowfile | Select-Object -Skip 1 |
Out-String | ConvertFrom-Csv |
Select-Object -Property status -Unique
$repStat = $data.Status
foreach ($_ in $repStat) {
if ($_ -eq "Healthy") {
Send-MailMessage -Attachments $nowfile -From admin@mydomain.com `
-To jsmith@mydomain.com -Subject "Database replication failed" `
-Body "Review attached CSV file for more details." `
-SmtpServer exchangehubcas.mydomain.com
}
}
}
qchk
最终“健康”将更改为“失败”,但正确生成.CSV文件时,qchk
函数失败,表示Get-Content
的路径为空。
我做错了什么以及达到预期目标的正确方法是什么?
答案 0 :(得分:0)
您的函数getRepQ
将数据从Get-MailboxDatabaseCopyStatus
导出为CSV,但不会返回任何内容。因此,当您在$nowfile
中调用函数时,没有为变量qchk
分配任何内容。
改变这个:
function getRepQ {
Get-MailboxDatabase | Get-MailboxDatabaseCopyStatus |
Sort-Object mailboxserver |
Select-Object -Property mailboxserver, name, status, copyqueuelength,
replayqueuelength, contentindexstate |
Export-Csv -Path "c:\admin\scripts\exchange\ExchangeReplication_$((Get-Date).ToString('yyyy-mm-dd_hh-mm-ss')).csv"
}
到此:
function getRepQ {
$date = (Get-Date).ToString('yyyy-mm-dd_hh-mm-ss')
$csv = "C:\admin\scripts\exchange\ExchangeReplication_$date.csv"
Get-MailboxDatabase | Get-MailboxDatabaseCopyStatus |
Sort-Object mailboxserver |
Select-Object -Property mailboxserver, name, status, copyqueuelength,
replayqueuelength, contentindexstate |
Export-Csv -Path $csv
$csv
}
该功能将数据导出到CSV 和,并将该CSV的路径返回给调用者。