我正在尝试在 SQL Server 代理作业中使用 Powershell 下载 zip 文件。该脚本使用 PuTTY (PSCP.exe) 从 SFTP 站点下载 zip 文件。
我遇到的问题是,当作业运行时,它会连接到 SFTP 站点,并且 PuTTY 会发回有关将服务器的主机密钥存储在注册表中的提示。我不想这样做,所以我试图将echo n命令通过管道传输到 PuTTY。但这似乎不起作用。
$SrcPath = "/somedirectory/somewhere/files/"
$DstPath = "D:\Download\"
$currentDate = (Get-Date).ToString('yyyyMMdd')
$FileName = "$currentDate.zip"
$ArchivePath = "D:\Archive\"
$File = "$SrcPath$FileName"
Set-Location $DstPath
echo n | C:\"Program Files (x86)"\PuTTY\pscp -P 99999 -pw password username@{IP_ADDRESS}:$File $DstPath
# Check the file is there
If (Test-Path "$DstPath$FileName")
{
# Unzip the contents
C:\"Program Files"\7-Zip\7z.exe e "$DstPath$FileName"
#Move the zip file to the …Run Code Online (Sandbox Code Playgroud) 我可能已经看这个太久了,无法弄清楚这一点......
下面的函数用于提取有关 SQL Server 实例的一个或多个数据库的信息。到目前为止,我知道这适用于 SQL 2005 及更高版本。
我已经使用过,Format-Table但最终会切断列。我使用了Output-Filewhich 可以工作但默认为列表视图,这可能很难进入 Excel。它还会切断超过一定长度的列数据。
编辑:此功能的目的是针对 100 多台服务器运行它以收集每个实例的清单。
PowerShell 功能:
# Load SMO
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO')
function Get-DatabaseInfo ($server,$dbname)
{
$srv = New-Object 'Microsoft.SqlServer.Management.Smo.Server' $server
$db = $srv.Databases.Item($dbname)
$DataFile = $db | Select -ExpandProperty FileGroups | Select -ExpandProperty Files
$LogFile = $db | Select -ExpandProperty LogFiles
$tables = $db | Select -ExpandProperty tables | ? {$_.IsSystemObject -eq $false}
$indexes = $tables | Select -ExpandProperty Indexes | ? …Run Code Online (Sandbox Code Playgroud) 我们最近在 Windows Server 2012(从 Windows Server 2008 R2)上升级到 SQL Server 2012(从 SQL Server 2008 R2),并且之前运行没有错误的 SQL Server 作业代理作业现在失败了。
该作业有一个 Powershell 作业步骤:
$ErrorActionPreference = 'Stop'
$DateStamp = Get-Date -format "yyyyMMdd"
$BizUnit = 'SHB'
# Target File directory
$BaseDir = '\\MyFileServer\MyDirectory'
$Query = "EXEC dbo.usp_MyQuery '$BizUnit'"
$TargetFile = "MyTargetFile_" + $BizUnit + "_$DateStamp.xml"
$TargetDir = (Join-Path -Path $BaseDir -ChildPath $BizUnit)
$Query | Out-File -FilePath (Join-Path -Path $TargetDir -ChildPath $TargetFile) -Force -Encoding "utf8"
Run Code Online (Sandbox Code Playgroud)
当作业运行时,它失败并显示以下错误消息:
作业步骤在 PowerShell 脚本的第 14 行收到错误。对应的行是 '$Query | Out-File -FilePath …
将 SQLPS 连接到 SQL Server 时,它会尝试运行一些 WMI 服务状态查询。在没有权限的环境中,PowerShell 会发出一堆警告,并且连接速度非常慢。
可以使用 $WarningPreference = 'SilentlyContinue' 来抑制警告消息,但有没有办法禁用实际检查,以提高性能?
例子:
PS SQLSERVER:\> set-location \sql\myserver\default
WARNING: Could not obtain SQL Server Service information. An attempt to connect to WMI on '<servername>' failed with the following error: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
WARNING: Could not obtain SQL Server Service information. An attempt to connect to WMI on '<servername>' failed with the following error: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
WARNING: Could not …
我有一个简单的脚本,它应该执行一个选择语句并将其输出到 CSV。我的问题是脚本连接到数据库并挂起等待我的 sql 指令(这些指令在脚本上并且应该自动执行)。任何帮助将不胜感激。
Set-Location 'E:\Program Files\PostgreSQL\9.1\bin\';
$env:PGPASSWORD = 'mypwd';
.\psql --% -U postgres -w myDB
.\psql --% -c "copy {'SELECT * FROM myTable';} TO 'C:\Users\e\Desktop\test1.csv' CSV DELIMITER ',';"
Run Code Online (Sandbox Code Playgroud)
我们不是通过 SQL Server Management Studio 为每个实例发出查询,而是尝试创建一个 powershell 脚本来通过循环引用文本文件来查询多个实例,其中假设有 100 个 SQL Server 实例。遍历每个 SQL Server 实例,发出查询,然后导出到 CSV。
下面是我们目前拥有的powershell脚本:
$ServerInstance = "C:\Users\<NAME>\Documents\InstanceList.txt"
foreach ($i in $ServerInstance)
{
$sql = "SELECT
DB_NAME(dbid) as DBName,
COUNT(dbid) as NumberOfConnections,
RTRIM(loginame) as LoginName,
RTRIM(Hostname) As HostName, Login_Time,Program_name
FROM
sys.sysprocesses
WHERE --DB_NAME(dbid) = 'genesys_wfm' and
dbid > 5
--and HostName = 'xxxx'
and loginame not in ('NT AUTHORITY\SYSTEM','ACE','domain\xxxx')
GROUP BY
dbid, loginame,Hostname, Login_Time,Program_name
order by Login_Time desc;"
Invoke-Sqlcmd -ServerInstance $i -Query $sql -ConnectionTimeout 60 -QueryTimeout 99999 …Run Code Online (Sandbox Code Playgroud) 
选择“记录到表”选项并运行作业后,当我返回此屏幕并选择“查看”时,我可以在文本文件中看到 powershell 脚本的输出。
唯一的工作步骤是调用 powershell 脚本,该脚本将字符串作为输入并在执行完 toUpper
但是,这些日志存储在哪里?
尝试使用 powershell删除现有表
$srv = new-Object Microsoft.SqlServer.Management.Smo.Server(".\CEF_2014_1")
$db = New-Object Microsoft.SqlServer.Management.Smo.Database
$db = $srv.Databases.Item("tempdb")
#drop the Table
$tb = new-object Microsoft.SqlServer.Management.Smo.Table($db, "listeningport")
$tb.Drop()
Run Code Online (Sandbox Code Playgroud)
但出现错误
PS C:\Users\craig.efrein\Dropbox\Scripts\Powershell> .\drop_table.ps1
Exception calling "Drop" with "0" argument(s):
"Drop failed for Table 'dbo.listeningport'. "
At C:\Users\craig.efrein\Dropbox\Scripts\Powershell\create_table.ps1:11 char:11
+ $tb.Drop <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Run Code Online (Sandbox Code Playgroud)
如果我先创建表,然后删除它,那就行了。
我是否正确填充了 $tb 对象?
$tb = new-object Microsoft.SqlServer.Management.Smo.Table($db, "listeningport")
Run Code Online (Sandbox Code Playgroud)
作为第二个问题,如何在 powershell 中删除表之前检查表是否存在?
题
Start-Service和Stop-Service命令启动和停止 SQL 的服务是否安全?附加信息
我们正在创建脚本,以便在维护窗口之前彻底关闭我们的系统,然后再将它们恢复。干净地我的意思是它停止应用程序的服务,然后停止数据库服务(即按依赖顺序),将所有设置为禁用,以便基础架构团队可以应用 Windows 更新/执行任何工作,根据需要安全地重新启动服务器,然后再运行脚本以使系统以正确的(依赖关系感知)顺序恢复。
我们的 DBA 提到停止服务的正确方法是通过 SQL 的配置管理器;暗示这与仅使用services.msc(或用于自动化,powershell 的stop-service/ 命令行的sc \\someServer stop someService.
但是,我找不到任何明确说明通过常规方式启动/停止服务不正确的内容,或者 SQL 配置管理器在简单启动和停止服务方面提供了哪些额外的保护/功能。
以下 PowerShell 函数在 PowerShell 2 上正常工作,但现在我正在 PowerShell 5 上对其进行测试,但无法使其工作。它旨在自动化备份恢复测试。但是由于 SQL Server 备份相关任务使用消息而不是返回记录集,因此我不得不添加一个add_InfoMessage带有处理程序的。
我想我可以做到,Invoke-Sqlcmd -Verbose但我更喜欢这种方法。
我还搜索了如何使用 SMO 进行操作,但一无所获
function Restore-Test {
param (
[string[]] $bkpPath,
[string[]] $testServer
)
$message = "Inicio: " + (get-date);
$message += "`nArchivo: " + $bkpPath;
$message += "`nTestServer: " + $testServer;
$connString = "Server=" + $testServer + ";Database=master;Integrated Security=SSPI;";
$commandString = "print 'Message from MSSQL!';";
#$commandString = "DBA_TOOLS.dbo.sp_RestoreTest N'" + $bkpPath + "'";
$conn = New-Object System.Data.SqlClient.SqlConnection $connString;
$conn.Open();
$cmd = $conn.CreateCommand(); …Run Code Online (Sandbox Code Playgroud)