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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Redefine Your Security with AI & Machine Learning

The implications of AI and machine learning in cyber security are massive and constantly growing, creating both efficiencies and new challenges across the board. Check out our on-demand webinar to learn more about how AI can help your organization!

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.