none
Why doesn't this script work?

    Soru

  • Hey Scripting Guys!

    I'm new to powershell (been learning it for a couple weeks now...literally) and I'm trying to figure out why the following works:

    $PMT = (Get-ChildItem "\\servername\sharename\PMT Shared" -recurse | Measure-Object -property length -sum)
    $PMTSum = ("{0:N2}" -f ($PMT.sum / 1GB))
    $PMT_Folder = "" | select-object @{Name="Folder"; Expression={"PMT Shared"}},@{Name="Size"; Expression={"$PMTSum"}}

    But this does not:

     

    start-job -scriptblock {$PMT = (Get-ChildItem "\\servername\sharename\PMT Shared" -recurse | Measure-Object -property length -sum)
    $PMTSum = ("{0:N2}" -f ($PMT.sum / 1GB))
    $PMT_Folder = "" | select-object @{Name="Folder"; Expression={"PMT Shared"}},@{Name="Size"; Expression={"$PMTSum"}}}

    The job runs without error, but doesn't populate any of my variables.  I'm testing it in the powershell ISE, when I run the first command, if I type in any of my variables they are correctly populated, but as soon as I turn into into a job it doesn't.

    My understanding of my Frankencode is that the script runs, populates my variables $PMT, $PMTSum, and $PMT_Folder with the values I want, which I could then export to CSV. (using export-csv of course)

    I want to run it as a job, because I have a handful of folders that I want to run the job against concurrently, rather than sequentially, then when the job is completed I can simply run something similar to $Folder1,$Folder2,$Folder3 | Export-CSV folders.csv

    15 Mart 2012 Perşembe 15:16

Yanıtlar

  • Hey Scripting Guys!

    I'm new to powershell (been learning it for a couple weeks now...literally) and I'm trying to figure out why the following works:

    $PMT = (Get-ChildItem "\\servername\sharename\PMT Shared" -recurse | Measure-Object -property length -sum)
    $PMTSum = ("{0:N2}" -f ($PMT.sum / 1GB))
    $PMT_Folder = "" | select-object @{Name="Folder"; Expression={"PMT Shared"}},@{Name="Size"; Expression={"$PMTSum"}}

    But this does not:

     

    start-job -scriptblock {$PMT = (Get-ChildItem "\\servername\sharename\PMT Shared" -recurse | Measure-Object -property length -sum)
    $PMTSum = ("{0:N2}" -f ($PMT.sum / 1GB))
    $PMT_Folder = "" | select-object @{Name="Folder"; Expression={"PMT Shared"}},@{Name="Size"; Expression={"$PMTSum"}}}

    The job runs without error, but doesn't populate any of my variables.  I'm testing it in the powershell ISE, when I run the first command, if I type in any of my variables they are correctly populated, but as soon as I turn into into a job it doesn't.

    My understanding of my Frankencode is that the script runs, populates my variables $PMT, $PMTSum, and $PMT_Folder with the values I want, which I could then export to CSV. (using export-csv of course)

    I want to run it as a job, because I have a handful of folders that I want to run the job against concurrently, rather than sequentially, then when the job is completed I can simply run something similar to $Folder1,$Folder2,$Folder3 | Export-CSV folders.csv

    A job can return data but it cannot return variables to your environment.

    read the job help. It discusses returning data from a job abd various ways to do it.

    If you remove the variable in the last line the results will be available in the job results. (Receive-Job $job)

    $PMT_Folder = "" | select-object @{Name="Folder"; Expression={"PMT Shared"}},@{Name="Size"; Expression={"$PMTSum"}}}

    Remove the left side of teh expression so the results will be passed back.

    select-object @{Name="Folder"; Expression={"PMT Shared"}},@{Name="Size"; Expression={"$PMTSum"}}}

    $job is the result of the start-job command.


    ¯\_(ツ)_/¯

    15 Mart 2012 Perşembe 15:37
  • jrv is correct. The start-job architecture doesn't allow for shared variables and the use of Receive-Job is required to pull the data from the job.

    If you wanted to set up shared variables with a background "job", then you would need to build your own runspace within PowerShell and set up a synchronized hashtable, which would then be supplied to the sessionstate of the runspace and invoke that. It is a more advanced alternative to the start-job approach as you then also need to properly dispose of the runspace and powershell instance that was created to free up resources.


    Boe Prox

    Please remember to mark the best solution as the answer using Mark as Answer. If you find a solution to be helpful, please use Vote as Helpful.

    Looking for a script? Check out the Script Repository
    Need a script written for you? Submit a request at the Script Request Page

    15 Mart 2012 Perşembe 17:16

Tüm Yanıtlar

  • Hey Scripting Guys!

    I'm new to powershell (been learning it for a couple weeks now...literally) and I'm trying to figure out why the following works:

    $PMT = (Get-ChildItem "\\servername\sharename\PMT Shared" -recurse | Measure-Object -property length -sum)
    $PMTSum = ("{0:N2}" -f ($PMT.sum / 1GB))
    $PMT_Folder = "" | select-object @{Name="Folder"; Expression={"PMT Shared"}},@{Name="Size"; Expression={"$PMTSum"}}

    But this does not:

     

    start-job -scriptblock {$PMT = (Get-ChildItem "\\servername\sharename\PMT Shared" -recurse | Measure-Object -property length -sum)
    $PMTSum = ("{0:N2}" -f ($PMT.sum / 1GB))
    $PMT_Folder = "" | select-object @{Name="Folder"; Expression={"PMT Shared"}},@{Name="Size"; Expression={"$PMTSum"}}}

    The job runs without error, but doesn't populate any of my variables.  I'm testing it in the powershell ISE, when I run the first command, if I type in any of my variables they are correctly populated, but as soon as I turn into into a job it doesn't.

    My understanding of my Frankencode is that the script runs, populates my variables $PMT, $PMTSum, and $PMT_Folder with the values I want, which I could then export to CSV. (using export-csv of course)

    I want to run it as a job, because I have a handful of folders that I want to run the job against concurrently, rather than sequentially, then when the job is completed I can simply run something similar to $Folder1,$Folder2,$Folder3 | Export-CSV folders.csv

    A job can return data but it cannot return variables to your environment.

    read the job help. It discusses returning data from a job abd various ways to do it.

    If you remove the variable in the last line the results will be available in the job results. (Receive-Job $job)

    $PMT_Folder = "" | select-object @{Name="Folder"; Expression={"PMT Shared"}},@{Name="Size"; Expression={"$PMTSum"}}}

    Remove the left side of teh expression so the results will be passed back.

    select-object @{Name="Folder"; Expression={"PMT Shared"}},@{Name="Size"; Expression={"$PMTSum"}}}

    $job is the result of the start-job command.


    ¯\_(ツ)_/¯

    15 Mart 2012 Perşembe 15:37
  • jrv is correct. The start-job architecture doesn't allow for shared variables and the use of Receive-Job is required to pull the data from the job.

    If you wanted to set up shared variables with a background "job", then you would need to build your own runspace within PowerShell and set up a synchronized hashtable, which would then be supplied to the sessionstate of the runspace and invoke that. It is a more advanced alternative to the start-job approach as you then also need to properly dispose of the runspace and powershell instance that was created to free up resources.


    Boe Prox

    Please remember to mark the best solution as the answer using Mark as Answer. If you find a solution to be helpful, please use Vote as Helpful.

    Looking for a script? Check out the Script Repository
    Need a script written for you? Submit a request at the Script Request Page

    15 Mart 2012 Perşembe 17:16