Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 包括if/else逻辑_Powershell_Outlook - Fatal编程技术网

Powershell 包括if/else逻辑

Powershell 包括if/else逻辑,powershell,outlook,Powershell,Outlook,您好,我正在尝试if/else并编写两个单独的文件,如果存在PST,请执行以下操作导出Csv-非类型信息C:\$UserName-$ComputerName OpenPSTs-$Date.Csv Else导出Csv-notype信息C:\$UserName-$ComputerName NOPSTs-$Date.Csv 谁能推荐一下吗 $Date = Get-Date -format d-M-yyyy $UserName = $env:USERNAME $ComputerName = $env:C

您好,我正在尝试
if/else
并编写两个单独的文件,
如果存在
PST
,请执行以下操作<代码>导出Csv-非类型信息C:\$UserName-$ComputerName OpenPSTs-$Date.Csv

Else
导出Csv-notype信息C:\$UserName-$ComputerName NOPSTs-$Date.Csv

谁能推荐一下吗

$Date = Get-Date -format d-M-yyyy
$UserName = $env:USERNAME
$ComputerName = $env:COMPUTERNAME
$Outlook = New-Object -comObject Outlook.Application

$object = $Outlook.Session.Stores | Where {$_.FilePath -like "*.PST"} | 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
$object | Export-Csv -NoTypeInformation C:\$UserName-$ComputerName-OpenPSTs-$Date.csv

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

您可以将
Where Object
过滤器替换为
ForEach Object
循环和嵌套条件:

$Outlook.Session.Stores | % {
  if ($_.FilePath -like '*.pst') {
    $_ | select ... | Export-Csv 'OpenPST.csv' -NoType -Append
  } else {
    $_ | select ... | Export-Csv 'NoPST.csv' -NoType -Append
  }
}
但是,这可能执行得不太好,因为它会重复地附加到输出文件中。最好只运行两条带有互补过滤器的管道:

$stores = $Outlook.Session.Stores

$stores | ? { $_.FilePath -like '*.pst' } | select ... |
    Export-Csv 'OpenPST.csv' -NoType
$stores | ? { $_.FilePath -notlike '*.pst' } | select ... |
    Export-Csv 'NoPST.csv' -NoType