I want to run a script that will continue executing even when I close the terminal window.
I use the following command on PowerShell 2.0 to download files from the Internet:
$fyle = New-Object System.Net.WebClient;
$fyle.DownloadFile($url, $dest);
The following script downloads and runs a .ps1 script, and I can close the window, but not immediately since I have to wait for the file to download..
$fyle = New-Object System.Net.WebClient;
$fyle.DownloadFile($url, $dest_filename);
Start-Process cmd -ArgumentList " /c start /min powershell -Exec Bypass $dest_filename" -NoNewWindow
To try to fix that, all I did was put $fyle
inside the Start-Process
block:
$url = "https://google.com";
$dir = "$env:userprofile/adir";
$dest_filename = "$dir/script.ps1";
Start-Process cmd -ArgumentList " /c start powershell -Exec Bypass mkdir $dir; $fyle=New-Object System.Net.WebClient; $fyl.DownloadFile($url,$dest_filename); $dest_filename"
However, I get this error:
The term '=New-Object' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:40 + mkdir C:\Users\IEUser/adir; =New-Object <<<< System.Net.WebClient; .DownloadFile(https://google.com,C:\Users\IEUser/adir/script.ps1); C:\Users\IEUser/adir/script.ps1 + CategoryInfo : ObjectNotFound: (=New-Object:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
The typo in your code aside, if you take a closer look at the statement throwing the error it becomes pretty much apparent what is happening:
+ mkdir C:\Users\IEUser/adir; =New-Object <<<< System.Net.WebClient; .DownloadFile(https://google.com,C:\Users\IEUser/adir/script.ps1); C:\Users\IEUser/adir/script.ps1 ^^ ^^
The variable you intend to use is missing at the positions I indicated above. Without the variable the statements become incomplete commands, thus throwing the error you observed.
The reason for this is that you're passing the arguments for CMD in a double-quoted string. PowerShell's parser expands all variables in that string before passing it to Start-Process
. Undefined variables are expanded to empty strings, leaving you with an incomplete commandline to execute. The variable expansion also leads to another issue with your code, because the variables that serve as the arguments for DownloadFile()
are also expanded before the string is passed to Start-Process
. However, DownloadFile()
expects strings not bare words as its arguments.
You can fix this by escaping the $
in variables you define inside your argument string and adding quotes around the arguments for DownloadFile()
:
Start-Process cmd -ArgumentList " /c start powershell -Exec Bypass mkdir $dir; `$fyle=New-Object System.Net.WebClient; `$fyle.DownloadFile('$url','$dest_filename'); $dest_filename"
# ^ ^ ^ ^ ^ ^
Better yet, define your argument list as an array before passing it to Start-Process
:
$params = '/c', 'start', 'powershell', '-Exec Bypass',
"mkdir $dir;",
'$fyle=New-Object System.Net.WebClient;',
"`$fyle.DownloadFile('$url','$dest_filename');",
"$dest_filename"
Start-Process cmd -ArgumentList $params
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.