Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Script works in PS v1, but not in PS v2

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 11-16-2007   #1 (permalink)
MitchM
Guest


 

Script works in PS v1, but not in PS v2

I have a function which builds an array of objects from data in an xml file.
It then returns that array.

In v1, calling the function prints the array contents in the console. In
v2, it just prints a couple of blank lines. I have put write-host statements
throughout the function and see that it is building the array correctly.

If I pipe the output of the function to a script block: % {'Item'}, I see
the correct number of items. If I pipe it to something that uses $_, I don't
see any attributes ($_.ToString()) just prints 'System.Object'. If I send $_
to get-member, I only see the few methods that System.Object provides, but
not the properties I added.

I am not very experienced in Powershell, so maybe I'm doing something wrong.
It did work in v1 however.

The script:

function Get-JobTable
{
param ([string]$machine)

$configPath = "\\" + $machine + "... path to xml file..."

$dom = new-Object System.Xml.XmlDocument
$dom.Load($configPath)

$jobs = @()

$dom.SelectNodes("//JobsToProcess/jobInfo") |
% {
$obj = New-Object System.Object
$obj | add-Member -type noteProperty -name "Machine" -value $machine
$obj | add-Member -type noteProperty -name "Source" -value "config"
$obj | add-Member -type noteProperty -name "JobType" -value
$_.SelectSingleNode("type").get_InnerText()
$obj | add-Member -type noteProperty -name "JobSubType" -value
$_.SelectSingleNode("subType").get_InnerText()
$obj | add-Member -type noteProperty -name "JobClassType" -value
$_.SelectSingleNode("classType").get_InnerText()
$obj | add-Member -type noteProperty -name "JobClassName" -value
$_.SelectSingleNode("className").get_InnerText()
if ($_.SelectNodes("jobParms").Count -gt 0) {
$obj | add-Member -type noteProperty -name "JobParms" -value
$_.SelectSingleNode("jobParms").get_InnerText()
}
else {
$obj | add-Member -type noteProperty -name "JobParms" -value ""
}
if ($_.SelectNodes("maxInstances").Count -gt 0) {
$obj | add-Member -type noteProperty -name "MaxInstances" -value
$_.SelectSingleNode("maxInstances").get_InnerText()
}
else {
$obj | add-Member -type noteProperty -name "MaxInstances" -value ""
}
if ($_.SelectNodes("maxSubjects").Count -gt 0) {
$obj | add-Member -type noteProperty -name "MaxSubjects" -value
$_.SelectSingleNode("maxSubjects").get_InnerText()
}
else {
$obj | add-Member -type noteProperty -name "MaxSubjects" -value ""
}
$jobs = $jobs + $obj
}

$jobs
}



My System SpecsSystem Spec
Old 11-16-2007   #2 (permalink)
Marco Shaw [MVP]
Guest


 

Re: Script works in PS v1, but not in PS v2

MitchM wrote:
Quote:

