问题
My script is populating a datarow from a stored procedure in SQL Server. I then reference specific columns in this datarow throughout the script. What I'm trying to do is add functionality that takes action X if the row count = 0, action Y if the row count = 1, and action Z if the row count > 1.
-- PowerShell script snippet
# $MyResult is populated earlier;
# GetType() returns Name=DataRow, BaseType=System.Object
# this works
ForEach ($MyRow In $MyResult) {
$MyFile = Get-Content $MyRow.FileName
# do other cool stuff
}
# this is what I'm trying to do, but doesn't work
If ($MyResult.Count -eq 0) {
# do something
}
ElseIf ($MyResult.Count -eq 1) {
# do something else
}
Else {
# do this instead
}
I can get $MyResult.Count to work if I'm using an array, but then I can't reference $MyRow.FileName directly.
This is probably pretty simple, but I'm new to PowerShell and object-oriented languages. I've tried searching this site, The Scripting Guy's blog, and Google, but I haven't been able to find anything that shows me how to do this.
Any help is much appreciated.
回答1:
It has everything to do with how you populate $MyResult
. If you query the database like
$MyResult = @( << code that returns results from database >> )
that is, enclosing the code that returns your dataset/datatable from the database within @( ... )
, then number of rows returned will be easily checked using $MyResult.count
.
Your original code should work as-is if you populate $MyResult this way.
回答2:
I don't have experience with PS and SQL, but I'll try to provide an answer for you. If you're object $myresult
is a datarow
-object, it means you only got the one row. If the results are empty, then $myresult
will usually be null.
If you get one or more rows, you can put them in an array and count it. However, if your $myresult
are null, and you put it in an array it will still count as one, so we need to watch out for that. Try this:
If ($MyResult -eq $null) {
# do something if no rows
}
Else If (@($MyResult).Count -eq 1) {
# do something else if there are 1 rows.
# The cast to array was only in the if-test,
# so you can reach the object with $myresult.
}
Else {
# do this if there are multiple rows.
}
回答3:
I know this thread is old, but if someone else finds it on Google, this should work also on PS V5:
Replace $MyResult.Count
with: ($MyResult | Measure-Object | select -ExpandProperty Count)
For Example:If (($MyResult | Measure-Object | select -ExpandProperty Count) -eq 0)
回答4:
Looks like this question gets a lot of views, so I wanted to post how I handled this. :)
Basically, the fix for me was to change the method I was using to execute a query on SQL Server. I switched to Chad Miller's Invoke-SqlCmd2 script: TechNet: Invoke-SqlCmd2, i.e.
# ---------------
# this code works
# ---------------
# Register the function
. .\Invoke-Sqlcmd2.ps1
# make SQL Server call & store results to an array, $MyResults
[array]$MyResults = Invoke-Sqlcmd2 -Serve
rInstance "(local)" -Query "SELECT TOP 1 * FROM sys.databases;"
If ($MyResult -eq $null) {
# do something
}
ElseIf ($MyResult.Count -eq 1) {
# do something else
}
Else {
# do this instead
}
来源:https://stackoverflow.com/questions/15395510/returning-a-row-count-for-a-datarow-in-powershell