Workflow and InlineScript

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 4 years and 4 months 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
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Workflow and InlineScript

Post by sekou2331 »

Hi,

I am trying to use workflow to run a python script on different machines. I am doing this because each run takes a long time to complete so I need to run it parallel. I am using the script below. It works if I write out the path but I need to be able to check the path before using it. So I am using another function to do this. The only issue is when I use the other function and then use $using: it makes the arg the path for some reason. I looked around but I cant seem to determine if you can use $using: twice in a Inlinescript. This is the error I am getting.
  1. ERROR: C:\Python27\python.exe: can't open file 'TheArg': [Errno 2] No such file or directory
  2. ERROR:     + CategoryInfo          : NotSpecified: (C:\Python27\pyt...le or directory:String) [], RemoteException
  3. ERROR:     + FullyQualifiedErrorId : NativeCommandError
  4. ERROR:     + PSComputerName        : [localhost]
  5. ERROR:
  6. ERROR: C:\Python27\python.exe: can't open file 'TheArg': [Errno 2] No such file or directory
  7. ERROR:     + CategoryInfo          : NotSpecified: (C:\Python27\pyt...le or directory:String) [], RemoteException
  8. ERROR:     + FullyQualifiedErrorId : NativeCommandError
  9. ERROR:     + PSComputerName        : [localhost]
  10. ERROR:
  11. ERROR: C:\Python27\python.exe: can't open file 'TheArg': [Errno 2] No such file or directory
  12. ERROR:     + CategoryInfo          : NotSpecified: (C:\Python27\pyt...le or directory:String) [], RemoteException
  13. ERROR:     + FullyQualifiedErrorId : NativeCommandError
  14. ERROR:     + PSComputerName        : [localhost]
  15. ERROR:
  16. ERROR: C:\Python27\python.exe: can't open file 'TheArg': [Errno 2] No such file or directory
  17. ERROR:     + CategoryInfo          : NotSpecified: (C:\Python27\pyt...le or directory:String) [], RemoteException
  18. ERROR:     + FullyQualifiedErrorId : NativeCommandError
  19. ERROR:     + PSComputerName        : [localhost]

  1. function Get-TruePath {
  2.     param
  3.     (
  4.         [Parameter(Mandatory = $true)]
  5.         $server
  6.     )
  7.     $verifyPathShare = Get-WMIObject -ComputerName ('{0}' -f $server) -Query 'SELECT * FROM Win32_Share' | Where-Object -Property Name -eq 'Share'
  8.     $tritonTSFPath = 'PartofPath\MyFile.py'
  9.     $foundPath = Join-Path -Path $verifyPathShare.Path -ChildPath $tritonTSFPath
  10.    
  11.     $foundPath
  12. }
  13.  
  14.  
  15. workflow foreachptest {
  16.     param ([string[]]$computers)
  17.     foreach –parallel ($computer in $computers) {
  18.         $a = Get-TruePath -server $computer
  19.        
  20.         InlineScript { Invoke-Command -Computer $using:computer -ArgumentList 'TheArg' -Scriptblock { python.exe ${$using:a} $args[0]  } }
  21.        
  22.        
  23.     }
  24.    
  25. }
  26.  
  27. foreachptest -computers 'Computername', 'Computername', 'Computername', 'Computername'
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Workflow and InlineScript

Post by jvierra »

You cannot call an external function in a workflow.

You do not want to check a path like this since it is on the remote computer. The function does not return a valid share name. It returns an object with all share properties and the "Where" does nothing useful.

To get a share path you will need to use both the actual share name and extract the path:

$path = (Get-WmiObject Win32_Share -Filter "Name='sharename'").Path
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: Workflow and InlineScript

Post by sekou2331 »

Ok how do I get the inlinescript to use using twice?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Workflow and InlineScript

Post by jvierra »

I think you should start by fixing all of your workflow. Pass elements to Invoke-Command in the ArgumentList.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Workflow and InlineScript

Post by jvierra »

This would be closer to what you are trying to do.

