我安装了带有SSDT的 VS2015 ,以及 SSMS和SqlServer PowerShell模块(包括invoke-sqlcmd命令),但是如果我尝试对Azure SQL数据仓库执行查询,如下所示:
invoke-sqlcmd -Query "Select top 5 * from customer" -ConnectionString "Server=tcp:my.database.windows.net,1433;Database=Customer; Authentication=Active Directory Integrated; Encrypt=True; "
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
invoke-sqlcmd : Unable to load adalsql.dll (Authentication=ActiveDirectoryIntegrated). Error code: 0x2. For more information, see
http://go.microsoft.com/fwlink/?LinkID=513072
At line:1 char:1
+ invoke-sqlcmd -Query "Select top 5 * from vwOffer" -ConnectionStrin ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Invoke-Sqlcmd], SqlException
+ FullyQualifiedErrorId : SqlExectionError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand
invoke-sqlcmd :
At line:1 char:1
+ invoke-sqlcmd -Query "Select top 5 …Run Code Online (Sandbox Code Playgroud) 我有一个PowerShell脚本,它检查它运行的服务器的CPU级别,然后如果它超过某个阈值,它将运行SQL存储过程和电子邮件.
该脚本在我的开发服务器上使用最新版本的PowerShell正确运行.但是,我Invoke-Sqlcmd在实时服务器上运行命令时遇到问题,我需要从中获取CPU值.为了举例,它可以称为LIVESERVER.它正在运行PowerShell 2.0,我认为是这个问题:
术语"Invoke-Sqlcmd"未被识别为cmdlet,函数,脚本文件或可操作程序的名称.检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试.
我宁愿不对此服务器进行任何更改,因为它对业务至关重要(除非这是一个简单的任务,无需重新启动即可更新PowerShell等).
是否可以从我的开发服务器运行我的脚本并指定服务器以获取CPU数据?我不确定语法如何实现这一点.
这是我的脚本:
# Email
$recipients = @("Ricky <ricky@email.com>")
# Loop 20 times
for ($i= 1; $i -le 20; $i++) {
# Find CPU average usage percentage for given time period
$cpuutil = (Get-Counter -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 1 -MaxSamples 20 |
select -ExpandProperty countersamples |
select -ExpandProperty cookedvalue |
Measure-Object -Average).Average
# Display average CP output
$cpuutil
# If CPU average percentage is higher than given value, run stored procedure and
# …Run Code Online (Sandbox Code Playgroud) 我从未见过如此简单的脚本如此失败:
$SQLServer = "localhost"
$cred = Get-Credential
invoke-sqlcmd -ServerInstance $SQLServer -Credential $cred -Query "select @@version"
Run Code Online (Sandbox Code Playgroud)
这句话说 -Credentials不被认可:
Invoke-Sqlcmd : A parameter cannot be found that matches parameter name 'Credential'.
Run Code Online (Sandbox Code Playgroud)
那么有什么意义Get-Credential呢?我在互联网上看到很多例子,他们都是这样使用的。
编辑示例:为什么此代码正在使用-Credential?因为-Credential在函数内部?
function Pause ($Message="Press any key to continue..."){
""
Write-Host $Message
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
function GetCompName{
$SQLServer = Read-Host "Please enter a computer name or IP"
CheckHost
}
function CheckHost{
$ping = gwmi Win32_PingStatus -filter "Address='$SQLServer'"
if($ping.StatusCode -eq 0){$pcip=$ping.ProtocolAddress; GetCollation}
else{Pause …Run Code Online (Sandbox Code Playgroud) 我已经在 14 个查询中吐出了Glenn Berry 的性能查询,但其中一个(第 9 个)不起作用。
这是我的 PowerShell 代码:
#Provide SQLServerName
$SQLServer ="localhost"
#Provide Database Name
$DatabaseName ="master"
#Prompt for user credentials
$credential = Get-Credential
$Query1= "-- 09 Index Sizes
-- Note: THIS IS SLOW as it reads index blocks. SAMPLED is not that high, but watch for prod I/O impact if using 'DETAILED'
SELECT DB_NAME() AS DatabaseName,
Object_name(i.object_id) AS TableName
,i.index_id, name AS IndexName
,i.type_desc
,ips.page_count, ips.compressed_page_count
,CAST(ips.avg_fragmentation_in_percent as DECIMAL(5,1)) [fragmentation_pct]
,CAST(ips.avg_page_space_used_in_percent as DECIMAL(5,1)) [page_space_used_pct]
,ips.index_depth, …Run Code Online (Sandbox Code Playgroud) 你们中有人知道使用 PowerShell 与启用了 MFA 的 Azure AD 帐户执行 SQL 命令的选项吗?什么是替代方案?我需要为此创建一个服务主体吗?
我运气不好,只找到了这个 cmdlet,
Add-SqlAzureAuthenticationContext
但是当我尝试运行时Invoke-Sqlcmd,出现以下错误:
Invoke-Sqlcmd:目标主体名称不正确。无法生成 SSPI 上下文。
我正在使用powershell和使用Invoke-SqlCmd.我能够将变量传递给SQL:
$variables = @( "MyVariable='hello'" )
Invoke-SqlCmd `
-ServerInstance 'localhost' `
-Database 'master' `
-Username 'matthew' `
-Password 'qwerty' `
-Query 'SELECT $(MyVariable) AS foo' `
-Variable $variables
Run Code Online (Sandbox Code Playgroud)
这让我回归正常hello.但是,如果我有一个包含equals(=)的值的变量:
$variables = @("MyVariable='aGVsbG8NCg=='") # base64 encoded 'hello'
Run Code Online (Sandbox Code Playgroud)
它给了我以下错误:
用于为
Invoke-Sqlcmdcmdlet 定义新变量的格式无效.请使用'var = value'格式来定义新变量.
我找不到在任的任何文件sqlcmd或Invoke-SqlCmd上我应该如何正确逃生值.
如何将变量发送到sqlcmd/ Invoke-SqlCmd?
输出示例:
#TYPE System.Data.DataRow <---
"PersonalNr" <---
"00001005"
"00001008"
"00001009"
"00001013"
"00001019"
"00001024"
Run Code Online (Sandbox Code Playgroud)
要求:
我想要一个没有前 2 行和引号的输出,我该怎么做?
对于标题,我尝试了选项,-h -1但输出仍然相同。甚至有可能Sqlcmd吗?
当前代码:
Invoke-Sqlcmd -ServerInstance SERVER -h -1 -Query $QueryFmt | Export-CSV $AttachmentPath
Run Code Online (Sandbox Code Playgroud) 我用巧克力安装了 ms sql server:
choco install SQLServer2012DeveloperEditionWithSP1 -y -f -source 'http://choco.developers.tcpl.ca/chocolatey' -c "$env:WINDIR\temp"
Run Code Online (Sandbox Code Playgroud)
SQL 似乎已安装并在无法正常工作的 powershell 之外运行良好。我可以看到 sqlps 模块:
Get-Module -listavailable
Run Code Online (Sandbox Code Playgroud)
...
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Manifest 1.0 SQLASCMDLETS
Manifest 1.0 SQLPS
Run Code Online (Sandbox Code Playgroud)
但是命令似乎丢失了。我没有 invoke-sqlcmd 等。理论上,如果我安装模块,我应该可以访问它们,但是当我尝试导入模块 sqlps 时,却收到关于没有 sqlserver 驱动器的错误:
PS C:\WINDOWS\system32> Import-Module SQLPS
Set-Location : Cannot find drive. A drive with the name 'SQLSERVER' does not exist.
At C:\Program Files (x86)\Microsoft SQL Server\110\Tools\PowerShell\Modules\SQLPS\SqlPsPostScript.ps1:1 char:1
+ Set-Location SQLSERVER:
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (SQLSERVER:String) [Set-Location], DriveNotFoundException …Run Code Online (Sandbox Code Playgroud) 我们最近开始使用Sql Server 2012 SP3,并使用powershell脚本构建SQL Server 2012。我们的自动化要求在db上运行多个数据库脚本,并且我发现Invoke-Sqlcmd非常可靠,直到发现此问题为止。当我在最近安装SQL Server的系统上以Powershell调试模式运行带有适当参数集的Invoke-sqlcmd时。我没问题 PowershellCommand:Invoke-Sqlcmd -InputFile $ sStrJBSPExecRolePath -ServerInstance $ sStrSQLName -ErrorAction Stop 但是,当我在重建同一服务器后通过Powershell 自动化脚本执行相同查询时,最终遇到错误
术语“ Invoke-Sqlcmd”不能识别为cmdlet,函数,脚本文件或可运行程序的名称。检查名称的拼写,或者是否包含路径,请验证路径是否正确,然后重试。
我在网上做了很多建议导入sqlps等的在线研究,因此为了测试,我在脚本中添加了以下内容
get-pssnapin-注册的导入模块“ sqlps” -DisableNameChecking
即使在脚本中添加了以上内容。我仍然会遇到同样的错误。但是当我手动运行相同的脚本时,它运行得很好。我不明白有什么问题。powershell自动化脚本-该脚本安装.Net Framework 3.5,SQL Server 2012,SQL Server 2012 SP3,然后加载用于更改sql设置(例如SQL的最大内存限制)的smo程序集。
我正在开发一个 Powershell 脚本,该脚本将 CSV 文件作为输入,其中包含有关服务器和每个服务器上的数据库的信息,以便为文件中的每个服务器和数据库创建特定的登录名和用户。该脚本尝试使用 Invoke-SqlCmd 通过 SQL 命令创建登录名和用户,然后将尝试结果输出到新的 CSV 文件。如果成功添加登录名和用户,则输出数据并显示成功消息,如果任一命令失败,则应输出数据并显示失败消息。现在,如果出现 Invoke-SqlCmd 错误,脚本不会确认发生了故障并输出运行成功。
我需要一些帮助来弄清楚如何编写我的 Try/Catch 块,以便它们真正捕获 SQL 命令失败并输出正确的消息。
我当前针对这些部分的 Try/Catch 块:
Try # Create Login
{
# Importing sqlserver module to run the commands
if (-not (Get-Module -Name "sqlserver"))
{
Import-Module sqlserver
}
Write-Host "Checking server $fullServerName to see if login [Example_Login] exists. Will create login if it does not already exist."
Invoke-Sqlcmd -ServerInstance $fullServerName -Query "$loginCommand"
Write-Host "Login command successfully executed.`n"
}
Catch
{
Write-Host "An error has occurred …Run Code Online (Sandbox Code Playgroud) 使用 invoke-sqlcmd 将行插入表时发生了一个奇怪的错误。如果执行一次,整个脚本可以完美运行,但是如果我第二次运行它,它会失败并显示错误消息:Get-ChildItem:无法调用方法。提供程序不支持使用过滤器。我测试了代码并注释掉了 invoke-sqlcmd 行,并且可以多次运行它而没有任何错误。我将 invoke-sqlcmd 放在一个函数中,但它仍然出错,并且在第一次成功运行后,它也会在 PS 命令行中失败。
Clear-Host
$ErrorActionPreference = 'Stop'
function Update-SQL
{
invoke-sqlcmd -query $strSQLInsert -server SXLSV-LEAPDBD1 -database DTCS_DV
}
$files = Get-ChildItem '\\pcslog1011\dtcs\ContractEmailNotes\' -Filter '*.txt' | Where-Object {$_.LastWriteTime -ge '08/22/2016 00:00:00' -and $_.LastWriteTime -le '08/22/2016 23:59:59'} | sort-object -Descending
for ($i=0; $i -lt $files.Count; $i++)
{
$SNAC_CNTR_ID = $files[$i].BaseName.Substring(0,6)
$SNAC_MODIFIED = '{0:yyyy-MM-dd HH:mm:ss}' -f ($files[$i].LastWriteTime)
$reader = [System.IO.File]::OpenText($files[$i].FullName)
$lineCnt = 0
$SNAC_ACTION = ''
for()
{
$lineCnt += 1
$line = $reader.ReadLine()
if ($line -eq …Run Code Online (Sandbox Code Playgroud) invoke-sqlcmd ×11
powershell ×8
sql-server ×5
cpu-usage ×1
multi-factor-authentication ×1
sqlcmd ×1
sqlps ×1
windows ×1