Powershell 到可执行文件 .bat
Powershell to executable .bat
我正在尝试将 Powershell 中的以下脚本转换为可执行的 .bat 文件。基本思想是我有一堆文件,文件名分为 3 个部分:
第 1 部分:TKSYSTEMID
第 2 部分:变量名称
第 3 部分:YYYY MM DD HHMM
所以一个简单的例子如下:TKSYSTEMID NOTES YYYY MM DD HHMM
我正在寻找一个 .bat 文件,它可以用提示用户输入的内容替换 TKSYSTEM ID 和 YYYY MM DD HHMM。
我目前拥有的 powershell 脚本是:
$NEWSYSTEMID = Read-Host -Prompt 'Please input SEJ SYSTEM ID'
Write-Host "Changing TKSYSTEMID to '$NEWSYSTEMID'"
dir | Rename-Item -NewName {$_.name -replace 'TKSYSTEMID',$NEWSYSTEMID}
$NEWDATETIME = Read-Host -Prompt 'Please input the upload date and time'
Write-Host "Changing YYYY MM DD HHMM to '$NEWDATETIME'"
dir | Rename-Item -NewName {$_.name -replace 'YYYY MM DD HHMM',$NEWDATETIME}
有人可以告诉我如何转换它或为此提供一个 .bat 脚本吗?这将意味着世界。
默认情况下,Windows 不允许通过双击 .PS1 文件来执行 运行 Powershell 脚本。这实际上是一个 security feature aimed at protecting the less-computer savvy users. Back in the days, VBScript files were a popular attack vector for all kind of malware. This was as VBScript files were runnable by default wit a double-click. It was all too easy to fool inexperienced users to get infected with, say, Love Letter 蠕虫。
作为 work-around,创建启动 Powershell 的 shell 并将参数传递给脚本的快捷方式。如果 link rot, :
powershell.exe -command "& 'C:\A path with spaces\MyScript.ps1' -MyArguments blah"
您可以将 powershell 代码包装到批处理文件中,例如:
<# : batch portion
@echo off & setlocal
(for %%I in ("%~f0";%*) do @echo(%%~I) | ^
powershell -noprofile "$argv = $input | ?{$_}; iex (${%~f0} | out-string)"
goto :EOF
: end batch / begin powershell #>
$NEWSYSTEMID = Read-Host -Prompt 'Please input SEJ SYSTEM ID'
Write-Host "Changing TKSYSTEMID to '$NEWSYSTEMID'"
dir | Rename-Item -NewName {$_.name -replace 'TKSYSTEMID',$NEWSYSTEMID}
$NEWDATETIME = Read-Host -Prompt 'Please input the upload date and time'
Write-Host "Changing YYYY MM DD HHMM to '$NEWDATETIME'"
dir | Rename-Item -NewName {$_.name -replace 'YYYY MM DD HHMM',$NEWDATETIME}
下面的批处理文件实际上是您原始 PowerShell 代码的直接翻译,不同之处在于每个文件只重命名一次。
@echo off
setlocal EnableDelayedExpansion
set /P "NEWSYSTEMID=Please input SEJ SYSTEM ID: "
set /P "NEWDATETIME=Please input the upload date and time: "
echo Renaming files
for /F "tokens=*" %%a in ('dir /B') do (
set "name=%%a"
set "name=!name:TKSYSTEMID=%NEWSYSTEMID%!"
set "name=!name:YYYY MM DD HHMM=%NEWDATETIME%!"
ren "%%a" "!name!"
)
但是,如果文件名中的"Part 2"由单个单词组成,例如NOTES
,那么代码可能会更简单,因为"Parts 1, 2 and rest"(标记1, 2*) 直接在重命名过程中使用:
@echo off
setlocal
set /P "NEWSYSTEMID=Please input SEJ SYSTEM ID: "
set /P "NEWDATETIME=Please input the upload date and time: "
echo Renaming files
for /F "tokens=1,2*" %%a in ('dir /B') do (
ren "%%a %%b %%c" "%NEWSYSTEMID% %%b %NEWDATETIME%"
)
我开始使用@npocmaka 的解决方案,但在编码非美国字符时遇到了问题。以下解决方案解决了这个问题。
<# : Save as UTF8 without BOM
@ECHO OFF
powershell.exe -NoProfile -Command "Get-Content -Path \"%~f0\" -Encoding UTF8 -Raw | Invoke-Expression"
PAUSE
EXIT /B %errorlevel%
:Script part below #>
Set-StrictMode -Version Latest
Write-Host("Hello Germans: ÄÖÜäöü")
我正在尝试将 Powershell 中的以下脚本转换为可执行的 .bat 文件。基本思想是我有一堆文件,文件名分为 3 个部分:
第 1 部分:TKSYSTEMID
第 2 部分:变量名称
第 3 部分:YYYY MM DD HHMM
所以一个简单的例子如下:TKSYSTEMID NOTES YYYY MM DD HHMM
我正在寻找一个 .bat 文件,它可以用提示用户输入的内容替换 TKSYSTEM ID 和 YYYY MM DD HHMM。
我目前拥有的 powershell 脚本是:
$NEWSYSTEMID = Read-Host -Prompt 'Please input SEJ SYSTEM ID'
Write-Host "Changing TKSYSTEMID to '$NEWSYSTEMID'"
dir | Rename-Item -NewName {$_.name -replace 'TKSYSTEMID',$NEWSYSTEMID}
$NEWDATETIME = Read-Host -Prompt 'Please input the upload date and time'
Write-Host "Changing YYYY MM DD HHMM to '$NEWDATETIME'"
dir | Rename-Item -NewName {$_.name -replace 'YYYY MM DD HHMM',$NEWDATETIME}
有人可以告诉我如何转换它或为此提供一个 .bat 脚本吗?这将意味着世界。
Windows 不允许通过双击 .PS1 文件来执行 运行 Powershell 脚本。这实际上是一个 security feature aimed at protecting the less-computer savvy users. Back in the days, VBScript files were a popular attack vector for all kind of malware. This was as VBScript files were runnable by default wit a double-click. It was all too easy to fool inexperienced users to get infected with, say, Love Letter 蠕虫。
作为 work-around,创建启动 Powershell 的 shell 并将参数传递给脚本的快捷方式。如果 link rot, :
powershell.exe -command "& 'C:\A path with spaces\MyScript.ps1' -MyArguments blah"
您可以将 powershell 代码包装到批处理文件中,例如:
<# : batch portion
@echo off & setlocal
(for %%I in ("%~f0";%*) do @echo(%%~I) | ^
powershell -noprofile "$argv = $input | ?{$_}; iex (${%~f0} | out-string)"
goto :EOF
: end batch / begin powershell #>
$NEWSYSTEMID = Read-Host -Prompt 'Please input SEJ SYSTEM ID'
Write-Host "Changing TKSYSTEMID to '$NEWSYSTEMID'"
dir | Rename-Item -NewName {$_.name -replace 'TKSYSTEMID',$NEWSYSTEMID}
$NEWDATETIME = Read-Host -Prompt 'Please input the upload date and time'
Write-Host "Changing YYYY MM DD HHMM to '$NEWDATETIME'"
dir | Rename-Item -NewName {$_.name -replace 'YYYY MM DD HHMM',$NEWDATETIME}
下面的批处理文件实际上是您原始 PowerShell 代码的直接翻译,不同之处在于每个文件只重命名一次。
@echo off
setlocal EnableDelayedExpansion
set /P "NEWSYSTEMID=Please input SEJ SYSTEM ID: "
set /P "NEWDATETIME=Please input the upload date and time: "
echo Renaming files
for /F "tokens=*" %%a in ('dir /B') do (
set "name=%%a"
set "name=!name:TKSYSTEMID=%NEWSYSTEMID%!"
set "name=!name:YYYY MM DD HHMM=%NEWDATETIME%!"
ren "%%a" "!name!"
)
但是,如果文件名中的"Part 2"由单个单词组成,例如NOTES
,那么代码可能会更简单,因为"Parts 1, 2 and rest"(标记1, 2*) 直接在重命名过程中使用:
@echo off
setlocal
set /P "NEWSYSTEMID=Please input SEJ SYSTEM ID: "
set /P "NEWDATETIME=Please input the upload date and time: "
echo Renaming files
for /F "tokens=1,2*" %%a in ('dir /B') do (
ren "%%a %%b %%c" "%NEWSYSTEMID% %%b %NEWDATETIME%"
)
我开始使用@npocmaka 的解决方案,但在编码非美国字符时遇到了问题。以下解决方案解决了这个问题。
<# : Save as UTF8 without BOM
@ECHO OFF
powershell.exe -NoProfile -Command "Get-Content -Path \"%~f0\" -Encoding UTF8 -Raw | Invoke-Expression"
PAUSE
EXIT /B %errorlevel%
:Script part below #>
Set-StrictMode -Version Latest
Write-Host("Hello Germans: ÄÖÜäöü")