Variables to CSV at end of Jobs

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Variables to CSV at end of Jobs

Post by localpct »

I have this simple code that I've been using for a year or so now that collects the data passed through. The problem is once I put it in a Job, it either doesn't run due to

ERROR: You cannot call a method on a null-valued expression.
Test.psf (34, 3): ERROR: At Line: 34 char: 3
ERROR: + [PSCustomObject]@{
ERROR: + ~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : InvalidOperation: (:) [], RuntimeException
ERROR: + FullyQualifiedErrorId : InvokeMethodOnNull
ERROR:

Or it loops twice and I get the correct information, but it's doubled.
  1. [PSCustomObject]@{
  2.                         $getdate = get-date
  3.             PCT         = $env:username;
  4.             Asset       = $env:COMPUTERNAME;
  5.             Date        = $getdate.ToShortDateString();
  6.             Time        = $getdate.ToShortTimeString();
  7.         } | EXPORT-CSV  "\\server\Projects\$($form1.Text)\Usage.csv" -NoTypeInformation -Append
As you can see, it's nothing elegant, but either I'm putting it in the wrong place or something
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Variables to CSV at end of Jobs

Post by jvierra »

The first line of your script is wrong. A key cannot begin with a "$".

A job cannot reference controls on a form.

Code: Select all

$csvfile = "\\server\Projects\$($args[0])\Usage.csv"
[PSCustomObject]@{
    PCT         = $env:username
    Asset       = $env:COMPUTERNAME
    Date        = [datetime]::Now.ToShortDateString()
    Time        = [datetime]::Now.ToShortTimeString()
} | 
EXPORT-CSV  $csvfile -NoTypeInformation -Append
User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Re: Variables to CSV at end of Jobs

Post by localpct »

Ah! Good eye.

Alright so after an hour of digging through my code and realizing some failures. I'm 100% if has to do with this block.
I have this code inside my $assetname_textchanged event
  1. if ($assetname.Text -match '\d{6}' -and $assetname.Text.Length -eq 6)
  2.     {
  3.         $assetname.Text = "PC" + $assetname.Text
  4.         FindIT
  5.        
  6.     }
  7.     elseif  ($assetname.Text -match '[PC]{2}\d{6}' -and $assetname.Text.Length -eq 8)
  8.     {
  9.         FindIT
  10.     }
  11.     else
  12.     {
  13.         return $false
  14.     }
What I was hoping this code would do is allow me to type PC123456 or just 123456 to run the function(FindIT) that determines if the PC is online or not.

If I type PC123456, the form and everything runs normally. If I just type 123456, FindIT runs it twice.

I know this because within my FindIT function, a label text changes from Looking to Found. I noticed it changed twice, so I commented out each section individually and the App ran normally.

So that's where I am.
This topic is 6 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked