批处理脚本调用Powershell脚本

时间:2020-01-23 11:51:09

标签: powershell scheduled-tasks windows-scripting

我有一个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终端运行时,同一脚本不会引发任何错误。但是,当我尝试通过命令行调用它时,会抛出错误。

1 个答案:

答案 0 :(得分:0)

.bat中的这一行对我有用:

Powershell.exe“ D:\ myfolder \ myfile.ps1”

具有管理员权限。