我遇到了一个非常奇怪的情况,我不清楚为什么会发生这个错误。
简而言之,我有一个powershell脚本,如果我手动运行100% 我已将此设置为Windows任务管理器中的计划任务,并且事件启动,运行powershell脚本的SQL元素,因此我知道该问题并不存在。但是,在powershell代码中我打开了一个特定的MS-Access数据库,这是行不通的。
更短的版本, - 在非自动化时工作,自动化工作但不是MS-Access
我很困惑,我已经包含了代码可以让任何人看到任何不妥之处吗?
$NewFile = '\\Operations Database\Compliance\CancelledLimitOrders\LIOAnalysis.csv'
$ComplianceSQL = @"
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
DECLARE @startdate CHAR(8)
DECLARE @finaldate CHAR(8)
SELECT @startdate=CONVERT(CHAR(8),GETDATE(),112), -- get date is editable in the negative figure to provide date range for SQL
@finaldate=CONVERT(CHAR(8),GETDATE()+7,112)
select LIOORDERREF,
lioclientcode,
liodatetimeinput,
liosecurityid,
CAST(LIOPRICE1 AS MONEY) AS PRICE1,
CAST(LIOPRICE2 AS MONEY) AS PRICE2,
CAST(lioexpiryperiod AS FLOAT) AS EXPIRYDAYS,
lioexpirydatetime,
liolastupdated,
liodatetimeoflaststatus,
liofailurecount,
liotransactiontype,
liobidprice1,
liobidprice2,
liobidprice3,
lioaskprice1,
lioaskprice2,
lioaskprice3,
CAST(lioordercashamount AS FLOAT) AS LIOCASHAMOUNT,
LIOSTOCKSYMBOL,
CASE WHEN liosecurityexchange = 'XLON' THEN LTRIM(RTRIM(liostocksymbol)) + '.L'
ELSE liostocksymbol
END AS STOCKAPI,
liocurrency,
lioquoteprovider,
liosecurityexchange,
CASE WHEN lioparenttype = 'NULL' THEN 'UNKNOWN' + '-' + LIOPARENTTYPE
WHEN lioparenttype = '' THEN 'UNKNOWN' + '-' + LIOPARENTTYPE
WHEN lioparenttype = 'BO' THEN 'BULK ORDER'
WHEN lioparenttype = 'DB' THEN 'BUY DEAL AT BEST'
WHEN lioparenttype = 'DS' THEN 'SELL DEAL AT BEST'
WHEN lioparenttype = 'LB' THEN 'LIMIT BUY'
WHEN lioparenttype = 'LS' THEN 'LIMIT SELL'
WHEN lioparenttype = 'PL' THEN 'UNKNOWN' + '-' + LIOPARENTTYPE
WHEN lioparenttype = 'RB' THEN 'RISING BUY'
WHEN lioparenttype = 'RT' THEN 'UNKNOWN' + '-' + LIOPARENTTYPE
WHEN lioparenttype = 'SL' THEN 'STOP LOSS'
WHEN LIOPARENTTYPE = 'SO' THEN 'SELL ORDER'
WHEN lioparenttype = 'TS' THEN 'UNKNOWN' + '-' + LIOPARENTTYPE
ELSE 'UNKNOWN' + '-' + LIOPARENTTYPE
END AS PARENTTYPE
from xtlimitorder
where left(liolastupdated,8) = @StartDate
and left(lioorderref,2) not like 'PP'
and liostatus = 'c'--
"@
##Development Notes##
## PS Script to run once every 5 minutes
## PS will download CSV of newly cancelled trades
## On download MS-Access Utilised to 'append data' to daily trades table
#####################
## - CREDENTIALS - ##
#####################
$MISA = 'xxxxxxxx'
$MISB = 'xxxxxxxx'
$userName = 'xxxxxxxxx'
$PassWord='xxxxxxxxxxx'
$DB = 'xxxxxxxxx'
$timeout=0
###### - StopWatch - ######
$timeout2 = new-timespan -Minutes 5
$sw = [diagnostics.stopwatch]::StartNew()
## CREATE MIS CREDENTIALS ##
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection("Connection Timeout=0")
$SqlConnection.ConnectionString = "Data Source=$MISA;Initial Catalog=$DB;
Initial Catalog=$DB;User ID=$userName;Password=$PassWord;"
## - Runs Script from Set Location
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand;
$SqlCmd.CommandTimeout=$timeout;
$SqlCMD.CommandText = $ComplianceSQL;
$SqlCmd.Connection = $SqlConnection;
## - Extract Data and build sql data object
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter;
$SqlAdapter.SelectCommand = $SqlCmd;
$DataSet = New-Object System.Data.DataSet;
$SqlAdapter.Fill($DataSet);
$DataSetTable = $DataSet.Tables["Table"];
$DSETCOUNT = $DataSetTable.Rows.Count
IF($DSETCOUNT -GT 0){
ECHO "AA"
}
else {
echo "NULL NULL NULL"
$SqlConnection.Close()
## CREATE MIS CREDENTIALS ##
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection("Connection Timeout=0")
$SqlConnection.ConnectionString = "Data Source=$MISb;Initial Catalog=$DB;
Initial Catalog=$DB;User ID=$userName;Password=$PassWord;"
## - Runs Script from Set Location
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand;
$SqlCmd.CommandTimeout=$timeout;
$SqlCMD.CommandText = $SECTABLEQUERY;
$SqlCmd.Connection = $SqlConnection;
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter;
$SqlAdapter.SelectCommand = $SqlCmd;
$DataSet = New-Object System.Data.DataSet;
$SqlAdapter.Fill($DataSet);
$DataSetTable = $DataSet.Tables["Table"];
$DSETCOUNT = $DataSetTable.Rows.Count
IF($DSETCOUNT -GT 0){
echo "A"
}
else {
echo "NULL NULL NULL"
$SqlConnection.Close()
}
$SqlConnection.Close()
}
#Finally Write the Data to the Table
$DataSet.Tables["Table"] | Export-Csv $NewFile -NoTypeINformation
#################################################
#### Data Download Concluded #############
################################################
### CALL ACCESS FOR FIVE MINUTE UPDATE
$CommandFinal = 'FiveMinuteDataUpdate'
$COMPDB = '\\operations database\compliance\cancelledlimitorders\DBCanxLIO.accdb'
$MSACCESS = New-Object -ComObject Access.Application
### this is not being done in automation, why?
$MSACCESS.OpenCurrentDatabase($COMPDB)
#$MSACCESS.AutomationSecurity = $False
$MSACCESS.Visible = $true
$MSACCESS.DoCmd.OpenForm($CommandFinal)
#$MSACCESS.RunCommand($CommandFinal)
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($MSACCESS)
#### Convert xlsx to csv file ####
$PricesToCSv = '\\Operations Database\Compliance\CancelledLimitOrders\GetCanxPriceTable.xlsx'
$PriceswbCSv = '\\Operations Database\Compliance\CancelledLimitOrders\GetCanxPriceTable.csv'
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $true
$Excel.DisplayAlerts = $false
$PricesToCSVWB = $Excel.Workbooks.Open($PricesToCSv)
$xlcsv = 6
$WorkIt = $PricesToCSVWB.ActiveSheet
$WorkIt.SaveAs($priceswbcsv,$xlcsv)
$Excel.Quit()
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($WorkIt)
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($xlCsv)
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($PricesToCSVWB)
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($Excel)
######
### CSV Converted
##############################################
Remove-Item '\\operations database\compliance\cancelledlimitorders\array.csv'
#####
### Iterate through each line of the converted csv and obtain the quote from yahoo finance writing all output to the array.csv file
$GetCanxTradeImport = '\\operations database\compliance\cancelledlimitorders\GetCanxPriceTable.csv'
$GCTI = Import-Csv $GetCanxTradeImport -Header ('LIOORDERREF', 'lioclientcode', 'liodatetimeinput', 'liosecurityid', 'PRICE1', 'PRICE2', 'EXPIRYDAYS', 'lioexpirydatetime', 'liolastupdated', 'liodatetimeoflaststatus', 'liofailurecount', 'liotransactiontype', 'liobidprice1', 'liobidprice2', 'liobidprice3', 'lioaskprice1', 'lioaskprice2', 'lioaskprice3', 'LIOCASHAMOUNT', 'LIOSTOCKSYMBOL', 'STOCKAPI', 'liocurrency', 'lioquoteprovider', 'liosecurityexchange', 'PARENTTYPE') | SELECT -Skip 1
$IE = New-Object -ComObject internetexplorer.application
ForEach ($StockApi in $GCTI.STOCKAPI) {
$URLA = "http://download.finance.yahoo.com/d/quotes.csv?s="
$URLB = "&f=sc1ll1b2&e=.csv"
#echo $URLA, $StockApi, $URLB
$FUllURL = ($URLA+ $StockApi+ $URLB)
#$IE.navigate2($FUllURL)
#$IE.visible=$true
$OP = '\\operations database\compliance\cancelledlimitorders\array.csv'
$String = Invoke-RestMethod $FullUrl
$Data = @()
$Row = New-Object PSObject
$row | Add-Member -MemberType NoteProperty -Name "STRING" -Value $String
$Data += $Row
$Data | Export-CSv $OP -NoTypeInformation -Append
###############################################################
### Has now been written out to array.csv
###############################################################
### Array file requires importing into access to allow updating of files and percentages
}
#### this is not working when automated why??????????????
$Command3 = 'LogicUpdate'
$COMPDB = '\\operations database\compliance\cancelledlimitorders\DBCanxLIO.accdb'
$MSACCESS = New-Object -ComObject Access.Application
$MSACCESS.OpenCurrentDatabase($COMPDB)
#$MSACCESS.AutomationSecurity = $False
$MSACCESS.Visible = $true
$MSACCESS.DoCmd.OpenForm($Command3)
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($MSACCESS)
## Kill resident processes
Stop-Process -Name MSACCESS
Stop-Process -Name iexplore
自动化任务简写为
Action = Start a program
Program/Script: = C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe
Add arguments (optional) = C:\scripts\ComplianceLimitOrderTestRunTime.ps1
我想知道这是否是执行政策问题,但它被设置为不受限制。
我无法理解为什么这可以作为手动执行而不是自动执行,任何人都对此有所了解?