简体   繁体   English

Powershell 如何通过管道传输交互式命令

[英]Powershell how to pipe an interactive command

I am connection to a database via an interactive SQL-Command-Line-Utility and when I am executing an SQL command, I want to pipe the output to another power shell cmdlet, for example out-gridview.我通过交互式 SQL-Command-Line-Utility 连接到数据库,当我执行 SQL 命令时,我想将输出通过管道传输到另一个 power shell cmdlet,例如 out-gridview。

Lets say for example I connect to a Sybase ASE database, and I login with比如说我连接到一个 Sybase ASE 数据库,然后我登录

C:\users\user>isql -U<user> -S<SI>
[password]:
1>use master
2>go
1>SELECT * FROM sysusers
2>go | out-gridview

The pipe to out-gridview, nor any other cmdlet, is working.到 out-gridview 的管道或任何其他 cmdlet 都在工作。 I can redirect the output to a file via > nonetheless, but I think its pre-implemented in the isql-command-line-utility.我可以通过 > 将输出重定向到一个文件,但我认为它是在 isql-command-line-utility 中预先实现的。

Has anyone an idea how to pipe this stuff?有谁知道如何用管道输送这些东西? I am well aware that there are scripts like this我很清楚有这样的脚本

$query="select * from syslogins"
$conn=New-Object System.Data.Odbc.OdbcConnection
$conn.ConnectionString= "driver={Adaptive Server Enterprise};
dsn=SERVER_NAME;db=master;na=IP,PORT;uid=SOMEUSER;pwd=******;"
$conn.open()
$cmd=new-object System.Data.Odbc.OdbcCommand($query,$conn)
$cmd.CommandTimeout=30
write-host $query
$ds=New-Object system.Data.DataSet
$da=New-Object system.Data.odbc.odbcDataAdapter($cmd)
write-host $ds
$da.fill($ds)
$ds.Tables[0] | out-gridview
$conn.close()

But I dont' want to store my password in clear text inside of a script.但我不想在脚本中以明文形式存储我的密码。 I want to login to a session, then execute the commands and then pipe my information.我想登录到一个会话,然后执行命令,然后通过管道传输我的信息。

You can't use cmdlets inside a console application.您不能在控制台应用程序中使用 cmdlet。 Console applications return text, while cmdlets work with object.控制台应用程序返回文本,而 cmdlet 使用对象。 You would need to use something like your second sample or run a "script" in isql and parse the text-output to objects.您需要使用类似于第二个示例的内容或在isql运行“脚本”并将文本输出解析为对象。

Since you don't want to automate this you could simply use Read-Host to get the password when running the script.由于您不想自动执行此操作,因此您可以在运行脚本时简单地使用Read-Host来获取密码。

#Ask for password
$pass = Read-Host Password

$query="select * from syslogins"
$conn=New-Object System.Data.Odbc.OdbcConnection

#User password-variable in string
$conn.ConnectionString= "driver={Adaptive Server Enterprise};
dsn=SERVER_NAME;db=master;na=IP,PORT;uid=SOMEUSER;pwd=$pass;"
$pass = ""
$conn.open()
$cmd=new-object System.Data.Odbc.OdbcCommand($query,$conn)
$cmd.CommandTimeout=30
write-host $query
$ds=New-Object system.Data.DataSet
$da=New-Object system.Data.odbc.odbcDataAdapter($cmd)
write-host $ds
$da.fill($ds)
$ds.Tables[0] | out-gridview
$conn.close()

U can replace你可以更换

go || out-gridview with go > out-gridview In this way, U just redirected the output to out_gridview. out-gridviewgo > out-gridview这样,U 只是将输出重定向到 out_gridview。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM