简体   繁体   中英

Converting a batch script to powershell

i am new to powershell and we are in the middle of a migrating application which need us to convert an old legacy batch script to powershell..The batch script is using lot old legacy tools like RCMD, XCOPY and SOON to perform the requirement. Can anybody help me on convert this script to powershell..I will be really thankful if able to explain the steps..

Thanks in advance

SET server=\\XAEO002.NET.
SET importShare=\\cefl03.net.\ioponguard

:BEGIN
IF NOT EXIST %importshare%\prod\data\in\Nordics\FI01_Debtor.csv GOTO END
IF NOT EXIST %importshare%\prod\data\in\Nordics\FI01_OpenItems.csv GOTO END
IF NOT EXIST %importshare%\prod\data\in\Nordics\SE01_Debtor.csv GOTO END
IF NOT EXIST %importshare%\prod\data\in\Nordics\SE01_OpenItems.csv GOTO END
IF NOT EXIST %importshare%\prod\data\in\Nordics\NO01_Debtor.csv GOTO END
IF NOT EXIST %importshare%\prod\data\in\Nordics\NO01_OpenItems.csv GOTO END
IF NOT EXIST %importshare%\prod\data\in\Nordics\DK01_Debtor.csv GOTO END
IF NOT EXIST %importshare%\prod\data\in\Nordics\DK01_OpenItems.csv GOTO END

RCMD %Server% XCOPY D:\Data\Onguard\Bestanden\Nordics\*.* D:\Data\Onguard\Bestanden\Nordics\History /c /h /r /y
ATTRIB -r %Server%\D$\Data\Onguard\Bestanden\Nordics\*.*
RCMD %Server% DEL /F D:\data\onguard\bestanden\Nordics\*Debtor.csv
RCMD %Server% DEL /F D:\data\onguard\bestanden\Nordics\*OpenItems.csv

::  Copy data files to target folder on OnGuard server and remove from source IOP folder

XCOPY /C /H /R /Y %importshare%\prod\data\in\Nordics\*Debtor.csv  %Server%\D$\Data\Onguard\Bestanden\Nordics\
XCOPY /C /H /R /Y %importshare%\prod\data\in\Nordics\*OpenItems.csv %Server%\D$\Data\Onguard\Bestanden\Nordics\
ECHO Y|DEL %importshare%\prod\data\in\Nordics\*Debtor.*
ECHO Y|DEL %importshare%\prod\data\in\Nordics\*OpenItems.*

::  Save previous intermediate data files and logfiles into history subfolders

RCMD %Server% XCOPY /C /H /R /Y D:\Data\Onguard\Debite~1\Nordics\*.* D:\Data\Onguard\Debite~1\Nordics\History
ECHO Y|DEL %server%\D$\data\onguard\Debite~1\Nordics\*.*
RCMD %Server% XCOPY /C /H /R /Y D:\Data\Onguard\Factuu~1\Nordics\*.* D:\Data\Onguard\Factuu~1\Nordics\History
ECHO Y|DEL %server%\D$\data\onguard\Factuu~1\Nordics\*.*
RCMD %Server% XCOPY /C /H /R /Y D:\Data\Onguard\Import\log\Nordics\*.*   D:\Data\Onguard\Import\log\Nordics\History
ECHO Y|DEL %server%\D$\data\onguard\import\log\Nordics\*.*

:: Start remotely the OnGuard PreProcessor and succesively OnGuard commandImport process

soon %server% 60 /INTERACTIVE D:\apps\OnGuard\Preprocessor.exe -a=NL  server=pdb11v.net\inst1 db=OnGuard trusted=yes

soon %server% 500 /INTERACTIVE D:\apps\OnGuard\CmdImport.exe server=pdb11v.net\inst1 db=OnGuard trusted=yes admin=6,7,8,9

:END

SET Server=
SET ImportShare=

There are quite a few different things going on here. Firstly you need to work with variables, so:

$server="\\XAEO002.NET."
$importShare="\\cefl03.net.\ioponguard"

Then cycle through the paths and "Return" if the path doesn't exist.

$path = "$importshare\prod\data\in\Nordics\"

#Creates a list of Paths that we can check, or exit out of script if they don't exist
$paths = foreach($file in @("FI01_Debtor.csv", "FI01_OpenItems.csv", "SE01_Debtor.csv", "SE01_OpenItems.csv", "NO01_Debtor.csv", "NO01_OpenItems.csv", "DK01_Debtor.csv", "DK01_OpenItems.csv"))
{
  "$path$file"
}

