Powershell 脚本运行,但输出错误:“术语“$varArgs”未被识别为 cmdlet、函数、脚本文件或可操作的名称”

Powershell 脚本运行,但输出错误:“术语“$varArgs”未被识别为 cmdlet、函数、脚本文件或可操作的名称”

如果这是一件非常简单的事情,我很抱歉,但我真的很难理解编码,所以我希望有人能帮助我。我有一个相对简单的 Powershell 脚本。它似乎可以运行,但对于输出,我得到了这个 Stderr 日志消息。如果有人能告诉我如何纠正它,我将不胜感激。

'$varArgs' : The term ''$varArgs'' is not recognized as the name of a cmdlet, function, script file, or operable 
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\ProgramData\CentraStage\Packages\2bc7843b-1d71-4c43-bb6a-1ececfde5a61#\command.ps1:25 char:18
+         $varArgs=`'$varArgs`'
+                  ~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: ('$varArgs':String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

脚本如下:

# If you are reading this, you have copied the Component successfully,
# probably for the purposes of attaching your own configuration file.
# Near the bottom of this page, attach your .bgi file to the Component
# and then save the Component with a new name. The script will identify
# the presence of the file and alter the startup routines to use it.
# ----------------------------------------------------------------------
# BGInfo installer/implanter :: build 17/seagull december 2020 :: thanks to michael mccool

write-host "Install and configure BGInfo to run on Startup"
write-host "============================================="

# install the executable somewhere we can bank on its presence
new-item -Path "$env:temp\BGInfo" -ItemType Directory -Force | Out-Null
Move-Item bginfo4.exe "$env:temp\BGInfo" -Force
Move-Item bginfo8.exe "$env:temp\BGInfo" -Force

# check for BGIs
if (!(test-path *.bgi)) {
    write-host "- ERROR: There needs to be at least one .bgi file for the Component to work."
    write-host "  Execution cannot continue. Exiting."
    exit 1
} else {
    if (test-path *.bgi -exclude default.bgi) {
        $varArgs=(ls *.bgi -Exclude default.bgi | Select-Object -First 1).Name
        $varArgs=`'$varArgs`'
    } else {
        $varArgs='default.bgi'
    }
}

Move-Item $varArgs "$env:temp\BGInfo" -Force

# furnish a quick CMD script
 set-content "$env:ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\BGILaunch.cmd" -value '@echo off'
 add-content "$env:ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\BGILaunch.cmd" -value "$env:temp\BGInfo\bginfo$([intptr]::Size).exe `"$env:temp\BGInfo\$varArgs`" /silent /nolicprompt /timer:0"
# add-content "$env:ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\BGILaunch.cmd" -value 'echo Please wait -- Configuring wallpaper'
# add-content "$env:ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\BGILaunch.cmd" -value "ping -n 5 127.0.0.1 > nul"

# inform the user
# write-host "- BGInfo has been installed and configured to run on Startup."
# write-host "  Endpoints will need to be rebooted for changes to take effect."

答案1

您的脚本语法不正确。

if (!(test-path *.bgi)) {
    write-host "- ERROR: There needs to be at least one .bgi file for the Component to work."
    write-host "  Execution cannot continue. Exiting."
    exit 1
} else {
    if (test-path *.bgi -exclude default.bgi) {
        $varArgs=(ls *.bgi -Exclude default.bgi | Select-Object -First 1).Name
        $varArgs=`'$varArgs`'
    } else {
        $varArgs='default.bgi'
    }
} 

应为以下内容:

if (!(test-path *.bgi)) {
    write-host "- ERROR: There needs to be at least one .bgi file for the Component to work."
    write-host "  Execution cannot continue. Exiting."
    exit 1
} else {
    if (test-path *.bgi -exclude default.bgi) {
        $varArgs=(ls *.bgi -Exclude default.bgi | Select-Object -First 1).Name            
    } else {
        $varArgs='default.bgi'
    }
}

通过运行以下命令我能够确定此行为:

$varArgs=(ls *.txt -Exclude "Test.txt" | Select-Object -First 1).Name
Move-Item $varArgs "$env:USERPROFILE\Desktop\New folder"

它将桌面上第一个不是“Test.txt”的 .txt 文件移动到桌面上名为“新文件夹”的文件夹中。

相关内容