> I have a function which builds an array of objects from data in an xml file.
> It then returns that array.
>
> In v1, calling the function prints the array contents in the console. In
> v2, it just prints a couple of blank lines. I have put write-host statements
> throughout the function and see that it is building the array correctly.
>
> If I pipe the output of the function to a script block: % {'Item'}, I see
> the correct number of items. If I pipe it to something that uses $_, I don't
> see any attributes ($_.ToString()) just prints 'System.Object'. If I send $_
> to get-member, I only see the few methods that System.Object provides, but
> not the properties I added.
>
> I am not very experienced in Powershell, so maybe I'm doing something wrong.
> It did work in v1 however.
>
> The script:
>
> function Get-JobTable
> {
> param ([string]$machine)
>
> $configPath = "\\" + $machine + "... path to xml file..."
>
> $dom = new-Object System.Xml.XmlDocument
> $dom.Load($configPath)
>
> $jobs = @()
>
> $dom.SelectNodes("//JobsToProcess/jobInfo") |
> % {
> $obj = New-Object System.Object
> $obj | add-Member -type noteProperty -name "Machine" -value $machine
> $obj | add-Member -type noteProperty -name "Source" -value "config"
> $obj | add-Member -type noteProperty -name "JobType" -value
> $_.SelectSingleNode("type").get_InnerText()
> $obj | add-Member -type noteProperty -name "JobSubType" -value
> $_.SelectSingleNode("subType").get_InnerText()
> $obj | add-Member -type noteProperty -name "JobClassType" -value
> $_.SelectSingleNode("classType").get_InnerText()
> $obj | add-Member -type noteProperty -name "JobClassName" -value
> $_.SelectSingleNode("className").get_InnerText()
> if ($_.SelectNodes("jobParms").Count -gt 0) {
> $obj | add-Member -type noteProperty -name "JobParms" -value
> $_.SelectSingleNode("jobParms").get_InnerText()
> }
> else {
> $obj | add-Member -type noteProperty -name "JobParms" -value ""
> }
> if ($_.SelectNodes("maxInstances").Count -gt 0) {
> $obj | add-Member -type noteProperty -name "MaxInstances" -value
> $_.SelectSingleNode("maxInstances").get_InnerText()
> }
> else {
> $obj | add-Member -type noteProperty -name "MaxInstances" -value ""
> }
> if ($_.SelectNodes("maxSubjects").Count -gt 0) {
> $obj | add-Member -type noteProperty -name "MaxSubjects" -value
> $_.SelectSingleNode("maxSubjects").get_InnerText()
> }
> else {
> $obj | add-Member -type noteProperty -name "MaxSubjects" -value ""
> }
> $jobs = $jobs + $obj
> }
>
> $jobs
> }
>
>
See also "CTP feedback: short circuit evaluation and XML" from November
6th. There may be some XML-related issues in the v2 CTP.

