If there's a single thing I were to point out here, it's that the entire script is a single one-liner that simply creates the variable $UsersToMove. You could remove $UsersToMove = and the only change is that you wouldn't have a variable at the end. This think needs to be split out into multiple lines to be useful. Here's my breakdown, with many notes thrown in.
$UsersToMove = Import-Csv -Path
E:\AutomationScripts\O365Automation\Users_to_Move_on_Prem.csv | Where-Object
{$_.MoveInitiated -ne "Yes"}
#Don't pipe that first line on to the rest of the script. Make the variable,
then use it in futire lines.
#I moved this up here so you only run it once. It is unlikely the day will
change mid-processing.
$MoveDateValue = Get-Date -Format d
$UsersToMove | ForEach-Object { #Here we use the variable you created
#Set variable to use later.
#$UserPrincipalName = $_.UserPrincipalName #Drop this, it's
redundant. I updated the next command.
#Initiate the mailbox move.
new-moverequest -identity $_.UserPrincipalName -OutBound
-RemoteTargetDatabase 'Mailbox Database' -RemoteHostName 'exchange.domain.com'
-RemoteCredential $mycred1 -TargetDeliveryDomain 'domain.mail.onmicrosoft.com'
#Update the values in the CSV
#Drop this extra import/foreach. You are already foreach-ing through it.
#$UpdateCsv = Import-Csv -Path
E:\AutomationScripts\O365Automation\Users_to_Move_on_Prem.csv | Select
UserPrincipalName, MoveInitiated, MoveDate | Where-Object {$_.UserPrincipalName
-eq $UserPrincipalName} |
#ForEach-Object {
#Remember that you are currently running through the $UsersToMove
variable? These lines will set the properties in the variable.
$_.MoveInitiated = "Yes"
$_.MoveDate = $MoveDateValue
#Now end the foreach loop, because that's the end of the stuff that needs
to be done per-object within the array.
}
#Just for fun, verify that $UsersToMove was updated:
$UsersToMove | Format-Table
#Now that $UsersToMove has been updated, you can export it. You only do that
once.
$UsersToMove | Export-Csv -Path
E:\AutomationScripts\O365Automation\Users_to_Move_on_Prem.csv -NoTypeInformation
I got a little chatty; hopefully that will help.
Nathan Randall
Network Specialist, HIS
Allegiance Health
Tel: (517) 788-4800 / Ext. 3956
AllegianceHealth.org<http://www.allegiancehealth.org/>
[cid:[email protected]]
From: [email protected] [mailto:[email protected]] On
Behalf Of [email protected]
Sent: Tuesday, October 08, 2013 3:41 PM
To: [email protected]
Subject: [powershell] Anyone have experience manipulating a csv with
PowerShell? (replacing values)
Below is what I'm trying to do. When the script runs, I want it to parse the
csv for accounts that have not been moved, move them, then update the two
fields for that account.
I can't find a good blog article on manipulating a csv.
CSV is formatted like so:
UserPrincipalName,MoveInitiated,MoveDate
[email protected]<mailto:[email protected]>,,
#Read CSV
$UsersToMove = Import-Csv -Path
E:\AutomationScripts\O365Automation\Users_to_Move_on_Prem.csv | Select
UserPrincipalName, MoveInitiated, MoveDate | Where-Object {$_.MoveInitiated -ne
"Yes"} |
ForEach-Object {
#Set variable to use later.
$UserPrincipalName = $_.UserPrincipalName
#Initiate the mailbox move.
new-moverequest -identity $UserPrincipalName -OutBound -RemoteTargetDatabase
'Mailbox Database' -RemoteHostName 'exchange.domain.com' -RemoteCredential
$mycred1 -TargetDeliveryDomain 'domain.mail.onmicrosoft.com'
#Update the values in the CSV
$MoveDateValue = Get-Date -Format d
$UpdateCsv = Import-Csv -Path
E:\AutomationScripts\O365Automation\Users_to_Move_on_Prem.csv | Select
UserPrincipalName, MoveInitiated, MoveDate | Where-Object {$_.UserPrincipalName
-eq $UserPrincipalName} |
ForEach-Object {
$_.MoveInitiated = "Yes"; $_
$_.MoveDate = $MoveDateValue} |
Export-Csv -Path E:\AutomationScripts\O365Automation\Users_to_Move_on_Prem.csv
-NoTypeInformation
}
Christopher Catlett
Consultant | Detroit
[cid:[email protected]]
Sogeti USA
Office 248-876-9738 |Fax 877.406.9647
26957 Northwestern Highway, Suite 130, Southfield, MI 48033-8456
www.us.sogeti.com<http://www.us.sogeti.com/>
================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1
________________________________
This e-mail message and any attachment(s) is intended only for the
individual(s) to whom it is addressed and may contain information that is
privileged, confidential or proprietary in nature. Any unauthorized disclosure,
copying or distribution of this e-mail or the content of this message is
prohibited. If you have received this e-mail message in error, please
immediately notify the sender at the e-mail address above, permanently delete
this e-mail and destroy any copies of this e-mail and attachments in your
possession. This electronic message ("e-mail"), including the typed name of the
sender, does not constitute an electronic signature unless there is a specific
statement to the contrary included in this e-mail.
================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1
<<inline: image002.gif>>
<<inline: image003.jpg>>
