2017-09-05 26 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是否至少返回一个项目。 –

相关问题