Solved

PowerShell - How do I pass the current iteration to a sql query from select object hashtable?

Posted on 2016-09-28
2
74 Views
Last Modified: 2016-09-28
I don't know if I am even asking this correctly. I am using Get-ADComputer to return computers that have not logged in for so long. I am also pulling some other information from this object as well as a test-communication to see if it is currently online. Lastly, I am trying to query a database that will show me if the computer is marked as retired in our inventory system. However, the Inventory query is using the full array of objects in the select rather than the current computer object being used in the reset of the hash table. Below is my current iteration of the script:
$sql= invoke-sqlcmd -query "Select 
a.Retired as retired
FROM [Inventory].[dbo].[Asset] a
JOIN [Inventory].[dbo].[AssetHistory] ah
ON a.AssetID=ah.AssetID
WHERE Description like '$($_.Name)'
and a.Retired = 0
" -database INVENTORY -serverinstance SERVER 

$DaysInactive = 120  
$time = (Get-Date).Adddays(-($DaysInactive)) 

    Get-ADComputer -Filter {LastLogonTimeStamp -lt $time} -Properties LastLogonTimeStamp | 
    select @{Name="Computer";Expression={$_.Name}},
    @{Name="Stamp"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}},
    @{Name='OU1';expression={$_.distinguishedName.split(",")[1].substring(3)}},
    @{Name='OU2';expression={$_.distinguishedName.split(",")[2].substring(3)}},
    @{Name="Pingable";Expression={"$($(Test-Connection $_.Name -count 1 -quiet).ToString())"}},
    @{Name="InInventory";expression={$sql.Retired}}; 

Open in new window


And here is the output:

Computer    : RIPHOPERAOLD
Stamp       : 3/12/2016 1:32:10 AM
OU1         : Servers
OU2         : phr
Pingable    : False
InInventory : {False, False, False, False...}

Notice the InIventory shows an array of results rather than only the current computer results.
0
Comment
Question by:0Paulie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 41819686
WHERE Description like '$($_.Name)', I guess here you are trying to query the computer name returned from Get-ADComputer

$sql.Retired wont work as a expression inside the custom property. You can use the SQL query as a function to query against each computer name.
Example..
Function SQLQuery ($Name) {
Invoke-Sqlcmd -query "Select 
a.Retired as retired
FROM [Inventory].[dbo].[Asset] a
JOIN [Inventory].[dbo].[AssetHistory] ah
ON a.AssetID=ah.AssetID
WHERE Description like '$Name'
and a.Retired = 0
" -database INVENTORY -serverinstance SERVER 
}
$DaysInactive = 120  
$time = (Get-Date).Adddays(-($DaysInactive)) 

    Get-ADComputer -Filter {LastLogonTimeStamp -lt $time} -Properties LastLogonTimeStamp | 
    select @{Name="Computer";Expression={$_.Name}},
    @{Name="Stamp"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}},
    @{Name='OU1';expression={$_.distinguishedName.split(",")[1].substring(3)}},
    @{Name='OU2';expression={$_.distinguishedName.split(",")[2].substring(3)}},
    @{Name="Pingable";Expression={"$($(Test-Connection $_.Name -count 1 -quiet).ToString())"}},
    @{Name="InInventory";expression={(SQLQuery $_.Name).Retired}}

Open in new window

0
 
LVL 1

Author Closing Comment

by:0Paulie
ID: 41819833
This worked perfectly. Thanks.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question