Сценарий Powershell запускается, но выводит ошибку: «Термин »$ varArgs» не распознается как имя командлета, функции, файла сценария или оперируемого объекта».

Извините, если это действительно простая вещь, но я действительно изо всех сил пытаюсь понять кодирование, поэтому я надеюсь, что кто-то может мне помочь. У меня есть относительно простой скрипт 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 ответ
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"

Он переместил первый файл .txt, который не был «Test.txt» на моем рабочем столе, в папку с именем «Новая папка», которая существовала на моем рабочем столе.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *