简体   繁体   English

无法从批处理文件执行PowerShell脚本以在CMD模式下运行

[英]Unable to execute PowerShell script from batch file to run in CMD mode

I created a PowerShell script for FileWatcher. 我为FileWatcher创建了PowerShell脚本。 I need to execute it in C#. 我需要在C#中执行它。 I tried in many ways but it didn't work. 我尝试了很多方法,但是没有用。 I even created a batch file to execute my script. 我什至创建了一个批处理文件来执行我的脚本。 still it is not working, simply the console gets opened and closed. 仍然无法正常工作,只是打开和关闭了控制台。 but when i run each step manually in the command prompt I could able to execute the script. 但是当我在命令提示符下手动运行每个步骤时,我可以执行脚本。

Below is my PowerShell script: 以下是我的PowerShell脚本:

$folder = 'D:\'
$filter = '*.csv'
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{
    IncludeSubdirectories = $false;
    NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}
Register-ObjectEvent $fsw Changed -SourceIdentifier FileChanged -Action {
    $name = $Event.SourceEventArgs.Name
    $changeType = $Event.SourceEventArgs.ChangeType
    $timeStamp = $Event.TimeGenerated
    Write-Host "The file '$name' was $changeType at $timeStamp" -Fore red
    Out-File -FilePath D:\outp.txt -Append -InputObject "The file '$name' was $changeType at $timeStamp"
}

Here is the batch file 这是批处理文件

D:
powershell.exe
powershell Set-ExecutionPolicy RemoteSigned
./FileWatcherScript

To call a script from Powershell, you should use the -File argument. 要从Powershell调用脚本,应使用-File参数。 I'd change your batch file to look something like this - all you need is this one line: 我将您的批处理文件更改为如下所示-您需要的是以下一行:

powershell.exe -ExecutionPolicy RemoteSigned -File D:\FileWatcherScript.ps1

Starting powershell.exe without passing any arguments, as in the batch script in your post, will always start an interactive shell that must be exited manually. 在不传递任何参数的情况下启动powershell.exe (如您帖子中的批处理脚本中一样),将始终启动必须手动退出的交互式shell。 To run commands through Powershell programatically and exit when finished, you can pass the -File argument as I did above, or the -Command argument which takes a string or script block. 以编程方式通过运行PowerShell命令和退出完成时,你可以传递-File参数如我上面做了,或者-Command这需要一个字符串或脚本块的参数。 Here is a short example of the -Command argument taking a string: 这是-Command参数采用字符串的简短示例:

powershell.exe -Command "Invoke-RestMethod https://example.com/api/do/thing; Invoke-RestMethod https://example.com/api/stop/otherthing"

That invocation calls Invoke-RestMethod twice on two different URLs, and demonstrates that you can separate commands with a semicolon ( ; ) to run one after another. 该调用在两个不同的URL上调用了两次Invoke-RestMethod,并演示了您可以使用分号( ; )分隔命令来依次运行命令。

You can also pass a scriptblock to -Command , but note that this only works from within another Powershell session . 您也可以将-Command传递给-Command ,但是请注意,这只能在另一个Powershell会话中使用 That looks like this: 看起来像这样:

powershell.exe -Command { Invoke-RestMethod https://example.com/api/do/thing; Invoke-RestMethod https://example.com/api/stop/otherthing }

That invocation does the same thing as the previous one. 该调用与上一个调用具有相同的作用。 The difference is that it is using a scriptblock - a Powershell construct, which again only works if the parent shell is also Powershell - and it's a bit nicer since you have fewer string quoting problems. 不同之处在于,它使用的是脚本块-Powershell构造,仅当父外壳也是Powershell时,该脚本块才起作用-而且它会更好一些,因为您的字符串引用问题更少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM