powershell samples

1,导出至EXCEL

$arr =New-Object System.Collections.ArrayList 
$i = 1
$pstablelist = @();
$array =get-user
do
{
IF ($array[$i].RecipientType -eq "UserMailbox")
{

$mbx=Get-CASMailbox $array[$i].name
   IF($mbx.OWAEnabled)
   {
    $arr.add("name:"+$array[$i].name+"    title:"+$array[$i].title );

    $table = @{"name"=$array[$i].name;"title"=$array[$i].title;}; 

    $pstable = New-Object -TypeName PSObject -Prop $table; 

    $pstablelist += $pstable;
   }
}

$i++;

}
while ($i -lt $array.Length)
$pstablelist | Export-Csv c:\test.csv -NoTypeInformation

 

 

 

 2,

$ob1 = "foo"

Try
 {
 $ErrorActionPreference1 = "stop"
 $ErrorActionPreference11 = "stop"
 $ErrorActionPreference122 = "stop"
 $ErrorActionPreference221 = "stop"
$ob11= 11/0;
 }
Catch [System.Management.Automation.PSArgumentException]
 {
  "invalid object"
 }
Catch [System.Exception]
 {
   $_.Exception.Message;
  "caught a system exception"
 }
Finally
 {
  "end of script"
 }

 

 3,过滤

Get-MessageTrackingLog|?{$_.sourcecontext -eq '*12*'}|fl

Get-MessageTrackingLog|?{$_.sourcecontext -like '*12*'}|fl

Operator Description Example Result

-eq

equals

$a = 5 ; $a -eq 4

False

-ne

not equal

$a = 5 ; $a -ne 4

True

-gt

greater than

$a = 5 ; $a -gt 4

True

-ge

greater than or equal to

$a = 5 ; $a -ge 5

True

-lt

less than

$a = 5 ; $a -lt 5

False

-le

less than or equal to

$a = 5 ; $a -le 5

True

-like

wildcard comparison

$a = "This is Text" ; $a -like "Text"

False

-notlike

wildcard comparison

$a = "This is Text" ; $a -notlike "Text"

True

-match

regular expression comparison

$a = "Text is Text" ; $a -match "Text"

True

-notmatch

regular expression comparison

$a = "This is Text" ; $a -notmatch "Text$"

False

http://blogs.technet.com/b/heyscriptingguy/archive/2009/05/06/how-can-i-use-the-if-statement-in-windows-powershell.aspx

 5,
Search-MailboxAuditLog js1 -ShowDetails -ResultSize 111/Unlimited | Sort-Object ItemSubject|fl itemsubject,identity -First 30

 

6, get the  compressed folder\files in  the  path C:\folder by powershell

gci -r C:\folder\ | where {$_.attributes -match "compressed"} | foreach {  $_.fullname }

 gci -r C:\folder\ | where {$_.attributes -match "enr"} | foreach {  $_.fullname }

http://serverfault.com/questions/452529/view-find-all-compressed-files-on-the-server

你可能感兴趣的:(powershell)