使用Powershell从创建的文件夹中执行脚本

时间:2018-11-28 18:32:47

标签: powershell

在powershell中,我可以在一个特定的随机名称创建的文件夹中下载脚本,但是我无法从那里找到执行脚本的正确方法。这是我使用的代码:

$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';

最后一个字符串中缺少某些内容,如果我也使用'+ $ path +',则问题仍然存在。

有什么建议吗?

1 个答案:

答案 0 :(得分:3)

问题是最后两行的单引号。由于您已将$path括在单引号中,因此不会对其进行扩展,而是直接使用它。更改为双引号以扩展变量,这应该可以工作。

$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"
相关问题