Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.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 我的脚本在ISE中运行,但在控制台中出错_Powershell_Windows 7_Powershell 2.0 - Fatal编程技术网

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

Powershell 我的脚本在ISE中运行,但在控制台中出错,powershell,windows-7,powershell-2.0,Powershell,Windows 7,Powershell 2.0,我的脚本在ISE中运行,但是当我想在cmd中运行它时,或者右键单击我的脚本文件并使用Powershell运行时,我会收到一条错误消息。 我使用的是Windows7,我的Powershell版本是Powershell 2.0 已尝试执行 powershell.exe -STA -FILE "C:\Users\Mgtspare\Documents\Conversion\Conversion_Script.ps1" 我的脚本: ### SET FOLDER TO WATCH + FILES TO W

我的脚本在ISE中运行,但是当我想在cmd中运行它时,或者右键单击我的脚本文件并使用
Powershell运行时,我会收到一条错误消息。
我使用的是Windows7,我的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}
错误消息:

### 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}
必须在“*”运算符的右侧提供值表达式


$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}

$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}

$watcher.Path*.xls
无效。也许您只是想通过管道将属性导入命令
$watcher.Path | rename item…..
我发现解决方案$watcher.Path是问题所在:)
$watcher.Path*.xls
无效。也许您只是想通过管道将属性导入命令
$watcher.Path | rename item…..
我发现解决方案$watcher.Path就是问题所在:)