我编写了以下PowerShell脚本。它适用于我的非群集SQL Server(2014版)上的作业。当我从PowerShell(2012版)手动运行它时,它可以在我的集群服务器上运行。但是,当我将此查询中的用户作为此群集服务器上的SQL代理作业的一部分运行时,它将为LDAP查询中的用户返回null结果。有任何想法吗?我甚至没有看到服务器在它和ldap服务器之间的防火墙上尝试LDAP连接。就像SQLps甚至没有尝试LDAP连接一样。
#set up sql connection
$DBServer = "(<servername redacted for this post>)"
$DBName = "Aection"
$tableName = "mcommunity.phonenumbers"
$sqlConnection = new-object system.data.sqlclient.sqlconnection
$sqlconnection.connectionString = "Data Source='$dbserver';Integrated Security=SSPI;Initial Catalog='$dbname'"
$sqlConnection.Open()
#get uniquenames
$command = $sqlconnection.CreateCommand()
$command.CommandText = "select distinct uniquename from PE_employee"
$users = New-Object System.Data.DataTable
$users.load($command.ExecuteReader())
#get telephone numbers for aec users from mcommunity
$auth = [System.DirectoryServices.AuthenticationTypes]::Anonymous
$domain = "LDAP://ldap.umich.edu"
$de= new-object System.DirectoryServices.DirectoryEntry ($domain, $null, $null, $auth)
#clear out mcommunity phone numbers table
$result=Invoke-Sqlcmd -database $dbname -serverinstance $DBServer -outputsqlerrors $true -query "truncate table $tablename"
#for each AEC user, insert phonenumbers into peoplesoft.phonenumbers table in aection
foreach($user in $users){
$user.uniquename
$filter = "(&(objectclass=umichperson)(uid=" + $user.uniquename + "))"
$ds = New-object System.DirectoryServices.DirectorySearcher($de,$filter)
$ds.PropertiesToLoad.add("*") | out-null
$ds.PropertiesToLoad.add("telephonenumber") |out-null
$ds.PropertiesToLoad.add("mobile")|out-null
$ldapuser = $ds.Findall()
$ldapuser
$ldapuser.Properties.telephonenumber
$ldapuser.Properties.mobile
if ($ldapuser.properties.telephonenumber -ne $null) { #must have at least one telephone number
$uniquename = $ldapuser.Properties.uid
$phone1 = $ldapuser.properties.telephonenumber[0] -replace ('/','-')
$phone2 = $ldapuser.properties.telephonenumber[1] -replace ('/','-')
$mobile = $ldapuser.properties.mobile
$result=Invoke-Sqlcmd -database $dbname -serverinstance $DBServer -outputsqlerrors $true -query "insert into $tablename values('$uniquename','$phone1','$phone2','$mobile')"
}
else { #if user only has a mobile number
$uniquename = $ldapuser.Properties.uid
$mobile = $ldapuser.properties.mobile
$result=Invoke-Sqlcmd -database $dbname -serverinstance $DBServer -outputsqlerrors $true -query "insert into $tablename values('$uniquename','$NULL','$NULL','$mobile')"
}
}
$sqlConnection.close
答案 0 :(得分:2)
通过SQLPS,我认为你的意思是PowerShell工作步骤。
SQL Server 2012 uses a crippled version of PS没有对所有cmdlet的完全访问权限(实际上它只是PS v2.0),而SQL Server 2014具有完整的PowerShell访问权限,允许您处理有罪不罚现象。
作为您的问题的解决方法,您可以使用操作系统(CmdExec)作业步骤并调出脚本,保存为.PS1文件。