Josiahwww
 New Member Posts:15

 |
| 18 Oct 2007 11:47 PM |
|
I have the following lines of code in a script.
$Table = New-Object System.Data.DataTable "Users" $Table.Columns.Add("Name", [string])
here is the output. It's outputting the Columns info by default. How do i supress this.
Josiah
|
|
|
|
|
bsonposh
 Basic Member Posts:392

 |
| 19 Oct 2007 12:53 AM |
|
You didnt post any output, but I assume the Add method is returning stuff... just add [Void] in front or pipe to Out-Null $Table = New-Object System.Data.DataTable "Users" [void]$Table.Columns.Add("Name", [string]) or $Table.Columns.Add("Name", [string]) | out-null |
|
Brandon Shell ---------------- Microsoft Powershell MVP https://mvp.support.microsoft.com/profile/Brandon Blog: http://www.bsonposh.com |
|
|
Josiahwww
 New Member Posts:15

 |
| 24 Oct 2007 09:51 PM |
|
It's a bummer that i have to add this [void] to everyline of code that i don't want to output to the console. I'm simply writing a line of code and when the script runs it lists all the properties of the data table even though i didn't tell it to by using write-host. I hope they change this.
CODE: $table = New-Object system.data.datatable "pwdhistory" $table.columns.add("computer", [string]) $table.columns.add("date", [datetime]) $table.columns.add("pwd", [string])
OUTPUT: In my opinion this should not output anything when written as a script. It outputs the following AllowDBNull : True AutoIncrement : False AutoIncrementSeed : 0 AutoIncrementStep : 1 Caption : computer ColumnName : computer Prefix : DataType : System.String DateTimeMode : UnspecifiedLocal DefaultValue : Expression : ExtendedProperties : {} MaxLength : -1 Namespace : Ordinal : 0 ReadOnly : False Table : {} Unique : False ColumnMapping : Element Site : Container : DesignMode : False
AllowDBNull : True AutoIncrement : False AutoIncrementSeed : 0 AutoIncrementStep : 1 Caption : date ColumnName : date Prefix : DataType : System.DateTime DateTimeMode : UnspecifiedLocal DefaultValue : Expression : ExtendedProperties : {} MaxLength : -1 Namespace : Ordinal : 1 ReadOnly : False Table : {} Unique : False ColumnMapping : Element Site : Container : DesignMode : False
AllowDBNull : True AutoIncrement : False AutoIncrementSeed : 0 AutoIncrementStep : 1 Caption : pwd ColumnName : pwd Prefix : DataType : System.String DateTimeMode : UnspecifiedLocal DefaultValue : Expression : ExtendedProperties : {} MaxLength : -1 Namespace : Ordinal : 2 ReadOnly : False Table : {} Unique : False ColumnMapping : Element Site : Container : DesignMode : False |
|
|
|
|
DonJ PowerShell MVP
 Basic Member Posts:134

 |
| 25 Oct 2007 02:52 PM |
|
The "problem" is that the default cmdlet is Out-Default--- remember that it's always at the end of the pipeline. So if a command generates any kind of object output, Out-Default converts them to text. [void] is obviously one solution - I pipe output to Out-Null, 'cuz it's more "normal looking" to me. But regardless, you have to do something - as you know - to "absorb" those output objects. FWIW, you don't typically run into this with cmdlets, because they tend to only generate *useful* objects - but when you're working with the Framework directly, as you are here, then you're putting up with whatever the Framework wants to pump out. |
|
- Don Jones www.ConcentratedTech.com Subscribe (RSS) or visit for weekly PowerShell tips and lessons |
|
|
Josiahwww
 New Member Posts:15

 |
| 25 Oct 2007 08:49 PM |
|
This is going to take some getting used to. I'm really counting on using Powershell and .NET to replace alot of my scripts.
Idea for your next book. Writing Powershell scripts that use .NET. I have a VB script that collects a bunch of stuff from my servers and e-mails me a report in HTML format. I'm trying to port that to Powershell but getting stumped in several areas. One being the issue in this discussion. I know how to program in C# and have programmed a few websites. However I'm self taught and feel sometimes that i'm missing some important concepts in object oriented code.
Anyway, thanks for the feedback.
Josiah |
|
|
|
|
SAPIENScripter
 New Member Posts:45
 |
| 25 Oct 2007 09:00 PM |
|
There truly is a paradigm shift in moving to PowerShell, especially from VBScript. You can usually "convert" an existing VBScript to Powershell, but what you really should be looking at is how to leverage Powershell features like the pipeline. Take your reporting script that emails you a report. You could certainly make this a single PowerShell script. But even better would be to create a function or cmdlet to generate the report, assuming you can't use something that already exists like Get-Wmiobject. The output of that cmdlet can be piped to Exportto-html which gets piped to Out-file which might then get piped to a send-mail cmdlet. There are a few available. This is all of the top of my head and you still might need to massage things a bit with a script, but the more you can leverage cmdlets, or functions that return objects, the better your PowerShell experience will be. Working with "raw" .NET is obviously do-able, but it takes a bit more work to get the output into a workable format as you've seen. |
|
Jeffery Hicks Microsoft PowerShell MVP http://blog.sapien.com http://www.scriptinganswers.com
"Those who forget to script are doomed to repeat their work."
|
|
|
bsonposh
 Basic Member Posts:392

 |
| 26 Oct 2007 02:54 PM |
|
Another option is to wrap the entire thing in @() and pipe to out-null or void @($table = New-Object system.data.datatable "pwdhistory" $table.columns.add("computer", [string]) $table.columns.add("date", [datetime]) $table.columns.add("pwd", [string]) ) | out-null |
|
Brandon Shell ---------------- Microsoft Powershell MVP https://mvp.support.microsoft.com/profile/Brandon Blog: http://www.bsonposh.com |
|
|
DonJ PowerShell MVP
 Basic Member Posts:134

 |
| 26 Oct 2007 02:57 PM |
|
Posted By Josiahwww on 10/25/2007 12:49 PM This is going to take some getting used to. I'm really counting on using Powershell and .NET to replace alot of my scripts.
Idea for your next book. Writing Powershell scripts that use .NET. I have a VB script that collects a bunch of stuff from my servers and e-mails me a report in HTML format. I'm trying to port that to Powershell but getting stumped in several areas. One being the issue in this discussion. I know how to program in C# and have programmed a few websites. However I'm self taught and feel sometimes that i'm missing some important concepts in object oriented code.
Anyway, thanks for the feedback.
Josiah The 2nd edition, which should be out next month, actually has several chapters that utilize .NET classes in PowerShell scripts for various purposes. Hopefully that'll be something you can look at for additional examples.
|
|
- Don Jones www.ConcentratedTech.com Subscribe (RSS) or visit for weekly PowerShell tips and lessons |
|
|
bsonposh
 Basic Member Posts:392

 |
| 26 Oct 2007 03:03 PM |
|
RE: .NET and Powershell, I would recommend browsing the huge number of powershell blogs out there. There are a ton of examples. I have a search you can use if you like http://bsonposh.com/modules/wordpress/?page_id=13 |
|
Brandon Shell ---------------- Microsoft Powershell MVP https://mvp.support.microsoft.com/profile/Brandon Blog: http://www.bsonposh.com |
|
|
Josiahwww
 New Member Posts:15

 |
| 26 Oct 2007 05:39 PM |
|
Thanks again for all great feedback.
Josiah |
|
|
|
|