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.