术语“<scriptpath> \ Move”未被识别为cmdlet的名称

时间:2017-06-26 10:26:39

标签: powershell powershell-v4.0

我创建了一个脚本,可以将文件和文件夹从一个文件夹移动到另一个文件夹。我在PowerShell ISE中设计了它,如果我从那里运行它,它可以很好地工作。

但是当我从任务计划程序或使用

的命令提示符运行它时
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass "C:\MilkImports\FileScripts\Move No Holdings Milk Files.ps1"

我收到以下错误

  

C:\ PSScripts \ Move:术语“C:\ PSScripts \ Move”无法识别为cmdlet,函数,脚本文件或可运行程序的名称。

我在脚本中对Move的唯一引用是当我使用Move-Item时。

我的脚本如下

$SQLServer = "xxx"
$SQLDatabase = "xxx"
$SQLUser = "xxx"
$SQLPassword = "xxx"
$SQLConnection = New-Object System.Data.SqlClient.SqlConnection
$SQLCommand = New-Object System.Data.SqlClient.SqlCommand
$SQLDataAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$dtPIDs = New-Object System.Data.DataTable

$SQLQuery = "SELECT DISTINCT(PID) FROM table1 t1
             INNER JOIN table2 t2 ON t1.CID = t2.CID
             WHERE CHARINDEX('UK', areaCode) > 0"

$SQLConnection.ConnectionString = "Server=$SQLServer;Database=$SQLDatabase;Integrated Security=false;User ID=$SQLUser;Password=$SQLPassword;"
$SQLCommand.Connection = $SQLConnection
$SQLCommand.CommandText = $SQLQuery
$SQLDataAdapter.SelectCommand = $SQLCommand
$SQLDataAdapter.Fill($dtPIDs)

foreach($Row in $dtPIDs)
{
    $PID = $Row["PID"]

    $SourceFolder = Join-Path -Path "C:\Imports\NoHolding" -ChildPath $PID
    $DestinationFolder = Join-Path -Path "C:\Imports\Input" -ChildPath $PID

    if ((Test-Path $SourceFolder) -eq 1)
    {
        if ((Test-Path $DestinationFolder) -eq 0)
        {
            mkdir $DestinationFolder
        }

        $SourceFiles = Get-ChildItem -Path $SourceFolder -Filter "*.dat"

        foreach ($SourceFile in $SourceFiles)
        {
            $DestinationFile = Join-Path -Path $DestinationFolder -ChildPath $SourceFile.Name

            Move-Item -Path $SourceFile.FullName -Destination $DestinationFile
        }
    }
}

$SourceFiles = Get-ChildItem -Path "C:\Imports\NoHolding" -Filter "*.dat"

foreach ($SourceFile in $SourceFiles)
{
    $DestinationFile = Join-Path -Path "C:\Imports\Input" -ChildPath $SourceFile.Name

    Move-Item -Path $SourceFile.FullName -Destination $DestinationFile
}

1 个答案:

答案 0 :(得分:2)

使用-File参数:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File "C:\PSScripts\Move No Holdings Milk Files.ps1"