2014-06-23 38 views
0

我想向此命令添加一個打開的變量,因此當數據從第24行中拉出時,它將其添加到變量並使其可執行。如何將數據從文件添加到PowerShell中的打開變量

$data = Get-Content "C:\Users\bgriffiths\Documents\test.dat" 
$data[24] 

我曾嘗試加入不同的格式,做到這一點,似乎沒有任何工作。

一個命令我想是

invoke-command sql -query = $data 

我得到一個錯誤,告訴我

Invoke-Command : A parameter cannot be found that matches parameter name 'query'. 
At line:4 char:26 
+ invoke-command sql -query <<<< = $data 
+ CategoryInfo   : InvalidArgument: (:) [Invoke-Command], ParameterBindingException 
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.InvokeCommandCommand 

另一個命令我一直在試圖運行是

$Command.CommandType.text = $data 

唯一的錯誤我得到從這是

Property 'text' cannot be found on this object; make sure it exists and is settable. 
    At line:10 char:30 
    +   $Command.CommandType. <<<< text = $data    
+ CategoryInfo   : InvalidOperation: (text:String) [], RuntimeException 
+ FullyQualifiedErrorId : PropertyNotFound 

我在迷失如何將文件數據導入腳本並讓它運行它。

+0

那是什麼線的聯繫?你想要執行什麼命令?而$ data [24]將引用第25行,因爲數組$ data開始於記錄0而不是記錄1. – TheMadTechnician

+0

它試圖運行的命令是DROP SYNONYM DATABASE1.BTXSUPB; – bgrif

+0

你爲什麼使用'Invoke-Command'?你是否試圖在另一臺計算機上執行該命令?你試圖執行的'sql'是一個exe文件嗎? –

回答

0

我想通了如何通過我的PowerShell的,我不得不actaully導入部件列表中的SQL PS,然後我就能夠運行Invoke-SqlCmd

腳本爲SQLPS信息添加到運行SQL命令您的Windows PS是 -

$ErrorActionPreference = "Stop" 

$sqlpsreg="HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.SqlServer.Management.PowerShell.sqlps" 

if (Get-ChildItem $sqlpsreg -ErrorAction "SilentlyContinue") 
{ 
    throw "SQL Server Powershell is not installed." 
} 
else 
{ 
    $item = Get-ItemProperty $sqlpsreg 
    $sqlpsPath = [System.IO.Path]::GetDirectoryName($item.Path) 
} 


# 
# Preload the assemblies. Note that most assemblies will be loaded when the provider 
# is used. if you work only within the provider this may not be needed. It will reduce 
# the shell's footprint if you leave these out. 
# 
$assemblylist = 
"Microsoft.SqlServer.Smo", 
"Microsoft.SqlServer.Dmf ", 
"Microsoft.SqlServer.SqlWmiManagement ", 
"Microsoft.SqlServer.ConnectionInfo ", 
"Microsoft.SqlServer.SmoExtended ", 
"Microsoft.SqlServer.Management.RegisteredServers ", 
"Microsoft.SqlServer.Management.Sdk.Sfc ", 
"Microsoft.SqlServer.SqlEnum ", 
"Microsoft.SqlServer.RegSvrEnum ", 
"Microsoft.SqlServer.WmiEnum ", 
"Microsoft.SqlServer.ServiceBrokerEnum ", 
"Microsoft.SqlServer.ConnectionInfoExtended ", 
"Microsoft.SqlServer.Management.Collector ", 
"Microsoft.SqlServer.Management.CollectorEnum" 


foreach ($asm in $assemblylist) 
{ 
    $asm = [Reflection.Assembly]::LoadWithPartialName($asm) 
} 

# 
# Set variables that the provider expects (mandatory for the SQL provider) 
# 
Set-Variable -scope Global -name SqlServerMaximumChildItems -Value 0 
Set-Variable -scope Global -name SqlServerConnectionTimeout -Value 30 
Set-Variable -scope Global -name SqlServerIncludeSystemObjects -Value $false 
Set-Variable -scope Global -name SqlServerMaximumTabCompletion -Value 1000 

# 
# Load the snapins, type data, format data 
# 
Push-Location 
cd $sqlpsPath 
Add-PSSnapin SqlServerCmdletSnapin100 
Add-PSSnapin SqlServerProviderSnapin100 
Update-TypeData -PrependPath SQLProvider.Types.ps1xml 
update-FormatData -prependpath SQLProvider.Format.ps1xml 
Pop-Location 

Write-Host -ForegroundColor Yellow 'SQL Server Powershell extensions are loaded.' 
Write-Host 
Write-Host -ForegroundColor Yellow 'Type "cd SQLSERVER:\" to step into the provider.' 
Write-Host 
Write-Host -ForegroundColor Yellow 'For more information, type "help SQLServer".' 

,我發現這是http://blogs.msdn.com/b/mwories/archive/2008/06/14/sql2008_5f00_powershell.aspx

相關問題