2017-09-05 63 views
1

我們創建了運行特定文件夾中所有腳本的powershell腳本。PowerShell腳本無法進行Windows身份驗證

$server = Read-Host -Prompt 'Please enter server name' 
$usrname = Read-Host -Prompt 'Please enter user name' 
$passwd = Read-Host -Prompt 'Please enter password' -assecurestring 
$dbname = Read-Host -Prompt 'Please enter DB name' 
$ScriptFolder = Read-Host -Prompt 'Please enter the folder path' 

$passwd =[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($passwd)) 

$logfile = "$ScriptFolder"+"\Scripts_Log_"+(Get- 
Date).ToString("yyyyMMdd")+".txt" 

"$(Get-Date) : Queries to be executed for Database $dbname on $server " >> $logfile 

try{ 
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection 
$SqlConnection.ConnectionString = "Server=$server;Database=$dbname;trusted_connection=true;User ID=$usrname;Password=$passwd;" 
$SqlConnection.Open() 
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand 

foreach ($query in Get-ChildItem -path $ScriptFolder -Filter *.sql) 
{ 
    $qu = Get-Content $query.FullName 

    try{ 

     $command = $SqlConnection.CreateCommand() 
     $command.CommandText = $qu 

     $Reader = $Command.ExecuteReader() 

     $Reader 

     "$(Get-Date) : $query - Successfully executed`n" >> $logfile 
     write-host "$query - Successfully executed`n" -foregroundcolor "Green" 

     $Reader.close() 

     } 



catch{ 
     write-host "Unable to execute - $query :" -foregroundcolor "Red" 
     write-host "$_.Message`n" -foregroundcolor "Red" 
     "$(Get-Date) : $query - Script failed due to:" >> $logfile 
     "$_.Message`n" >> $logfile 
    } 
} 
} 

    catch{ 
    write-host "Login failed:" -foregroundcolor "Red" 
    write-host "$_.Message`n" -foregroundcolor "Red" 
    "$(Get-Date) : login failedn:" >> $logfile 
    "$_.Message`n" >> $logfile 
} 

$SqlConnection.Close() 

write-host "All the process has been completed" -foregroundcolor "Yellow" 
"$(Get-Date) : All the process has been completed" >> $logfile 

但是當這個腳本,數據庫服務器上運行,我有Windows身份驗證,那麼它沒有做裏面的東西每個循環和不失敗任。有沒有一種方法,這個腳本將同時適用於Windows和SQL身份驗證

+1

所以,當你想要的東西沒有做,那還有什麼它實際上做什麼?有錯誤嗎?要調試的東西,這是不夠的,說它不是你想要的。瞭解它**正在做什麼很重要。爲了回答你最後的問題,你的連接字符串決定了用戶的認證方式。如果您包含trusted_connection選項,那麼您將獲得 - 用戶標識和密碼將被忽略。所以要使用sql認證,你必須離開那一點。 – SMor

+0

@SMor我明確告訴它,它不會進入每個循環,只是移出循環 – DevelopmentIsMyPassion

回答

1

$ScriptFolder UNC路徑到PowerShell遠程會話中的第三臺服務器?如果是這樣,這可能是two-hop kerberos problem

你不能在第三個服務器上列出文件,所以循環是循環遍歷一個空集。您可以使用CredSSP或各種其他技術來解決它,但它們在設置IMX方面都有不同程度的痛苦。他們上面推薦的基於資源的Kerberos約束委派方法是一種我沒有使用的方法,但需要Windows Server 2012的域控制器。

您可以通過授予對第三個服務器的訪問權限文件共享到您正在運行的計算機帳戶,但具體取決於您正在執行的操作可能不起作用。

當至少有一個域控制器是Server 2012或更高版本時,$using:cred方法也可能正常工作,但顯然它不起作用(或者您不應該使用它......文章有點模棱兩可)。在這一點上,這當然應該是真的。

# This works without delegation, passing fresh creds    
# Note $Using:Cred in nested request    
$cred = Get-Credential Contoso\Administrator    
Invoke-Command -ComputerName ServerB -Credential $cred -ScriptBlock {    
    hostname    
    Invoke-Command -ComputerName ServerC -Credential $Using:cred -ScriptBlock {hostname}    
} 

這裏是討論解決方案的利弊,對這個問題的另一篇文章:

Making the second hop in PowerShell Remoting

+0

腳本文件夾不在第三個服務器上,但在我們運行腳本的同一臺服務器上。使用powershell的要點是捕獲運行的SQL查詢中的所有錯誤。以前我們使用批處理文件,但沒有捕獲錯誤 – DevelopmentIsMyPassion

+0

@DevelopmentIsMyPassion等待,爲什麼你的連接字符串使用'trusted_connection = true;用戶ID = $ usrname;密碼= $ passwd'? 'trusted_connection = true'與'Integrated Security = SSPI'相同,基本上就是「以當前登錄的用戶身份進行身份驗證」。你不能有意義地使用它並提供用戶名和密碼。 –

+0

@DevelopmentIsMyPassion我唯一能想到的其他事情是檢查Get-ChildItem -path $ ScriptFolder -Filter * .sql是否至少返回一個項目。 –

相關問題