Code: Select all

workflow foreachptest{
    param (
        [string[]]$computers
    )

    foreach –parallel ($computer in $computers){
        $sharePath = (Get-WMIObject Win32_Share -ComputerName $computer -Filter "Name='Share'").Path + '\PartofPath\MyFile.py'

        InlineScript{
            Invoke-Command -ArgumentList $sharePath,'TheArg' -Scriptblock {python.exe $args[0] $args[1]} -Computer $using:computer 
        }
    }
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Workflow and InlineScript

Post by jvierra »

This would be even more efficient:

Code: Select all

workflow foreachptest{
    param (
        [string[]]$computers
    )

    foreach –parallel ($computer in $computers){
        InlineScript{
            $sharePath = (Get-WMIObject Win32_Share -Filter "Name='ShareName'").Path + '\PartofPath\MyFile.py'
            Invoke-Command -ArgumentList 'TheArg' -Scriptblock {python.exe $sharePath $args[1]} -Computer $using:computer 
        }
    }
}
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: Workflow and InlineScript

Post by sekou2331 »

Hi for the first example I am getting this error:
ERROR: C:\Python27\python.exe: can't open file 'GroupMain': [Errno 2] No such file or directory
ERROR: + CategoryInfo : NotSpecified: (C:\Python27\pyt...le or directory:String) [], RemoteException
ERROR: + FullyQualifiedErrorId : NativeCommandError
ERROR: + PSComputerName : [localhost]
ERROR:


For the second one I am getting nothing. So I changed $args[1] to $args[0] and I get this error.
ERROR: Get-WmiObject : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Provide an argument that is not null or empty, and then
ERROR: try the command again.
foreachptest (6, 6): ERROR: At Line: 6 char: 6
ERROR: +
ERROR: + CategoryInfo : InvalidData: (:) [Get-WmiObject], ParameterBindingValidationException
ERROR: + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetWmiObjectCommand
ERROR: + PSComputerName : [localhost]
ERROR:
ERROR: Get-WmiObject : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Provide an argument that is not null or empty, and then
ERROR: try the command again.

The funny thing is if I use the below it works with no issue but I am unable to check the share path
  1. workflow foreachptest{
  2.     param (
  3.         [string[]]$computers
  4.     )
  5.  
  6.     foreach –parallel ($computer in $computers){
  7.        #$sharePath = (Get-WMIObject Win32_Share -ComputerName $computer -Filter "Name='NAME'").Path + '\PartofPath\MyFile.py'
  8.         InlineScript{
  9.        
  10.             $sharePath = 'D:\PartofPath\MyFile.py'
  11.             Invoke-Command -ArgumentList $sharePath,'TheArg'  -Scriptblock {python.exe $args[0] $args[1]} -Computer $using:computer
  12.         }
  13.     }
  14. }
  15. foreachptest -computers 'Computername1', 'Computername2', 'Computername3', 'Computername4'
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Workflow and InlineScript

Post by jvierra »

Can you please fix you post so the code is properly formatted. I cannot read or copy it correctly.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Workflow and InlineScript

Post by jvierra »

Sorry - I typed the above example incorrectly.

Code: Select all

workflow foreachptest{
    param (
        [string[]]$computers
    )

    foreach –parallel ($computer in $computers){
        InlineScript{
            $sb = {
                $sharePath = (Get-WMIObject Win32_Share -Filter "Name='ShareName'").Path + '\PartofPath\MyFile.py'
                python.exe $sharePath $args[0]
            }
            Invoke-Command -ArgumentList 'TheArg' -Scriptblock $sb -Computer $using:computer 
        }
    }
}
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: Workflow and InlineScript

Post by sekou2331 »

This works Thanks! Sorry about the formatting. Quick question. If I put the server name in a file and use GC it looks like workflow does not run parallel. Is there a way to pass a file full over servers into memory and then hand it to the workflow function. Because I am thinking that GC is handing the server names one at a time instead of all at once.
This topic is 4 years and 4 months 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