Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用Invoke SqlCmd和Invoke RestMethod创建Powershell脚本?_Rest_Powershell_Powershell 2.0_Powershell 3.0_Sqlcmd - Fatal编程技术网

无法使用Invoke SqlCmd和Invoke RestMethod创建Powershell脚本?

无法使用Invoke SqlCmd和Invoke RestMethod创建Powershell脚本?,rest,powershell,powershell-2.0,powershell-3.0,sqlcmd,Rest,Powershell,Powershell 2.0,Powershell 3.0,Sqlcmd,Powershell v3添加了Invoke WebRequest和Invoke RestMethod,但我无法从该版本获得Invoke SqlCmd。如果我回到Powershell v2,我可以调用SqlCmd,但现在调用WebRequest和调用RestMethod不再可用 我可以“调用表达式sqlcmd”或“&sqlcmd”,但我认为我必须手动将输出(这只是一个字符串)封送到PS更为对象化的对象 是否有任何方法可以同时使用所有这些方法,最好是从PS3中使用这些方法?调用SqlCmd确实做到

Powershell v3添加了Invoke WebRequest和Invoke RestMethod,但我无法从该版本获得Invoke SqlCmd。如果我回到Powershell v2,我可以调用SqlCmd,但现在调用WebRequest和调用RestMethod不再可用

我可以“调用表达式sqlcmd”或“&sqlcmd”,但我认为我必须手动将输出(这只是一个字符串)封送到PS更为对象化的对象


是否有任何方法可以同时使用所有这些方法,最好是从PS3中使用这些方法?

调用SqlCmd确实做到了这一点,它使用给定的参数执行外部的
sclcmd

备选方案:

Import-Module SQLPS
# Create connection to server using Integrated Security
$ServerName = "localhost"
$DBname = "master"

$sqlConn = New-Object -Type:Microsoft.SqlServer.Management.Common.ServerConnection -ArgumentList:$ServerName
$server = New-Object -Type:Microsoft.SqlServer.Management.Smo.Server -ArgumentList:$sqlConn
$Database = $server.Databases[$DBname]

# Get script from file:
#$Script = (Get-Content $ScriptFileName) -Join [Environment]::NewLine
$Script = "sp_who"
$Batch = New-Object -TypeName:Collections.Specialized.StringCollection
$Batch.AddRange($Script)
$result = $Database.ExecuteWithResults($Batch)

$result.Tables | Format-Table

对于我的问题,我没有找到直接的答案,只是找到了一个解决办法

安装Powershell 4并再次使用installutil为Sql Powershell管理单元注册.dll文件似乎已经奏效。无论我在运行PS 3时对installutil做了什么,它都会为Powershell的第2版注册管理单元,这是行不通的

Get-PSSnapin-Registered
将始终显示正确注册的SQL*100 DLL,但对于Powershell的版本2。在运行版本4时,对相同的DLL重复相同的命令将为版本4注册它们。从那里,我可以运行
Add-PSSnapin-Sql*100
,然后
调用SqlCmd


尽管这对我有所帮助,但很可能是环境问题出了问题,这个答案可能对任何人都没有多大帮助。

您是否在v3中明确添加了SQL Server Cmdlet管理单元?也许你的个人资料里有什么?试着注册PSSnapin。您是否看到列出的管理单元可用?如果是这样,请尝试使用Add-PSSnapin添加它。谢谢,
格式表
管道看起来像是我在将文本输出解析为更客观的内容时丢失的位。