如何在 Windows 上从批处理文件运行此 Powershell 函数?

问题描述 投票:0回答:1

由于某种原因,我无法从命令行/批处理文件调用中正确执行此函数。

function streamMonitor {
[cmdletbinding()]
    param(
        [string]$directoryPath,
        [string]$scriptToRestartPath
    )
... the rest of my code
}

在挖掘了大量论坛并询问 chatgpt 后,这里是尝试过的众多方法之一,我相信应该完全有效,但事实并非如此。

Powershell.exe -executionpolicy remotesigned -File  C:\pathto\streamMonitor.ps1 -directoryPath "C:\pathto\stream1" -scriptToRestartPath "C:\pathto\stream1.bat"

执行时,终端不会抛出语法错误,但 powershell 脚本也不会运行。

windows powershell batch-file cmd scripting
1个回答
0
投票

您正在创建一个函数,但没有调用它。这是输出您输入的两个参数的版本。

function streamMonitor {
    [cmdletbinding()]
        param(
            [string]$directoryPath,
            [string]$scriptToRestartPath
        )
    Write-Output $directoryPath
    Write-Output $scriptToRestartPath
    }
    
    streamMonitor -directoryPath "C:\pathto\stream1" -scriptToRestartPath "C:\pathto\stream1.bat"
© www.soinside.com 2019 - 2024. All rights reserved.