我試圖在插入我的Doxie Go掃描儀時自動執行復制和重命名功能。爲了檢測當我將設備連接到我的機器我使用下面的腳本:https://superuser.com/questions/219401/starting-scheduled-task-by-detecting-connection-of-usb-device從另一個腳本啓動一個腳本不起作用
#Requires -version 2.0
Register-WmiEvent -Class win32_VolumeChangeEvent -SourceIdentifier volumeChange
Write-Host (Get-Date -format s) " Beginning script..."
do
{
$newEvent = Wait-Event -SourceIdentifier volumeChange
$eventType = $newEvent.SourceEventArgs.NewEvent.EventType
$eventTypeName = switch($eventType)
{
1 {"Configuration changed"}
2 {"Device arrival"}
3 {"Device removal"}
4 {"docking"}
}
Write-Host (Get-Date -format s) " Event detected = " $eventTypeName
if ($eventType -eq 2)
{
$driveLetter = $newEvent.SourceEventArgs.NewEvent.DriveName
$driveLabel = ([WMI]"Win32_LogicalDisk='$driveLetter'").VolumeName
Write-Host (Get-Date -format s) " Drive name = " $driveLetter
Write-Host (Get-Date -format s) " Drive label = " $driveLabel
# Execute process if drive matches specified condition(s)
if ($driveLabel -eq 'DOXIE')
{
Write-Host (Get-Date -format s) " Starting task in 3 seconds..."
Start-Sleep -seconds 3
Start-Process -FilePath "D:\My Archives\Automation\PowerShell\Batch Move and Rename for Google Cloud.ps1"
}
}
Remove-Event -SourceIdentifier volumeChange
}
while (1-eq1) #Loop until next event
Unregister-Event -SourceIdentifier volumeChange
這按預期工作。我遇到的問題是這行代碼:
Start-Process -FilePath "D:\My Archives\Automation\PowerShell\Batch Move and Rename for Google Cloud.ps1"
運行此行PowerShell的窗口打開的一瞬間當第二然後立即關閉。我知道該腳本需要比運行時間更長的時間,特別是當掃描儀上有數百個文檔時。另外,被調用的腳本工作正常。
我在Windows 10使用PowerShell v5.1.14393.693。任何幫助表示讚賞。謝謝!
謝謝你的幫助!我可以使它工作周圍使用腳本名周圍的論據單引號和雙引號: 開始處理-FilePath PowerShell的-ArgumentList「ExecutionPolicy繞道」,「 - 文件「d:\我的檔案\自動化\ PowerShell的\批量移動並重命名Google Cloud.ps1「' – Tchotchke