首页 > 解决方案 > 使用 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+',最后一个字符串中缺少一些东西可能问题仍然存在。

有什么建议么??

标签: powershell

解决方案


问题是最后两行的单引号。由于您已将其括$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"

推荐阅读