Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 如何通过同一文件夹中的.vbs文件运行.ps1文件?_Powershell_Vbscript - Fatal编程技术网

Powershell 如何通过同一文件夹中的.vbs文件运行.ps1文件?

Powershell 如何通过同一文件夹中的.vbs文件运行.ps1文件?,powershell,vbscript,Powershell,Vbscript,以下文件位于同一文件夹中。 Testing.cmd Toast notification.ps1 运行[Toast通知].vbs 当我运行Toast notification.ps1时,它会工作。但是,当我运行[Toast notification].vbs时,.ps1文件不会运行 以下是PowerShell脚本: [reflection.assembly]::loadwithpartialname("System.Windows.Forms") [reflection.assembly]::l

以下文件位于同一文件夹中。
Testing.cmd
Toast notification.ps1
运行[Toast通知].vbs

当我运行Toast notification.ps1时,它会工作。但是,当我运行[Toast notification].vbs时,.ps1文件不会运行
以下是PowerShell脚本:

[reflection.assembly]::loadwithpartialname("System.Windows.Forms")
[reflection.assembly]::loadwithpartialname("System.Drawing")
# notify.icon type: Information, Warning or Error.
$notify = new-object system.windows.forms.notifyicon
$notify.icon = [System.Drawing.SystemIcons]::Information
$notify.visible = $true
$notify.showballoontip(10,"", "The CPU is hot.", 
[system.windows.forms.tooltipicon]::None)
$notify.dispose()
以下是VBScript:

Path = split(wscript.scriptFullName, wscript.scriptname)(0)
Item1 = Path & "Toast notification.ps1" 
Item2 = Path & "Testing.cmd" 
Set Action = CreateObject("Wscript.shell")
Action.run ("powershell -executionpolicy bypass -file ""& Item1 &""")
Action.run ("""" & Item2 & ""), 0
双击VBScript文件时,.ps1文件未运行。“路径”可用于运行Testing.cmd,但我无法使其与.ps1文件一起工作。我怎样才能解决这个问题
以下是Testing.cmd文件:

(ncpa.cpl)

运行PowerShell脚本时,它应该更像这样:

Option Explicit

Dim Path : Path = split(wscript.scriptFullName, wscript.scriptname)(0)
Dim Item1 : Item1 = Path & "Toast notification.ps1" 
Dim Item2 : Item2 = Path & "Testing.cmd" 
Dim Action : Set Action = CreateObject("Wscript.shell")
Action.run "powershell -executionpolicy bypass -file " & chr(34) & Item1 & chr(34), 0, true
Action.run "cmd /c " & chr(34) & Item2 & chr(34), 0, true
Set Action = Nothing

如果路径包含空格,则chr(34)表示引号。我还建议进行更多的错误检查,并检查文件是否存在等。上面的示例也隐藏了PowerShell和CMD窗口…

执行这些命令不需要3个文件,只需创建一个vbscript文件即可。 那么,参考,

您可以这样做:

Option Explicit
Dim Ws,Ret,ByPassPSFile,PSFile
Set Ws = CreateObject("wscript.Shell")
ByPassPSFile = "cmd /c PowerShell.exe -ExecutionPolicy bypass -noprofile -file "
Call WritePSFile("Warning","10","'The CPU is hot !'","'The CPU is hot '","'Warning'","10")
Ret = Ws.run(ByPassPSFile & PSFile,0,True)
Ret = Ws.run("cmd /c ncpa.cpl",0,True)
'------------------------------------------------------------------------------------------------------------
Sub WritePSFile(notifyicon,time,title,text,icon,Timeout) 
Const ForWriting = 2
Dim fso,ts,strText
PSFile = Left(Wscript.ScriptFullName, InstrRev(Wscript.ScriptFullName, ".")) & "ps1"
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(PSFile,ForWriting,True)
strText = strText & "[reflection.assembly]::loadwithpartialname('System.Windows.Forms') | Out-Null;" & VbCrlF
strText = strText & "[reflection.assembly]::loadwithpartialname('System.Drawing') | Out-Null;" & VbCrlF 
strText = strText & "$notify = new-object system.windows.forms.notifyicon;" & VbCrlF
strText = strText & "$notify.icon = [System.Drawing.SystemIcons]::"& notifyicon &";" & VbCrlF 
strText = strText & "$notify.visible = $true;" 
strText = strText & "$notify.showballoontip("& time &","& title &","& text &","& icon &");" & VbCrlF 
strText = strText & "Start-Sleep -s " & Timeout &";" & VbCrlF
strText = strText & "$notify.Dispose()"
ts.WriteLine strText
End Sub
'-----------------------------------------------------------------------------------------------------------

还有一个巧妙的技巧,可以使用通用批处理(CMD)启动任何PowerShell脚本。诀窍是按照PowerShell脚本命名批,例如
MyPS\u script.cmd
MyPS\u script.ps1
。它甚至允许通过批处理向脚本传递参数

批次:

@ECHO OFF

REM *****
REM * Wrapper CMD to start the PowerShell script of the same name
REM * I.e. if the script is named ServiceRestart.ps1, then the 
REM * CMD needs to be named ServiceRestart.cmd
REM *****

PowerShell.exe -Command "& '%~dpn0.ps1' %1 %2 %3 %4 %5 %6 %7"

PAUSE
PS脚本演示

ForEach ($Arg In $Args) {
    Write-Host "Arguments from cmd" $Arg
}

请编辑问题,并添加更多详细信息。您使用哪个shell和哪个命令调用vbs脚本?@vonPryz,按您说的做。请重新阅读问题。当我运行“Warning--CPU is hot.cmd”时,我收到以下消息:Warning--CPU is hot.ps1无法加载,因为在此系统上已禁用运行脚本。如果要绕过此限制,请在VBScript示例中执行此操作,相应地编辑批处理文件:
PowerShell.exe-ExecutionPolicy Bypass…
My VBScript不会显示任何命令窗口,但CMD脚本会显示。有办法去掉窗户吗?我不知道。至少没有一些。