我的脚本在ISE中运行,但在控制台中出现错误

时间:2019-08-15 19:09:44

标签: powershell windows-7 powershell-2.0

我的脚本在ISE中运行,但是当我想在cmd中运行它或右键单击脚本文件并使用Powershell运行时,我收到一条错误消息。
我使用的是 Windows 7 ,而我的Powershell版本是 Powershell 2.0

已经尝试执行

powershell.exe -STA -FILE "C:\Users\Mgtspare\Documents\Conversion\Conversion_Script.ps1"

我的脚本:

### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\Users\Mgtspare\Downloads"
$watcher.Filter = "*.xls"
$watcher.IncludeSubdirectories = $false
$watcher.EnableRaisingEvents = $true  

### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
$action = { 
            $watcher.Path *.xls | rename-item -newname { [io.path]::ChangeExtension($_.name, "xlsx") }
          }    
### DECIDE WHICH EVENTS SHOULD BE WATCHED 
Register-ObjectEvent $watcher "Created" -Action $action
while ($true) {sleep 5}

错误消息:

  

您必须在'*'运算符的右侧提供一个值表达式。

enter image description here

1 个答案:

答案 0 :(得分:0)

$ watcher.Path是我的脚本的问题,我更改了我的脚本,如果有人需要,可以在解决方案下方查看:

### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = "C:\Users\Mgtspare\Downloads"
    #$watcher.Filter = "*.*"
    $watcher.IncludeSubdirectories = $true
    $watcher.EnableRaisingEvents = $true

### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
    $action = { 
                Get-ChildItem -Path C:\Users\Mgtspare\Downloads *.xls | rename-item -newname { [io.path]::ChangeExtension($_.name, "xlsx") }
              }

### DECIDE WHICH EVENTS SHOULD BE WATCHED 
    Register-ObjectEvent $watcher 'Created' -SourceIdentifier 'FileCreated' -Action $action
    while ($true) {sleep 5}