Home Variables when running Powershell on cmd line vs ISE
Reply: 0

Variables when running Powershell on cmd line vs ISE

B.Wykes
1#
B.Wykes Published in 2017-12-05 15:02:19Z

This is a block of the code I am using in PowerShell - works perfectly well when run in the ISE, however when called from the command line it loses the value of some of the fields in the Write-Output command:

$csvfile | Group-Object "Employee ID" | ForEach-Object {
    $first_line = "Y"
    $EmployeeID = ($_.Group.'Employee ID' | Select -First 1) 
    if ($_.Group.'Employee ID' -ne "Employee ID") {
        $date = Get-Date -format "yyyyMMdd"
        $DateStr = '{0:yyyyMMdd}' -f $date
        if ($first_line = "Y") {
            $Sum = ($_.Group | Measure-Object 'Journal Amount' -Sum).Sum 
            $TaxSum = (($_.Group | Measure-Object 'Report Entry Total Reclaim Adjusted Amount' -Sum).Sum / 2)
            $SumMinusTax = $Sum - $TaxSum
            $myoutput = "E;{8};INV;;{3};{2};PLE;4;{4};{6};{2};{3};GB01NETBACS;Expenses;GBP;;DMG;;0;;;;;;;;;;;;;;;;;{7};{9}" -f $_.Name, $EmployeeID , $Item.'Employee ID', $DateStr, $Item.'Report Name', ";", $DateStr, $SumMinusTax, $Item.'Branch', $TaxSum 
            write-output $myoutput | out-file -filepath $outputfile -append -encoding ASCII 
            $first_line = "N"
        }
    }
}

The fields that lose the values are any of the ones with the specified field name in them $Item.'Report Name' for example. Calculated values like $TaxSum are fine

I have been trying to find the answer - tried making these global variables, but nothing seems to get these values output ....

Any ideas?


To elaborate a little on this (thanks for the ideas)

Thanks for the ideas - I'm probably missing something very obvious ... I tried the $Item = $_.group - but that didn't work for the ISE run - i got System.Object[] in place of the field i wanted. Here's the full code (aside from the header variables with servers/passwords etc : [code]$outputfile=$files.Fullname + ".output"

$csvfile=import-csv $files
$csvfile | group-object "Employee ID" |
ForEach-Object {
$first_line="Y"
$EmployeeID = ($_.Group.'Employee ID' | Select -First 1) 
if ($_.Group.'Employee ID' -ne "Employee ID")
    {
    write-host "Emp ID : " $EmployeeID
    $date = Get-Date -format "yyyyMMdd"
    $DateStr = '{0:yyyyMMdd}' -f $date
    if ($first_line="Y") 
            {
            $Sum = ($_.Group | Measure-Object 'Journal Amount' -Sum).Sum 
            $TaxSum = (($_.Group | Measure-Object 'Report Entry Total Reclaim Adjusted Amount' -Sum).Sum /2)
            $SumMinusTax=$Sum - $TaxSum
            $datestr_report='{0:yyyyMMdd}' -f $Item.'Batch Date'
            $myoutput="E;{8};INV;;{3};{2};PLE;4;{4};{6};{2};{3};GB01NETBACS;Expenses;GBP;;DMG;;0;;;;;;;;;;;;;;;;;{7};{9}" -f $_.Name, $EmployeeID , $Item.'Employee ID', $DateStr,  $Item.'Report Name', ";", $DateStr, $SumMinusTax,$Item.'Branch',$TaxSum #";" $Sum #$date
            write-output $myoutput | out-file -filepath $outputfile -append -encoding ASCII 
            $first_line="N"
            }
    ForEach ($Item in $_.Group ) {
            $Item = $_.Group
        if ($item.'Journal Account Code' -ne '')
        {
        $tax_line_det="Detail Tax Line : {0}" -f $Item.'Report Entry Total Reclaim Adjusted Amount'
        $taxcode_det="GBNIL"
        if ($Item.'Report Entry Total Reclaim Adjusted Amount' -gt 0)
            {
            $taxcode_det="GB001"
            }
        if ($item.'Journal Account Code' -eq "OHVEHFUEL")
            {
             $veh="ZNA"
            }
        $myoutput_detail="L;5;;0;0;{2};{1};UN;1;{3};{3};0;0;0;;;{4};" -f $Item.'Employee ID',$Item.'Report Entry Description', $item.'Journal Account Code',  $item.'Journal Amount', $taxcode_det
        $myoutput_detail_lc="LC;18L;;;;;BRN;PRD;EMP;VEH;{0};;{1};{4}" -f $Item.'Branch', $Item.'Employee ID', $Item.'Report Entry Total Reclaim Adjusted Amount',$taxcode_det, $veh
        }
}
        # Now need to append the Tax Lines
        $tax_line2=($_.Group | Measure-Object 'Report Entry Tax Reclaim Transaction Amount' -Sum).Sum
        write-host "Tax Line 2 : " $tax_line2
        if ($tax_line2 -gt 0)
            {
            $tax_line1="T;GB001;" + ($_.Group | Measure-Object 'Report Entry Tax Reclaim Transaction Amount' -Sum).Sum 
            }
        if ($tax_line2 = 0)
            {
            $tax_line1="T;GBNIL;" + ($_.Group | Measure-Object 'Report Entry Tax Reclaim Transaction Amount' -Sum).Sum 
            }
            write-output $tax_line1 | out-file -filepath $outputfile -append -encoding ASCII 
}
# Now need to move the file into a 'processed' area
Move-Item $files $processed_dir
Move-Item $outputfile $import_dir[/code]

Is there something in that causing my cmd line call to forget certain fields?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.301101 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO