遇到问题if / else我最终得到两个输出文件

时间:2014-06-15 21:29:40

标签: powershell

遇到if / else问题我最终得到两个输出文件,我只需要 C:\ $ UserName- $ ComputerName-OpenPSTs- $ Date.csv如果PST存在并包含详细信息。

或C:\ $ UserName- $ ComputerName-NOPSTs- $ Date.csv如果没有PST与"没有找到PST"写在文件中。

感谢任何帮助。

#---------------------------------h 
#Getting date and system variables
#---------------------------------
$Date = Get-Date -format d-M-yyyy
$UserName = $env:USERNAME
$ComputerName = $env:COMPUTERNAME

#-------------------------------
#Launch Outlook and check stores
#-------------------------------
$Outlook = New-Object -comObject Outlook.Application
$Object = $Outlook.Session.Stores | % {    

#-------------------
#Check if PSTs exist
#-------------------
If ($_.FilePath -like "*.PST") { 

#------------------------------------------------------------
#If PST exsists then collect data and build CSV column labels
#------------------------------------------------------------ 
$_ | Select `
@{Expression={$_.DisplayName}; Label="PST Name in Outlook"},`
@{Expression={$_.FilePath}; Label="PST Location/FileName"},`
@{Expression={$_.IsOpen}; Label="PST Open in Outlook"},`
@{Expression={(Get-Item $_.FilePath).Length / 1KB}; Label="PST File Size (KB)"}
$Object | Add-Member -MemberType NoteProperty -Name 'ComputerName' -Value $ComputerName
$Object | Add-Member -MemberType NoteProperty -Name 'UserName' -Value $UserName

#--------------------------------------
#Output PST and User information to log
#--------------------------------------
$Object | Export-Csv -NoTypeInformation C:\$UserName-$ComputerName-OpenPSTs-$Date.csv

#-----------------------------------------
#If no PST exsists then create file NOPSTs
#-----------------------------------------
$Text = "No PSTs Found" 
} Else {
$_ | Select $Text | Out-File C:\$UserName-$ComputerName-NOPSTs-$Date.csv
  }
}

#---------------
#Closing Outlook
#---------------
Start-Sleep 5
Get-Process | Where {$_.Name -like "Outlook*"} | Stop-Process

1 个答案:

答案 0 :(得分:0)

替换它:

$Text = "No PSTs Found" 
} Else {
$_ | Select $Text | Out-File C:\$UserName-$ComputerName-NOPSTs-$Date.csv
  }

用这个:

} Else {
  'No PSTs Found' | Out-File C:\$UserName-$ComputerName-NOPSTs-$Date.csv
}