我有以下从 VB 脚本转换而来的 powershell 命令:
$conn = New-Object System.Data.SqlClient.SqlConnection(“Data Source=SERVER; Initial Catalog=DATABASE; Integrated Security=SSPI”)
$conn.Open()
$cmd1 = $conn.CreateCommand()
$cmd1.CommandType = [System.Data.CommandType]::Text
$cmd1.CommandText =“SELECT guid, name, [Serial Number] FROM dbo.wrksta as a inner join Inv_AeX_HW_Serial_Number as b on a.GUID=b._resourceGUID ORDER BY Name”
$data = $cmd1.ExecuteReader()
$cmd2 = $conn.CreateCommand()
$cmd2.CommandType = [System.Data.CommandType]::Text
$cmd2.CommandText =“SELECT top 1 [User] FROM dbo.Evt_aex_client_logon WHERE _resourceGuid = (SELECT top 1 Guid FROM dbo.Wrksta WHERE Name='" + $data["name"] + "' ORDER BY WrkstaID DESC) AND Event = 'logon' AND _eventTime > getdate()-60 GROUP BY [User] ORDER BY count([User]) desc”
$data2 = $cmd2.ExecuteReader()
$dt = new-object “System.Data.DataTable”
$dt.Load($data2)
$dt | format-table
$conn.Close()
如果您查看第二个 select 语句,我想使用第一个变量 $data 中的值,但我不确定如何编写它。在 VB 中就像这样 WHERE Name='" + Workstation("name").value + "' 以下是在 powershell 中编写此代码的正确方法吗?'" + $数据["名称"] + "'。我在 $date 变量中存储的名称列中的值之后
以前用来做这个的VB如下
connString = "Provider=sqloledb;Data Source=SERVER;Initial Catalog=DATABASE;Integrated Security=SSPI;"
objSQL.Open connString
sqlWorkstations = "SELECT guid, name, [Serial Number] FROM dbo.wrksta as a inner join Inv_AeX_HW_Serial_Number as b on a.GUID=b._resourceGUID ORDER BY Name"
Set workstation = objSQL.Execute(sqlWorkstations)
Do Until (workstation.EOF)
sqlUser = "SELECT top 1 [User] FROM dbo.Evt_aex_client_logon WHERE _resourceGuid = (SELECT top 1 Guid FROM dbo.Wrksta WHERE Name='" + workstation("name").value + "' ORDER BY WrkstaID DESC) AND Event = 'logon' AND _eventTime > getdate()-60 GROUP BY [User] ORDER BY count([User]) desc"
set user = objSQL.Execute(sqlUser)
if not user.eof then
userName = user("User").value
else
username = "??????"
end if