将批处理文件调用到Powershell脚本中

时间:2018-04-20 07:51:24

标签: powershell

每次我的Powershellscript调用Batchfile时,都会打开一个新的Powershell窗口。如何修改脚本以避免新窗口打开?我试过使用-windowstyle hidden和NoNewWindow,但没有任何效果。

以下是启动我的Powershell脚本的批处理文件:

@echo off

set scriptFileName=%~n0
set scriptFolderPath=%~dp0
set powershellScriptFileName=test.ps1

powershell.exe -windowstyle hidden -Command "Start-Process powershell \"-ExecutionPolicy Bypass -NoProfile -NoExit -Command `\"cd \`\"C:\Temp\`\"; & \`\".\test.ps1\`\"`\"\" -Verb RunAs"

这是我的Powershell脚本:

$Source = "C:\Program Files\Planmeca\Romexis\client\cephmodule\analyses"

$Destination = "\\PRENCIPE-THINK\PDATA\analyses"  #<------ UNC PFAD FUER DEN SERVER ANPASSEN!!
New-Item -ItemType directory -Path $Destination -Force 
Copy-Item -Path $Source\*.* -Destination $Destination -Force -Recurse

(ROBOCOPY $Source $Destination /MIR /W:30 /R:10) 

# Ueberpruefe den Ordner auf veraenderungen
# Fuehre den Script aus bei Aenderung oder Umbennenung

$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = 'C:\Program Files\Planmeca\Romexis\client\cephmodule\analyses'
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $false
$watcher.NotifyFilter = [System.IO.NotifyFilters]::LastWrite -bor [System.IO.NotifyFilters]::FileName

while($TRUE){
$result = $watcher.WaitForChanged([System.IO.WatcherChangeTypes]::Changed -bor [System.IO.WatcherChangeTypes]::Renamed -bOr [System.IO.WatcherChangeTypes]::Created, 1000);
    if($result.TimedOut){
    continue;
}
write-host "Change in " + $result.Name
$A = Start-Process -WindowStyle Hidden -FilePath c:\temp\Bypass.bat -Wait -passthru ;$a.ExitCode
}

1 个答案:

答案 0 :(得分:0)

尝试'-NoNewWindow'开关。我在Windows 7上运行PS版本5.1,它可用。如果您在WMF中运行较早的迭代,则可能需要将计算机更新为5或更高。