首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将批处理脚本转换为powershell

将批处理脚本转换为powershell
EN

Stack Overflow用户
提问于 2015-02-13 04:10:41
回答 2查看 2.4K关注 0票数 1

我是powershell的新手,我们正处于一个迁移应用程序的中间,需要我们将一个旧的遗留批处理脚本转换为powershell..The批处理脚本,它使用了许多老的遗留工具,比如RCMD、XCOPY,并且很快就会执行这个需求。有人能帮我把这个脚本转换成powershell吗?如果能解释这些步骤,我会非常感激的。

提前感谢

代码语言:javascript
复制
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=
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-02-13 05:39:35

这里发生了很多不同的事情。首先,您需要处理变量,因此:

代码语言:javascript
复制
$server="\\XAEO002.NET."
$importShare="\\cefl03.net.\ioponguard"

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

代码语言:javascript
复制
$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将很高兴地使用它们。

代码语言:javascript
复制
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% simple %\prod\data\in\Nordics*债务人。*可以简单到

代码语言:javascript
复制
Remove-Item "$importShare\prod\data\in\Nordics\*Debtor.*" -Force

继续我们的工作,但在不久的执行过程中,您可以作为远程命令块的一部分执行该批命令:

代码语言:javascript
复制
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.0Cmdlet替换:

您可以使用Schtasks.exe来完成它,或者在PowerShell (v3.0及更高版本)中使用以下Cmdlet进行本地操作:

新-ScheduledTaskAction,,ScheduledTaskAction和Register。

代码语言:javascript
复制
$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"

为了在远程服务器上运行上述命令,需要在调用命令期间执行上述命令.

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

代码语言:javascript
复制
SCHTASKS /S $server /Create /SC DAILY /TN PreProcessor /TR 'D:\apps\OnGuard\Preprocessor.exe' /ST 09:00

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

代码语言:javascript
复制
SCHTASKS /Create /? 

我希望这能帮上忙。

谢谢克里斯。

票数 1
EN

Stack Overflow用户

发布于 2015-02-16 15:12:31

代码语言:javascript
复制
#  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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28492353

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档