foreach ($fullpath in $paths)
{
  If (-not (Test-Path $fullpath -ErrorAction "SilentlyContinue") ) { return }
}

Next in order to replace your RCMD calls you can use Invoke-Command, and for XCOPY /C /H /R /Y you could use Robocopy /e:, and /A-:R to do the ATTRIB -R. Alternatively you can keep your xcopy and attrib execution, as PowerShell will happily use them.

Invoke-Command -ComputerName $server -ScriptBlock { Robocopy.exe D:\Data\Onguard\Bestanden\Nordics\ D:\Data\Onguard\Bestanden\Nordics\History /S /A-:R /R:0}

The replacement for ECHO Y|DEL %importshare%\\prod\\data\\in\\Nordics*Debtor.* can be as simple as

Remove-Item "$importShare\prod\data\in\Nordics\*Debtor.*" -Force

Continue on as we have been, but for the SOON execution at the end, you could execute the lot as part of a remote command block:

Invoke-Command -ComputerName $server -ScriptBlock {
  D:\apps\OnGuard\Preprocessor.exe -a=NL  server=pdb11v.net\inst1 db=OnGuard trusted=yes
  <#
    If you need to wait in between commands, you could throw in a:
    Sleep -Seconds 3600
  #>
  D:\apps\OnGuard\CmdImport.exe server=pdb11v.net\inst1 db=OnGuard trusted=yes admin=6,7,8,9
}

Or replace SOON using PowerShell 3.0 Cmdlets:

You can use Schtasks.exe to do it, or natively in PowerShell (v3.0 and above) using the following Cmdlets:

New-ScheduledTaskAction, New-ScheduledTaskTrigger, New-ScheduledTask, and Register-ScheduledTask.

$action = New-ScheduledTaskAction -Execute 'D:\apps\OnGuard\Preprocessor.exe' -Argument '-a=NL server=pdb11v.net\inst1 db=OnGuard trusted=yes'
$trigger =  New-ScheduledTaskTrigger -Daily -At 9am
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "Replaced SOON" -Description "Doing the Preprocessor Tasks"

The above would need to be executed during an Invoke-Command in order to run this on a remote server...

Alternatively SCHTASKS is still available, for which you can specify the server in the command:

SCHTASKS /S $server /Create /SC DAILY /TN PreProcessor /TR 'D:\apps\OnGuard\Preprocessor.exe' /ST 09:00

You can get more options via:

SCHTASKS /Create /? 

I hope that helps.

Thanks, Chris.

#  Chris made this possible...Full marks to him.. :)

$ErrorActionPreference = "Continue"
$Dte =(get-date).tostring("dd-MM-yyyy")
Start-Transcript -path "C:\temp\EYGSA-$Dte.txt" -append


Function Start-Countdown 
{   
Param(
    [Int32]$Seconds = 1500,
    [string]$Message = "Pausing Script for 1500 seconds..."
)
ForEach ($Count in (1..$Seconds))
{   Write-Progress -Id 1 -Activity $Message -Status "Script will resume    after $Seconds seconds, $($Seconds - $Count) Seconds left" -PercentComplete (($Count / $Seconds) * 100)
    Start-Sleep -Seconds 1
}
Write-Progress -Id 1 -Activity $Message -Status "Resuming" -PercentComplete 100 -Completed
}


$server="\\XAEO002.NET"
$importShare="\\cefl03.net.\ioponguard"


cls
write-host "`n"(" "*67)"`n"(" "*20)"00 / 16 TASK COMPLETED..."(" "*20)"`n"("   "*67)"`n`n" -backgroundcolor "White" -foreground "Red"
write-host "Varifying the .CSV Files are valid ...`n" -foreground "Yellow"

$path = "$importshare\prod\data\in\"

$paths = foreach($file in @("ATL01_Debitor.csv", "ATL01_OpenItems.csv",    "ATL02_Debitor.csv", "ATL02_OpenItems.csv", "ATL03_Debitor.csv", "ATL03_OpenItems.csv", "ATL04_Debitor.csv", "ATL04_OpenItems.csv", "ATL05_Debitor.csv", "ATL05_OpenItems.csv", "ATL06_Debitor.csv", "ATL06_OpenItems.csv", "CHL01_Debitor.csv", "CHL01_OpenItems.csv", "CHL02_Debitor.csv", "CHL02_OpenItems.csv", "DEL10_Debitor.csv", "DEL10_OpenItems.csv", "DEL22_Debitor.csv", "DEL22_OpenItems.csv", "DEL44_Debitor.csv", "DEL44_OpenItems.csv", "DEL48_Debitor.csv", "DEL48_OpenItems.csv"))
{
"$path$file"
}

