out of office powershell
Hi guys,
Can you please help with the following command:
Set-Mailbox <<var>alias</var>> -ForwardingAddress <<var>alias</var>> -DeliverToMailboxAndForward $true
Set-MailboxAutoReplyConfiguration <<var>alias</var>> -AutoReplyState enabled -ExternalAudience <none all="" known="">-InternalMessage
$internalmessage -ExternalMessage $externalmessage</none>
I would like to use the above commands for multiple users. How can I use it with an excel file?
Regards
October 24th, 2012 2:29am
If you can use just a .csv file (I can't think of a way to use an actual .xls file), you can probably do it with something like this
import-csv "c:\whatever.csv" | foreach{
}
but what goes between the braces depends very much on what's in the file, and what you use for column headings.Mobile OWA For Smartphone
www.leederbyshire.com
email a@t leederbyshire d.0.t c.0.m
Free Windows Admin Tool Kit Click here and download it now
October 24th, 2012 11:43am
Your excel file should be as below
Alias | Email
user1 | user1@email.com
$pass = "password_account" #to be modified
$secPass = ConvertTo-SecureString $pass -AsPlainText -Force
$a = New-Object System.Management.Automation.PsCredential "domain\account",$secPass
$session = New-PSSession -ConfigurationName Microsoft.Exchange -Credential $a -ConnectionUri http://cas_server/PowerShell/ -Authentication Kerberos
Import-PSSession $session -AllowClobber
Function Import-Excel
{
Param (
[string]$FileName,
[string]$WorkSheetName,
[bool]$DisplayProgress = $true
)
IF ($FileName -eq "") {
thRow "Please provide path to the Excel file"
Exit
}
IF (-not (Test-Path $FileName)) {
thRow "Path '$FileName' does not exist."
exit
}
#US culture info
$CultureUS = [System.Globalization.CultureInfo]'en-US'
#Changing the Culture (Regional settings) For this script so we can open the Excel file
[System.Threading.Thread]::CurrentThread.CurrentCulture = $CultureUS
$FileName = Resolve-Path $FileName
$ExcellObj = New-Object -comobject Excel.Application
$ExcellObj = New-Object -comobject Excel.Application
$ExcellObj.Visible = $False #you can see the ps1 create the result is set to True, but this takes longer
$Workbook = $ExcellObj.Workbooks.Open($FileName)
IF (-not $WorkSheetName) {
Write-Warning "Defaulting to the first workSheet in workbook."
$Sheet = $workbook.ActiveSheet
} ELSE {
$Sheet = $workbook.Sheets.Item($WorkSheetName)
}
IF (-not $Sheet)
{
thRow "Unable to open workSheet $WorkSheetName"
exit
}
$SheetName = $Sheet.Name
$Columns = $Sheet.UsedRange.Columns.Count
$Lines = $Sheet.UsedRange.Rows.Count
Write-Warning "WorkSheet $SheetName contains $Columns Columns and $Lines Lines of data"
$Fields = @()
For ($Column = 1; $Column -le $Columns; $Column ++) {
$FieldName = $Sheet.Cells.Item.Invoke(1, $Column).Value2
IF ($FieldName -eq $NULL) {
$FieldName = "Column" + $Column.ToString()
}
$Fields += $FieldName
}
$Line = 2
For ($Line = 2; $Line -le $Lines; $Line ++) {
$Values = New-Object object[] $Columns
For ($Column = 1; $Column -le $Columns; $Column++) {
$Values[$Column - 1] = $Sheet.Cells.Item.Invoke($Line, $Column).Value2
}
$Row = New-Object psobject
$Fields | Foreach-object -begin {$i = 0} -process {
$Row | Add-Member -MemberType noteproperty -Name $Fields[$i] -Value $Values[$i]; $i++
Write-Host "$Row.'Alias';$Row.'Email'"
Set-Mailbox $Row.'Alias' -ForwardingAddress $Row.'Email' -DeliverToMailboxAndForward:$true
}
}
$Workbook.Close()
$ExcellObj.Quit()
}
# Run Function
$reportLines = Import-Excel "C:\Temp\Import.xls" # to be modified
Don't forgot to modify the account, password and server and source path of file.
October 26th, 2012 12:21pm