HashTables , Arrays, powershell

Hi All,

I have these three commands that i am trying to script:

.\emcopy64.exe \\sreadvfile03\f$ \\sreadvfile02\g$ /o /s /de /secforce /purge /cm md5 /r:1 /w:1 /c /log:sreadvfile03_fdrive_$(Get-Date -format "dd-MMM-yyyy_HH:mm").txt


.\emcopy64.exe \\sreadvfile03\e$ \\sreadvfile02\f$ /o /s /de /secforce /purge /cm md5 /r:1 /w:1 /c /log:sreadvfile03_edrive_$(Get-Date -format "dd-MMM-yyyy_HH:mm").txt


.\emcopy64.exe \\sreadvfile03\z$ \\sreadvfile02\h$ /o /s /de /secforce /purge /cm md5 /r:1 /w:1 /c /log:sreadvfile03_zdrive_$(Get-Date -format "dd-MMM-yyyy_HH:mm").txt

Open in new window


so far my solution is:

$copy=@(@{source="\sreadvfile03\f$";destination="\\sreadvfile02\g$"}, @{source="\\sreadvfile03\e$";destination="\\sreadvfile02\f$"},@{source="\sreadvfile03\f$";destination="\\sreadvfile02\g$"}) 


foreach ($m in $copy){

{
	
.\emcopy64.exe $($m.source) $($m.destination) /o /s /de /secforce /purge /cm md5 /r:1 /w:1 /c /log:$($m.source)_fdrive_$(Get-Date -format "dd-MMM-yyyy_HH:mm").txt

}

Open in new window


will the above work?

Also how do i select the correct drive in $($m.source)_fdrive_$

also any other ways of doing as this as i am trying to master powershell?
Kelly GarciaSenior Systems AdministratorAsked:
Who is Participating?
 
oBdACommented:
On a side note: when using a "ForEach ($loopVar in $whatever)" statement, the loop variable already contains the current object of the list in $Whatever, not an index.
And when accessing an object's properties inside a string, you need to use a subexpression.
$copy=@(@{source="\\sreadvfile03\f$";destination="\\sreadvfile02\g$"}, @{source="\\sreadvfile03\e$";destination="\\sreadvfile02\f$"},@{source="\sreadvfile03\f$";destination="\\sreadvfile02\g$"}) 
ForEach ($m in $copy) {
	Write-host "Copying $($m['source']) to $($m['destination'])"
}

Open in new window

0
 
Kelly GarciaSenior Systems AdministratorAuthor Commented:
I cant seem to access the $m, ive tried

$copy=@(@{source="\\sreadvfile03\f$";destination="\\sreadvfile02\g$"}, @{source="\\sreadvfile03\e$";destination="\\sreadvfile02\f$"},@{source="\sreadvfile03\f$";destination="\\sreadvfile02\g$"}) 


foreach ($m in $copy){



Write-host "Copying $copy[$m].source to $copy[$m].destination"
	



}

Open in new window

0
 
aikimarkCommented:
You have other concerns, such as the log name.  Try this
$copy=@(("f","g"),("e","f"),("z","h"))
$copy | %{".\emcopy64.exe \\sreadvfile03\$($_[0])$ \\sreadvfile02\$($_[1])$  /o /s /de /secforce /purge /cm md5 /r:1 /w:1 /c /log:sreadvfile03_$($_[0])drive_$(Get-Date -format "dd-MMM-yyyy_HH:mm").txt"}

Open in new window

0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
oBdACommented:
You can't have a ":" as part of the log file name (as it happens in your date formatting). And the date format should by y-M-d, so that the logs will be automatically sorted properly when sorted by name.
The easiest way to have data like that in a script is to put it in a here-string as csv.
$DateFormat = 'yyyy-MM-dd_HH-mm'
$Copy = @'
"Source",				"Destination"
"\\sreadvfile03\f$",	"\\sreadvfile02\g$"
"\\sreadvfile03\e$",	"\\sreadvfile02\f$"
"\\sreadvfile03\z$",	"\\sreadvfile02\h$"
'@ | ConvertFrom-Csv

$Copy | ForEach-Object {
	$Server, $Drive = $_.Source.Split('\', [StringSplitOptions]::RemoveEmptyEntries)
	$LogFile = "$($Server)_$($Drive[0])drive_$(Get-Date -Format $DateFormat).log"
	& .\emcopy64.exe $_.Source $_.Destination /o /s /de /secforce /purge /cm md5 /r:1 /w:1 /c /log:$LogFile
}

Open in new window

0
 
Kelly GarciaSenior Systems AdministratorAuthor Commented:
Fantastic Thank you
0
 
Kelly GarciaSenior Systems AdministratorAuthor Commented:
I've put the script into a workflow for parallel processing:

workflow copyfiles {

$copy=@(@{source="\\sreadvfile01\c$\testemcopy";destination="\\W10000601\C$\testemcopy_1"}, @{source="\\sreadvfile02\c$\testemcopy";destination="\\W10000601\C$\testemcopy_1"},@{source="\\sreadvfile03\c$\testemcopy";destination="\\W10000601\C$\testemcopy_1"}) 

ForEach -Parallel ($m in $copy) {

$DateFormat = 'yyyy-MM-dd_HH-mm'

write-host  "Copying $($m['source']) to $($m['destination'])"

<#.\emcopy64.exe $($m['source']) $($m['destination']) /o /s /de /secforce /purge /cm md5 /r:1 /w:1 /c /log:$($m['source'])_$($DateFormat).txt #>


}

}

Open in new window


however I get this error:

At line:5 char:1
+ write-host  "Copying $($m['source']) to $($m['destination'])"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Cannot call the 'Write-Host' command. Other commands from this module have been packaged as workflow activities, but
this command was specifically excluded. This is likely because the command requires an interactive Windows PowerShell
session, or has behavior not suited for workflows. To run this command anyway, place it within an inline-script
(InlineScript { Write-Host }) where it will be invoked in isolation.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : CommandActivityExcluded
0
 
Kelly GarciaSenior Systems AdministratorAuthor Commented:
Also I get this error:


Cannot find the '.\emcopy64.exe' command. If this command is defined as a workflow, ensure it is defined before the
workflow that calls it. If it is a command intended to run directly within Windows PowerShell (or is not available on
this system), place it in an InlineScript: 'InlineScript { .\emcopy64.exe }'
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : CommandNotFound

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.