【发布时间】:2020-01-23 11:51:09
【问题描述】:
我有一个 powershell 脚本 gather_objects_from_csv.ps1 正在通过 cmd 进行调度。我正在使用这个命令:
powershell.exe -noexit "& 'e:\admin\gather_objects_from_csv.ps1'"
调用powershell脚本,但脚本中的行抛出错误
# create an empty hash which will hold a number of smaller hashes, of member details supplied in the csv, then piped to nw_sync_employees_8.ps1
$employees_list = @{}
# import the config.xml file containing the relevant user data, username/password etc...
$config = Import-CliXML nw-config.xml
# import some detail from the "normal" Newsweaver config file
$API_user = $config["API_USER"]
$API_user_password = $config["API_PASSWORD"]
$USE_PROXY = $config["USE_PROXY"]
$verboseMode = $config["VERBOSE_MODE"]
$account_code = $config["ACCOUNT_CODE"]
$source_file = $config["CSV_PATH"]
# check to see if the source file actually exists first
if (-Not (Test-Path $source_file)) {
# if the source file can't be found, there isn't much value in continuing
Write-Host -foregroundcolor red "Could not find specified source file. Check the path, permissions or location of source file specified: " $source_file
Exit
}
# import the source file with a specified delimiter (it is a comma as it is a CSV file which is being imported)
# pipe "|" the csv to a forEach - Object loop to iterate throught the file row by row
$HashTableData = Import-CSV $source_file -Delimiter ',' |`
# ForEach - Object - iterates over each row of the csv
ForEach-Object {
# Empty hash $memberDetails, this will be populated with the information from the csv
$memberDetails = @{}
$EmployeeID = "$($_.EmployeeID)".Trim()
$FirstName = "$($_.LegalFirstName)".Trim()
$LastName = "$($_.LegalLastName)".Trim()
$LegalNameinGeneralDisplayFormat = "$($_.LegalNameinGeneralDisplayFormat)".Trim()
$LegalNameinLocalScript = "$($_.LegalNameinLocalScript)".Trim()
#set the email address as the key for the hashtable $employees_list
#and the value of this "hash of hashes" is the hash table created above ie. memberDetails (csv information)
$employees_list.Set_Item("$($Email)", $memberDetails)
}
# pass/ pipe the hash called $employees_list to the nw_sync script with the relevant parameters
$employees_list | ./nw_sync_employees.ps1 -account_code $account_code -password $API_user_password -user $API_user -permission 'All'
exit
错误行:
nw_sync_employees.ps1: The term ./nw_sync_employees.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, of if a path was included, verify that the path is correct and try again. + $employee_list | ./nw_sync_employees.ps1 -account_code $account_cod... + ~~~~~~~~~~~~~~~~~~~ +CategoryInfo : ObjectNotFound: (.\nw_sync_employees.ps1:String)[], CommandNotFoundException +FullyQualifiedErrorId: CommandNotFoundException
powershell 脚本正在调用位于同一文件夹中的另一个 ps 脚本。通过 powershell 终端运行时,相同的脚本不会引发任何错误。但是当我试图通过命令行调用它时,它会抛出错误。
【问题讨论】:
-
您为什么不使用
-File选项?你不是至少在使用 PowerShell v2.0 吗? -
“通过 cmd 调度”究竟是什么意思?无需cmd,直接调用Powershell.exe即可。
-
Windows 也使用反斜杠作为目录分隔符,所以我假设
./nw_sync_employees.ps1应该是.\nw_sync_employees.ps1! -
我建议您提供生成错误消息的 PowerShell 脚本的代码/内容。运行脚本的命令不是问题的根源,(您可以清楚地看到它运行了它被要求执行的脚本),因此批处理文件标记与您的无关问题。
-
@olaf 请您帮忙处理命令如何使用 powershell,我对 powershell 命令不太熟悉。我们所有的批处理脚本都是 .bat 文件。我们第一次需要安排一个 powershell 脚本。
标签: powershell scheduled-tasks windows-scripting