2

I am using the following code to call a script with a time out:

function getHDriveSize($usersHomeDirectory) {
  $timeOutSeconds = 600

  $code = {
    & powershell.exe $args[0] -Path $args[1]
  }

  $job = Start-Job -ScriptBlock $code -ArgumentList $script:getHDriveSizePath, $usersHomeDirectory

  if (Wait-Job $job -Timeout $timeOutSeconds) {
    Receive-Job $job
  } else {
    'Timed Out'
  }
}

However when $script:getHDriveSizePath has a space in it, I get the following error:

The term 'H:\FolderNameBeforeSpace' is not recognized as the name of a cmdlet,...

I have tried using "'$($args[0])'" (with an escape character before the single quotes) instead of $args[0] but then I get this error:

Unexpected token '-Path' in expression or statement.

The script works fine if $script:getHDriveSizePath contains no spaces.

1 Answer 1

1

Assuming $script:getHDriveSizePath holds the path to a PS1 script, you should change

& powershell.exe $args[0] -Path $args[1]

to

& powershell.exe -File $args[0] -Path $args[1]

If you don't specify the -File argument, PowerShell will assume $args[0] is a PowerShell command. Alternatively, you can do it like:

& powershell.exe "& '$($args[0])'" -Path $args[1]

This is the same issue as discussed here: How to run a PowerShell script?. HTH.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.