将批处理脚本转换为PowerShell

时间:2015-02-13 04:10:42

标签: powershell batch-file

我是PowerShell的新手,我们正处于迁移应用程序的中间,需要我们将旧的旧批处理脚本转换为powershell ..批处理脚本使用许多旧的遗留工具,如RCMD,XCOPY和SOON来执行需求。任何人都可以帮助我将此脚本转换为powershell ..如果能够解释这些步骤,我将非常感激..

提前致谢

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=

2 个答案:

答案 0 :(得分:1)

这里有很多不同的事情。首先,您需要使用变量,所以:

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

然后循环通过路径,如果路径不存在则返回“返回”。

$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 }
}

接下来为了替换您的RCMD调用,您可以使用Invoke-Command,对于XCOPY / C / H / R / Y,您可以使用Robocopy / e:和/ A-:R来执行ATTRIB -R。或者,您可以保留xcopy和attrib执行,因为PowerShell会很乐意使用它们。

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

替换ECHO Y | DEL%importshare%\ prod \ data \ in \ Nordics * Debtor。*可以像

一样简单
Remove-Item "$importShare\prod\data\in\Nordics\*Debtor.*" -Force

继续像往常一样,但是对于最后的SOON执行,您可以将该批次作为远程命令块的一部分执行:

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
}

或使用PowerShell 3.0 Cmdlet替换SOON:

您可以使用Schtasks.exe执行此操作,或使用以下Cmdlet在PowerShell(v3.0及更高版本)中本机执行此操作:

New-ScheduledTaskAction,New-ScheduledTaskTrigger,New-ScheduledTask和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"

上面需要在Invoke-Command期间执行,以便在远程服务器上运行...

或者,SCHTASKS仍然可用,您可以在命令中指定服务器:

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

您可以通过以下方式获得更多选择:

SCHTASKS /Create /? 

我希望有所帮助。

谢谢,克里斯。

答案 1 :(得分:0)

#  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