Kelly Garcia
asked on
HashTables , Arrays, powershell
Hi All,
I have these three commands that i am trying to script:
so far my solution is:
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?
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
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
}
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?
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"}
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.
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
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Fantastic Thank you
ASKER
I've put the script into a workflow for parallel processing:
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
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 #>
}
}
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
ASKER
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
ASKER
Open in new window