powershell - 术语 '<ScriptPath>\Move' 未被识别为 cmdlet 的名称

标签 powershell powershell-4.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 : The term 'C:\PSScripts\Move' is not recognized as the name of a cmdlet, function, script file, or operable program.

我的脚本中唯一引用 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
}

最佳答案

使用-File参数:

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

关于powershell - 术语 '<ScriptPath>\Move' 未被识别为 cmdlet 的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44757654/

相关文章:

powershell - 如何在不扩展 token 的情况下获取 PATH 环境变量的值?

powershell - 如何通过powershell获取azure vm的cpu使用率

Powershell根本无法连接到互联网

powershell - 找不到 “Add”和参数计数的重载: “1”

Powershell 桌面刷新

powershell - PowerShell Where-Object-如何对条件进行分组

Powershell不显示RSS提要

powershell - 如何在Powershell中生成格式化输出?

json - 在Powershell中转换为JSON时如何更改选项卡宽度

regex - 使用 RegEx 查找字符串中特定文本后的数字