Please report anything related to the v2 CTP with the subject prefix
"CTP" (source:
http://blogs.msdn.com/powershell/arc...shell-2-0.aspx).

Marco

--
Microsoft MVP - Windows PowerShell
http://www.microsoft.com/mvp

PowerGadgets MVP
http://www.powergadgets.com/mvp

Blog:
http://marcoshaw.blogspot.com
My System SpecsSystem Spec
Old 11-16-2007   #3 (permalink)
MitchM
Guest


 

Re: Script works in PS v1, but not in PS v2

Thanks, I will look this over when I have time.

Mitch

"Marco Shaw [MVP]" wrote:
Quote:

> MitchM wrote:
Quote:

> > I have a function which builds an array of objects from data in an xml file.
> > It then returns that array.
> >
> > In v1, calling the function prints the array contents in the console. In
> > v2, it just prints a couple of blank lines. I have put write-host statements
> > throughout the function and see that it is building the array correctly.
> >
> > If I pipe the output of the function to a script block: % {'Item'}, I see
> > the correct number of items. If I pipe it to something that uses $_, I don't
> > see any attributes ($_.ToString()) just prints 'System.Object'. If I send $_
> > to get-member, I only see the few methods that System.Object provides, but
> > not the properties I added.
> >
> > I am not very experienced in Powershell, so maybe I'm doing something wrong.
> > It did work in v1 however.
> >
> > The script:
> >
> > function Get-JobTable
> > {
> > param ([string]$machine)
> >
> > $configPath = "\\" + $machine + "... path to xml file..."
> >
> > $dom = new-Object System.Xml.XmlDocument
> > $dom.Load($configPath)
> >
> > $jobs = @()
> >
> > $dom.SelectNodes("//JobsToProcess/jobInfo") |
> > % {
> > $obj = New-Object System.Object
> > $obj | add-Member -type noteProperty -name "Machine" -value $machine
> > $obj | add-Member -type noteProperty -name "Source" -value "config"
> > $obj | add-Member -type noteProperty -name "JobType" -value
> > $_.SelectSingleNode("type").get_InnerText()
> > $obj | add-Member -type noteProperty -name "JobSubType" -value
> > $_.SelectSingleNode("subType").get_InnerText()
> > $obj | add-Member -type noteProperty -name "JobClassType" -value
> > $_.SelectSingleNode("classType").get_InnerText()
> > $obj | add-Member -type noteProperty -name "JobClassName" -value
> > $_.SelectSingleNode("className").get_InnerText()
> > if ($_.SelectNodes("jobParms").Count -gt 0) {
> > $obj | add-Member -type noteProperty -name "JobParms" -value
> > $_.SelectSingleNode("jobParms").get_InnerText()
> > }
> > else {
> > $obj | add-Member -type noteProperty -name "JobParms" -value ""
> > }
> > if ($_.SelectNodes("maxInstances").Count -gt 0) {
> > $obj | add-Member -type noteProperty -name "MaxInstances" -value
> > $_.SelectSingleNode("maxInstances").get_InnerText()
> > }
> > else {
> > $obj | add-Member -type noteProperty -name "MaxInstances" -value ""
> > }
> > if ($_.SelectNodes("maxSubjects").Count -gt 0) {
> > $obj | add-Member -type noteProperty -name "MaxSubjects" -value
> > $_.SelectSingleNode("maxSubjects").get_InnerText()
> > }
> > else {
> > $obj | add-Member -type noteProperty -name "MaxSubjects" -value ""
> > }
> > $jobs = $jobs + $obj
> > }
> >
> > $jobs
> > }
> >
> >
>
> See also "CTP feedback: short circuit evaluation and XML" from November
> 6th. There may be some XML-related issues in the v2 CTP.
>
> Please report anything related to the v2 CTP with the subject prefix
> "CTP" (source:
> http://blogs.msdn.com/powershell/arc...shell-2-0.aspx).
>
> Marco
>
> --
> Microsoft MVP - Windows PowerShell
> http://www.microsoft.com/mvp
>
> PowerGadgets MVP
> http://www.powergadgets.com/mvp
>
> Blog:
> http://marcoshaw.blogspot.com
>
My System SpecsSystem Spec
Old 11-19-2007   #4 (permalink)
Lee Holmes [MSFT]
Guest


 

Re: Script works in PS v1, but not in PS v2

Could you please share out a sample (trimmed down) XML file that
demonstrates the issue?

--
Lee Holmes [MSFT]
Windows PowerShell Development
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.


"MitchM" <MitchM@xxxxxx> wrote in message
news:AE84953F-C55A-49CF-B7F6-2C0A033AAA1F@xxxxxx
Quote:

> Thanks, I will look this over when I have time.
>
> Mitch
>
> "Marco Shaw [MVP]" wrote:
>
Quote:

>> MitchM wrote:
Quote:

>> > I have a function which builds an array of objects from data in an xml
>> > file.
>> > It then returns that array.
>> >
>> > In v1, calling the function prints the array contents in the console.
>> > In
>> > v2, it just prints a couple of blank lines. I have put write-host
>> > statements
>> > throughout the function and see that it is building the array
>> > correctly.
>> >
>> > If I pipe the output of the function to a script block: % {'Item'}, I
>> > see
>> > the correct number of items. If I pipe it to something that uses $_, I
>> > don't
>> > see any attributes ($_.ToString()) just prints 'System.Object'. If I
>> > send $_
>> > to get-member, I only see the few methods that System.Object provides,
>> > but
>> > not the properties I added.
>> >
>> > I am not very experienced in Powershell, so maybe I'm doing something
>> > wrong.
>> > It did work in v1 however.
>> >
>> > The script:
>> >
>> > function Get-JobTable
>> > {
>> > param ([string]$machine)
>> >
>> > $configPath = "\\" + $machine + "... path to xml file..."
>> >
>> > $dom = new-Object System.Xml.XmlDocument
>> > $dom.Load($configPath)
>> >
>> > $jobs = @()
>> >
>> > $dom.SelectNodes("//JobsToProcess/jobInfo") |
>> > % {
>> > $obj = New-Object System.Object
>> > $obj | add-Member -type noteProperty -name "Machine" -value
>> > $machine
>> > $obj | add-Member -type noteProperty -name "Source" -value "config"
>> > $obj | add-Member -type noteProperty -name "JobType" -value
>> > $_.SelectSingleNode("type").get_InnerText()
>> > $obj | add-Member -type noteProperty -name "JobSubType" -value
>> > $_.SelectSingleNode("subType").get_InnerText()
>> > $obj | add-Member -type noteProperty -name "JobClassType" -value
>> > $_.SelectSingleNode("classType").get_InnerText()
>> > $obj | add-Member -type noteProperty -name "JobClassName" -value
>> > $_.SelectSingleNode("className").get_InnerText()
>> > if ($_.SelectNodes("jobParms").Count -gt 0) {
>> > $obj | add-Member -type noteProperty -name "JobParms" -value
>> > $_.SelectSingleNode("jobParms").get_InnerText()
>> > }
>> > else {
>> > $obj | add-Member -type noteProperty -name "JobParms" -value ""
>> > }
>> > if ($_.SelectNodes("maxInstances").Count -gt 0) {
>> > $obj | add-Member -type noteProperty -name
>> > "MaxInstances" -value
>> > $_.SelectSingleNode("maxInstances").get_InnerText()
>> > }
>> > else {
>> > $obj | add-Member -type noteProperty -name
>> > "MaxInstances" -value ""
>> > }
>> > if ($_.SelectNodes("maxSubjects").Count -gt 0) {
>> > $obj | add-Member -type noteProperty -name "MaxSubjects" -value
>> > $_.SelectSingleNode("maxSubjects").get_InnerText()
>> > }
>> > else {
>> > $obj | add-Member -type noteProperty -name "MaxSubjects" -value
>> > ""
>> > }
>> > $jobs = $jobs + $obj
>> > }
>> >
>> > $jobs
>> > }
>> >
>> >
>>
>> See also "CTP feedback: short circuit evaluation and XML" from November
>> 6th. There may be some XML-related issues in the v2 CTP.
>>
>> Please report anything related to the v2 CTP with the subject prefix
>> "CTP" (source:
>> http://blogs.msdn.com/powershell/arc...shell-2-0.aspx).
>>
>> Marco
>>
>> --
>> Microsoft MVP - Windows PowerShell
>> http://www.microsoft.com/mvp
>>
>> PowerGadgets MVP
>> http://www.powergadgets.com/mvp
>>
>> Blog:
>> http://marcoshaw.blogspot.com
>>
My System SpecsSystem Spec
Old 11-21-2007   #5 (permalink)
MitchM
Guest


 

Re: Script works in PS v1, but not in PS v2

OK, here's a sample file I've tested (trimmed down.) It's just a config file
for a .NET program. Thanks.

<configuration>
<configSections>
<section name="JobsToProcess"
type="Fares.Mit.Configuration.ElementNormalCfgHandler, Fares.Mit.Support" />
</configSections>
<JobsToProcess>
<jobInfo>
<type>TYPE1</type>
<subType>SUBTYPE1</subType>
<classType>RT</classType>
<className>Class,Assembly</className>
</jobInfo>
<jobInfo>
<type>TYPE2</type>
<subType>SUBTYPE2</subType>
<classType>S</classType>
<className>Class,Assembly</className>
</jobInfo>
<jobInfo>
<type>TYPE3</type>
<subType>SUBTYPE3</subType>
<classType>S</classType>
<className>Class,Assembly</className>
</jobInfo>
</JobsToProc



"Lee Holmes [MSFT]" wrote:
Quote:

> Could you please share out a sample (trimmed down) XML file that
> demonstrates the issue?
>
> --
> Lee Holmes [MSFT]
> Windows PowerShell Development
> Microsoft Corporation
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
Quote:
Quote:

> >> MitchM wrote:
> >> > I have a function which builds an array of objects from data in an xml
> >> > file.
> >> > It then returns that array.
> >> >
> >> > In v1, calling the function prints the array contents in the console.
> >> > In
> >> > v2, it just prints a couple of blank lines. I have put write-host
> >> > statements
> >> > throughout the function and see that it is building the array
> >> > correctly.
> >> >
> >> > If I pipe the output of the function to a script block: % {'Item'}, I
> >> > see
> >> > the correct number of items. If I pipe it to something that uses $_, I
> >> > don't
> >> > see any attributes ($_.ToString()) just prints 'System.Object'. If I
> >> > send $_
> >> > to get-member, I only see the few methods that System.Object provides,
> >> > but
> >> > not the properties I added.
> >> >
> >> > I am not very experienced in Powershell, so maybe I'm doing something
> >> > wrong.
> >> > It did work in v1 however.
> >> >
> >> > The script:
> >> >
> >> > function Get-JobTable
> >> > {
> >> > param ([string]$machine)
> >> >
> >> > $configPath = "\\" + $machine + "... path to xml file..."
> >> >
> >> > $dom = new-Object System.Xml.XmlDocument
> >> > $dom.Load($configPath)
> >> >
> >> > $jobs = @()
> >> >
> >> > $dom.SelectNodes("//JobsToProcess/jobInfo") |
> >> > % {
> >> > $obj = New-Object System.Object
> >> > $obj | add-Member -type noteProperty -name "Machine" -value
> >> > $machine
> >> > $obj | add-Member -type noteProperty -name "Source" -value "config"
> >> > $obj | add-Member -type noteProperty -name "JobType" -value
> >> > $_.SelectSingleNode("type").get_InnerText()
> >> > $obj | add-Member -type noteProperty -name "JobSubType" -value
> >> > $_.SelectSingleNode("subType").get_InnerText()
> >> > $obj | add-Member -type noteProperty -name "JobClassType" -value
> >> > $_.SelectSingleNode("classType").get_InnerText()
> >> > $obj | add-Member -type noteProperty -name "JobClassName" -value
> >> > $_.SelectSingleNode("className").get_InnerText()
> >> > if ($_.SelectNodes("jobParms").Count -gt 0) {
> >> > $obj | add-Member -type noteProperty -name "JobParms" -value
> >> > $_.SelectSingleNode("jobParms").get_InnerText()
> >> > }
> >> > else {
> >> > $obj | add-Member -type noteProperty -name "JobParms" -value ""
> >> > }
> >> > if ($_.SelectNodes("maxInstances").Count -gt 0) {
> >> > $obj | add-Member -type noteProperty -name
> >> > "MaxInstances" -value
> >> > $_.SelectSingleNode("maxInstances").get_InnerText()
> >> > }
> >> > else {
> >> > $obj | add-Member -type noteProperty -name
> >> > "MaxInstances" -value ""
> >> > }
> >> > if ($_.SelectNodes("maxSubjects").Count -gt 0) {
> >> > $obj | add-Member -type noteProperty -name "MaxSubjects" -value
> >> > $_.SelectSingleNode("maxSubjects").get_InnerText()
> >> > }
> >> > else {
> >> > $obj | add-Member -type noteProperty -name "MaxSubjects" -value
> >> > ""
> >> > }
> >> > $jobs = $jobs + $obj
> >> > }
> >> >
> >> > $jobs
> >> > }
> >> >
> >> >
My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Include another script, keep variables in included script? pschmidt PowerShell 32 08-18-2008 01:48 PM
My command works in the PS prompt but not in a script (.ps1) / reg Kari PowerShell 15 07-31-2008 08:25 AM
Howcome something works on the commandline but not when i put thesame commands in a script? Gobba PowerShell 3 06-03-2008 07:48 AM
Script file has 'OS Handle' error when run from script Jay PowerShell 1 09-18-2007 07:06 PM
measure-object works interactively but not in script Frank PowerShell 1 02-26-2007 01:11 PM


Update your Vista Drivers Update Your Vista Drivers Now!!

Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008
Page generated in 0.40849 seconds with 10 queries