Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么转换的批处理没有运行我的powershell脚本?_Powershell_Batch File - Fatal编程技术网

为什么转换的批处理没有运行我的powershell脚本?

为什么转换的批处理没有运行我的powershell脚本?,powershell,batch-file,Powershell,Batch File,我的文凭有一个示例脚本,它使用按键机制,如下所示: echo "A text prompting you to pres Enter" $key = [System.Windows.Input.Key]::Enter do { $isCtrl = [System.Windows.Input.Keyboard]::IsKeyDown($key) if ($isCtrl) { $query = Get-Childitem 'D:\' -Recurse | Where-Obj

我的文凭有一个示例脚本,它使用按键机制,如下所示:

echo "A text prompting you to pres Enter" 
$key = [System.Windows.Input.Key]::Enter


do
{
$isCtrl = [System.Windows.Input.Keyboard]::IsKeyDown($key)
if ($isCtrl)
{
$query = Get-Childitem 'D:\' -Recurse | Where-Object {$_.Name -match "controller.st$"}
foreach-object { $name = $query.FullName}

(Get-Content $name).Replace('A := AND55_OUT OR AND56_OUT OR AND61_OUT;', 'A := AND55_OUT') | Set-Content $name 
#this actually does the replacing in a file

echo "sample text"

Start-Sleep -Seconds 30

echo "sample text"

break
}
} while ($true)
脚本的其余部分继续

我使用转换脚本

function Convert-PowerShellToBatch
{
    param
    (
        [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
        [string]
        [Alias("FullName")]
        $Path
    )
 
    process
    {
        $encoded = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes((Get-Content -Path $Path -Raw -Encoding UTF8)))
        $newPath = [Io.Path]::ChangeExtension($Path, ".bat")
        "@echo off`npowershell.exe -NoExit -encodedCommand $encoded" | Set-Content -Path $newPath -Encoding Ascii
    }
}
 
Get-ChildItem -Path C:\path\to\powershell\scripts -Filter *.ps1 |
  Convert-PowerShellToBatch
我将其修改为我的案例,但当我运行批处理文件时,会出现以下错误:

Unable to find type [System.Windows.Input.Keyboard].
At line:7 char:11
+ $isCtrl = [System.Windows.Input.Keyboard]::IsKeyDown($key)
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Windows.Input.Keyboard:TypeName) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound
我能做些什么来克服这个问题;dr

作为注释中的注释,您需要使用以下命令将包含类型和的程序集加载到脚本中,然后才能使用它们:


转换脚本非常漂亮,但它无法检测此类问题

在转换之前,您应该始终在从
-NoProfile
开始的新会话中运行脚本,以确保脚本不会意外地依赖于当前会话的副作用,例如所需的程序集事先已由不同的代码加载:

# Run the script in a pristine session to ensure that it is self-contained.
powershell -noprofile -file somescript.ps1
特别是,在您的情况下,ISE的使用可能隐藏了问题,因为这些类型默认情况下在ISE会话中可用(因为ISE是基于WPF构建的,这些类型属于WPF)——这与常规控制台窗口不同

这只是ISE和常规控制台窗口之间的行为差异之一),这使得ISE——尽管方便——存在问题。 此外,ISE是且无法运行PowerShell(Core)v6+—有关详细信息,请参阅的底部部分


结合its,将其视为积极维护的替代方案。

我不理解您的问题与故障的关系。你能解释一下吗。生活在
PresentationCore.dll
中,因此您可能需要
添加类型-Assembly-PresentationCore
才能正常工作。“当我使用转换脚本时”-什么转换脚本?我怀疑它只是将您的powershell脚本放入批处理包装器中。它最终仍然是一个powershell脚本,特别是因为powershell中并非所有内容都具有等效的批处理。由于未包装的powershell脚本工作正常,因此问题位于转换后的脚本中的某个位置。既然你没有在你的问题中发布脚本,我就没有办法解决你的问题。
# Run the script in a pristine session to ensure that it is self-contained.
powershell -noprofile -file somescript.ps1