2016-11-02 15 views
1

我編寫了以下PowerShell腳本。它適用於我的非集羣SQL Server(2014版)上的作業。當我手動運行PowerShell(2012版)時,它可以在我的羣集服務器上運行。但是,當它作爲此羣集服務器上的SQL代理作業的一部分運行時,它會爲LDAP查詢中的用戶返回空結果。有任何想法嗎?我甚至沒有看到服務器在它和ldap服務器之間的防火牆上嘗試LDAP連接。這就像SQLPS甚至沒有嘗試LDAP連接。羣集實例sql作業中的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 

回答

2

通過SQLPS我推測您的意思是PowerShell作業步驟。

SQL Server 2012 uses a crippled version of PS沒有完全訪問所有cmdlet(實際上它只是PS v2.0),而SQL Server 2014具有完整的PowerShell訪問權限,允許您處理有罪不罰現象。

作爲解決您的問題的解決方法,您可以利用操作系統(CmdExec)作業步驟並向腳本發出呼叫,並將其保存爲.PS1文件。