【发布时间】:2020-03-15 07:58:16
【问题描述】:
我有一个脚本,它从 AD 中提取一些信息,将行插入临时表,然后调用一个 SQL 脚本,将行转换并插入到人行横道表中。该脚本在 ISE 中运行良好,但在 TaskScheduler 中运行时失败,无论是手动运行还是计划运行。
在“ACTION”页面,程序为“powershell.exe”,参数为“-executionpolicy bypass C:\scripts\SysManagement\Populate_AD_Xwalk.ps1”。上次运行结果为 (0x1)。
知道有什么问题吗?
谢谢
# Invoke-sqlcmd Connection string parameters
$params = @{'server'='xxx';'UserName'='xxx';'Password'='xxx'; 'Database'='xxx'}
######################
# Function to manipulate the data
Function writeDiskInfo
{
param($UPN,$EMAIL,$SAM,$ACTIVE)
$InsertResults = @"
INSERT INTO [xxx].[dbo].[WORK_UPN_Email](UPN, EMAIL, SAM, ACTIVE)
VALUES ('$UPN','$EMAIL','$SAM', '$ACTIVE')
"@
# call the invoke-sqlcmdlet to execute the query
Invoke-sqlcmd @params -Query $InsertResults
}
#####################
# Query AD objects and store in an array
$dp = Get-ADUser -property 'emailaddress' -Filter *
# Loop through array and insert into WORK table
foreach ($item in $dp)
{
# Call the function to transform the data and prepare the data for insertion
writeDiskInfo $item.UserPrincipalName $item.EmailAddress $item.SamAccountName $item.Enabled
}
# Call SQL procedure to delete rows with blank upns and upsert crosswalk table
Invoke-Sqlcmd @params -Query "EXEC ZZproc_Upsert_AD_Email"
【问题讨论】:
-
初步猜测获得许可,在步骤之间使用 add-content 来跟踪它走了多远。甚至可以添加一行来首先记录“whoami”的输出。检查窗口事件、sql 错误事件并启用 sql 跟踪会话以获取可能更有用的内容。
-
谢谢。首先,我在其中添加了一行,该行创建了一个带有 whoami 输出的文本文件。当我从 ISE 运行时,会写入文件。从调度程序运行时不会创建文件。
-
嗯。如果没有更具体的消息,很难进行故障排除。尝试各种任务计划程序设置。首先,确保您在上面粘贴的句点实际上并不存在于参数字段的末尾。然后测试各种组合,包括在路径“c:\scripts\SysManagement\”中添加开始,确保操作系统正确等。至少尝试触发错误或使用更简单的 ps1 运行可能在打开的临时文件夹中每个人都读/写。
标签: powershell scheduled-tasks taskscheduler