foreach ($fullpath in $paths)
{
write-host "Varifying File : $fullpath"
If (-not (Test-Path $fullpath -ErrorAction "SilentlyContinue") ) 
{ 
write-host "`nFile varification $fullpath Failed.!! `a`n`nAbandoning the   script`n " -foreground "magenta"
Stop-Transcript
return 
}
}

cls
write-host "`n"(" "*67)"`n"(" "*20)"01 / 16 TASK COMPLETED..."(" "*20)"`n"(" "*67)"`n`n" -backgroundcolor "White" -foreground "Red"
write-host "Copying \Bestanden\GSA\ to History ...`n" -foreground "yellow"

 Invoke-Command -ComputerName $server -ScriptBlock { Robocopy.exe D:\Data\Onguard\Bestanden\GSA\ D:\Data\Onguard\Bestanden\GSA\History /A-:R /R:0}

cls
write-host "`n"(" "*67)"`n"(" "*20)"02 / 16 TASK COMPLETED..."(" "*20)"`n"(" "*67)"`n`n" -backgroundcolor "White" -foreground "Red"
write-host "Removing Bestanden\GSA ... `n" -foreground "yellow"


Remove-Item "\\$server\D$\data\onguard\bestanden\GSA\*Debitor.csv" -Force
Remove-Item "\\$server\D$\data\onguard\bestanden\GSA\*OpenItems.csv" -Force

cls
write-host "`n"(" "*67)"`n"(" "*20)"03 / 16 TASK COMPLETED..."(" "*20)"`n"(" "*67)"`n`n" -backgroundcolor "White" -foreground "Red"
write-host "Copying \data\in\ to \Bestanden\GSA\ ...`n" -foreground "yellow"

 Robocopy.exe $importshare\prod\data\in\ \\$Server\D$\Data\Onguard\Bestanden\GSA\ *Debtor.csv /R:0
 Robocopy.exe $importshare\prod\data\in\ \\$Server\D$\Data\Onguard\Bestanden\GSA\ *OpenItems.csv /R:0

 cls
write-host "`n"(" "*67)"`n"(" "*20)"04 / 16 TASK COMPLETED..."(" "*20)"`n"(" "*67)"`n`n" -backgroundcolor "White" -foreground "Red"
write-host "Removing Debtor, OpenItems ...`n" -foreground "yellow"

Remove-Item "$importShare\prod\data\in\*Debitor.*" -Force
Remove-Item "$importshare\prod\data\in\*OpenItems.*" -Force

cls
write-host "`n"(" "*67)"`n"(" "*20)"05 / 16 TASK COMPLETED..."(" "*20)"`n"(" "*67)"`n`n" -backgroundcolor "White" -foreground "Red"
write-host "Copying \Debiteur XML\GSA to History ...`n" -foreground "yellow"
Invoke-Command -ComputerName $server -ScriptBlock { Robocopy.exe "D:\Data\OnGuard\Debiteur XML\GSA\/" "D:\Data\OnGuard\Debiteur XML\GSA\History" /R:0}

cls
write-host "`n"(" "*67)"`n"(" "*20)"06 / 16 TASK COMPLETED..."(" "*20)"`n"(" "*67)"`n`n" -backgroundcolor "White" -foreground "Red"
write-host "Removing \Debiteur XML\GSA ...`n" -foreground "yellow"
Remove-Item "\\$server\D$\data\onguard\Debiteur XML\GSA\*.*" -Force

cls
write-host "`n"(" "*67)"`n"(" "*20)"07 / 16 TASK COMPLETED..."(" "*20)"`n"(" "*67)"`n`n" -backgroundcolor "White" -foreground "Red"
write-host "Copying \Factuur XML\GSA to History ...`n" -foreground "yellow"
Invoke-Command -ComputerName $server -ScriptBlock { Robocopy.exe "D:\Data\OnGuard\Factuur XML\GSA\/" "D:\Data\OnGuard\Factuur XML\GSA\History" /R:0}

