How can I run this Powershell function from a batch file on windows?

69 Views Asked by At

For some reason I am unable to get this function to execute properly from a cmd line / batch file call.

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

After digging through tons of forums and asking chatgpt here is one of the many things have tried and I believe should totally work but isnt.

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

When executed, the terminal doesnt throw a syntax error but the powershell script doesn't run either.

1

There are 1 best solutions below

1
DMCochran On

You're creating a function, but you aren't calling it. Here is a version that outputs the two parameters you entered. Thanks to mklement0 for pointing out that my original script wasn't an exact fit.

Here is a version that passes the parameters in calling the script as different from the parameters used for the function.

[cmdletbinding()]
param ($directoryPath,$scriptToRestartPath)

function streamMonitor($param1, $param2) {
    Write-Output $param1
    Write-Output $param2
    }

streamMonitor ($directoryPath, $scriptToRestartPath)

And here is a version that just uses one set of parameters.

[cmdletbinding()]
param ($directoryPath,$scriptToRestartPath)

function streamMonitor {
    Write-Output $directoryPath
    Write-Output $scriptToRestartPath
    }

streamMonitor