创建PowerShell作业步骤无法执行 - 从SQLPS提示符开始工作

时间:2018-02-23 00:20:54

标签: powershell view

我正在遵循文章在SQL Server代理中运行Windows PowerShell步骤中记录的Technet步骤。当我从SQLPS命令行运行它时,它工作。

当我使用相同的未更改代码并将其放在SQL Server代理的作业步骤列表中以运行它时 - 我收到这些错误抱怨第一行。为什么使用SSMS时这是一个问题而不是SQLPS提示没有问题>

作业步骤在PowerShell脚本的第1行收到错误。

相应的行是'CD \ sql \ localhost \ default \ databases'。纠正脚本并重新安排工作。

PowerShell返回的错误信息是:'找不到路径'C:\ sql \ localhost \ default \ databases',因为它不存在。 '

作业步骤在PowerShell脚本的第2行收到错误。相应的行是'$ db = get-item Echo'。

PowerShell返回的错误信息是:'无法找到路径'C:\ Windows \ system32 \ Echo',因为它不存在。 '作业步骤在PowerShell脚本的第3行收到错误。

对应的行是'$ myview = New-Object -Typename Microsoft.SqlServer.Management.SMO.View -argumentlist $ db“TestView2”dbo'。

PowerShell返回的错误信息是:'异常调用“.ctor”带有“3”参数:“SetParent对于'dbo.TestView2'失败。”SetParent对于'dbo.TestView2'失败。值不能为空。参数名称:newParent'。处理退出代码-1。步骤失败。,00:00:00,0,0 ,,, 0

我的观点创作脚本:

CD \sql\localhost\default\databases
$db = get-item Echo
$myview = New-Object -Typename Microsoft.SqlServer.Management.SMO.View -
argumentlist $db, "TestView2", dbo
$myview.TextHeader = "CREATE VIEW [dbo].[TestView2] AS"
$myview.TextBody = @"
SELECT drname.dr_id, drname.create_dt, dr_fname, dr_iname, dr_lname, 
drtitle, drsuffix, gender, birthdate, marital, ethnic, drname.email, 
org_name, dr_ofname, dr_oiname, dr_olname, nationalid, drname.medicare, 
drname.medicaid,

CASE
               WHEN EXISTS(SELECT 1 FROM stfstatu act WHERE act.dr_id = 
drname.dr_id AND fac_cd  NOT IN('SMC','WAR','CCS') AND act.active123 IN 
('1','3','N','G','6','S','U','CA','TPRO')) THEN 'Y'
ELSE 'N'
END  as Active_Status, 
              a.sch_type as Addr_Type_sch_type, a.addr, a.addr2, a.city, 
a.state, a.zip, a.phone, a.fax, a2.tax_id,
              (select top 1 rtrim(d.dr_lname) from drname d join address aa 
on d.dr_id = aa.dr_id join address2 aa2 on aa.link = aa2.l_address where 
d.verfac = 1 and aa2.tax_id = a2.tax_id and drname.verfac = 0) as 
Addr_DrPGp,
              lan_tab.txt as lan_tab_txt, dr_lan.lan_cd as dr_lan_cd, CASE 
WHEN dr_spec.specorder = 1 THEN dr_spec.specialty ELSE '' END as dr_spec, 
CASE WHEN dr_spec.PrimSpec = 1 THEN 'P' ELSE '' END as PrimSpec, school.cd 
as school_cd, school.txt as school_txt, educate.fdate as educate_fdate, 
educate.sch_type, educ_arr.txt as school_type_txt,  educate.degree, 
educ_tab.txt 
as degree_txt, educate.tdate as educate_tdate, CASE WHEN dr_spec.specorder = 
1 
THEN spec_tab.txt ELSE '' END as spec_Tab_txt,  

                  CASE WHEN stfstatu.pcp_spec = 'B' then 'Yes' WHEN 
stfstatu.pcp_spec = 'P' then 'Yes' ELSE 'No' END as PCP, 

              case when stfstatu.pcp_spec = 'B' then 'PCP and Specialist'
      else case when stfstatu.pcp_spec = 'P' then 'PCP'
                      else case when stfstatu.pcp_spec = 'S' then 
'Specialist'
                                       else ''
                               END
                  END
                              END as PCPSpec, taxonomy.cd as tax_cd

FROM drname
JOIN stfstatu on drname.dr_id = stfstatu.dr_id
LEFT JOIN address1 a on drname.dr_id = a.dr_id AND a.sch_type = 'G' AND 
a.link = (SELECT TOP 1 link FROM address1 WHERE sch_type = 'G' AND 
address1.dr_id = drname.dr_id ORDER BY LINK DESC)
LEFT JOIN address2 a2 on a2.l_address = a.link
LEFT JOIN dr_lan on drname.dr_id = dr_lan.dr_id
LEFT JOIN lan_tab on lan_tab.cd = dr_lan.lan_cd
LEFT JOIN dr_spec on drname.dr_id = dr_spec.dr_id
LEFT JOIN spec_tab on dr_spec.specialty = spec_tab.cd
LEFT JOIN educate on educate.dr_id = drname.dr_id
LEFT JOIN school on educate.cd = school.cd
LEFT JOIN educ_arr on educ_arr.cd = educate.sch_type
LEFT JOIN facility on facility.fac_cd = educate.fac_cd
LEFT JOIN educ_tab on educate.degree = educ_tab.cd
LEFT JOIN taxonomy on taxonomy.dr_id = drname.dr_id
--Where LTRIM(RTRIM(dr_spec.Fac_Cd)) = ''
“@
$myview.Create()

1 个答案:

答案 0 :(得分:1)

我不知道这是否是最好的方法,但尝试将其放在开头,将位置设置为本地目录。

Set-Location C:\;

这有助于我的工作正确执行。从这个来源获得了https://www.sqlhammer.com/sql-agent-2016-jobs-running-powershell-scripts/