cls
write-host "`n"(" "*67)"`n"(" "*20)"08 / 16 TASK COMPLETED..."(" "*20)"`n"(" "*67)"`n`n" -backgroundcolor "White" -foreground "Red"
write-host "Removing \Factuur XML\GSA ...`n" -foreground "yellow"
Remove-Item "\\$server\D$\data\onguard\Factuur XML\GSA\*.*" -Force

cls
write-host "`n"(" "*67)"`n"(" "*20)"09 / 16 TASK COMPLETED..."(" "*20)"`n"(" "*67)"`n`n" -backgroundcolor "White" -foreground "Red"
write-host "Copying \log\GSA to History ...`n" -foreground "yellow"
Invoke-Command -ComputerName $server -ScriptBlock { Robocopy.exe "D:\Data\Onguard\Import\log\GSA\/" "D:\Data\Onguard\Import\log\GSA\History" /R:0}

cls
write-host "`n"(" "*67)"`n"(" "*20)"10 / 16 TASK COMPLETED..."(" "*20)"`n"(" "*67)"`n`n" -backgroundcolor "White" -foreground "Red"
write-host "Removing \log\Nordics ...`n" -foreground "yellow"
Remove-Item "\\$server\D$\data\onguard\import\log\GSA\*.*" -Force

cls
write-host "`n"(" "*67)"`n"(" "*20)"11 / 16 TASK COMPLETED..."(" "*20)"`n"(" "*67)"`n`n" -backgroundcolor "White" -foreground "Red"
write-host "Creating Schedule Task for Preprocessor.exe ...`n" -foreground "yellow"

Invoke-Command -ComputerName $server -ScriptBlock { SCHTASKS.EXE /create /tn (get-date).tostring("P-dd-MM-yyyy,HH.mm.ss") /sc once /st (get-date).Addminutes(1).ToString("HH:mm") /tr "D:\apps\OnGuard\Preprocessor.exe -a=NL server=pdb11v.net.net\inst1 db=OnGuard trusted=no"}

cls
write-host "`n"(" "*67)"`n"(" "*20)"12 / 16 TASK COMPLETED..."(" "*20)"`n"(" "*67)"`n`n" -backgroundcolor "White" -foreground "Red"
write-host "Creating Schedule Task for CmdImport.exe ...`n" -foreground "yellow"
Invoke-Command -ComputerName $server -ScriptBlock { SCHTASKS.EXE /create /tn (get-date).tostring("C-dd-MM-yyyy,HH.mm.ss") /sc once /st (get-date).Addminutes(9).ToString("HH:mm") /tr "D:\apps\OnGuard\CmdImport.exe server=pdb11v.net\inst1 db=OnGuard trusted=no admin=5"}

cls
write-host "`n`n`n`n"(" "*67)"`n"(" "*20)"13 / 16 TASK COMPLETED..."(" "*20)"`n"(" "*67)"`n`n" -backgroundcolor "White" -foreground "Red"
#write-host "Please wait the script will resume after 25 minuts .. `n" -   foreground "yellow" -backgroundcolor "Red"
#Sleep -Seconds 1500


Start-Countdown -Seconds 1500 -Message "Do not close or cancel this window.."

cls
write-host "`n"(" "*67)"`n"(" "*20)"14 / 16 TASK COMPLETED..."(" "*20)"`n"(" "*67)"`n`n" -backgroundcolor "White" -foreground "Red"
write-host "Copying File Impaddressen.log ...`n" -foreground "yellow"
Invoke-Command -ComputerName $server -ScriptBlock { Robocopy.exe "C:\windows\system32\/" "D:\Data\Onguard\Import\log\" Impaddressen.log /R:0}

cls
write-host "`n"(" "*67)"`n"(" "*20)"15 / 16 TASK COMPLETED..."(" "*20)"`n"(" "*67)"`n`n" -backgroundcolor "White" -foreground "Red"
write-host "RemovingFile Impaddressen.log from System32 ...`n" -foreground "yellow"
Remove-Item "\\$server\C$\windows\system32\ImpAddressen.log" -Force -ErrorAction SilentlyContinue

cls
write-host "`n"(" "*67)"`n"(" "*20)"16 / 16 TASK COMPLETED..."(" "*20)"`n"(" "*67)"`n`n" -backgroundcolor "White" -foreground "Red"
write-host "Script Completed.."




Stop-Transcript

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM