avatar_PijanySmerf

skrypt powershell

Zaczęty przez PijanySmerf, 23 Listopad 2025, 18:19:50

Poprzedni wątek - Następny wątek

0 użytkowników i 1 Gość przegląda ten wątek.

PijanySmerf

Witam moze ktos pomoże to ogarnac jak wklejam bezposrednio w okno powershell to dziala ale jak uruchomie z prawokliku to juz wali bledami

razor1995

Jakimi błędami? Wklej ich treść.

Masz ustawione:
Set-Executionpolicy UnrestrictedŻeby odblokować uruchamianie skryptów Powershell?

Ewentualnie utwórz sobie np. start.bat z taką treścią:
@echo off
start /wait "" powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0skrypt.ps1" %*

Albo od razu z wywołaniem UAC (jeśli potrzeba admina):
@echo off
powershell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process -FilePath 'powershell.exe' -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File \"%~dp0skrypt.ps1\"' -Verb RunAs"

Oczywiście w folderze powinieneś mieć tylko twój .ps1 i .bat.
Laptop: Lenovo ThinkPad T480s | Intel Core i5 8250U | Intel HD 620 | 24GB RAM Hynix | Lexar NM620 NVME 1TB | Windows 10 Pro
Laptop testowy: Lenovo ThinkPad T430 | Intel Core i7 3740QM | Intel HD 4000 | 16GB RAM Hynix | Samsung 850 Pro 256GB | Windows 7 Pro | Windows XP Pro x64
PC: MSI Z87-G43 | Intel Xeon E3-1240 v3 | nVidia RTX 3060Ti 8GB | 32GB RAM Hynix | GoodRAM PX 500 NVME 512GB | Windows 10 Pro
Mobile: Google Pixel 6 | Google Tensor GS101 @2.8 GHz | Mali-G78 MP20 | 8GB RAM | 128GB MMC | Android 15
Sieć: Cudy WR3000 AX OpenWRT | Huawei HG8010H | Netia 1Gb/s
PlayStation 4 | CUH-1116A | 1TB SSD|

PijanySmerf

At C:\Users\SASIK\Desktop\BT.ps1:60 char:71
+ ...      Write-Host "Bluetooth is in use. Waiting another 30 seconds..."
+                                                                        ~
The string is missing the terminator: ".
At C:\Users\SASIK\Desktop\BT.ps1:54 char:66
+        if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
+                                                                  ~
Missing closing '}' in statement block or type definition.
At C:\Users\SASIK\Desktop\BT.ps1:31 char:45
+    if (-not (Is-BluetoothEnabledAndInUse)) {
+                                            ~
Missing closing '}' in statement block or type definition.
At C:\Users\SASIK\Desktop\BT.ps1:29 char:15
+ while ($true) {
+              ~
Missing closing '}' in statement block or type definition.
    + CategoryInfo          : ParserError: (:) [], ParseException


takie to błedy dodam ze jak otworze okno power shell i tam wkleje ten skrypt i enter to działa bez bledow ale jak chce dwuklikiem albo otworz za pomoca tak sie dzieje a zalezy mi na tym zeby dziaalo bo ma to byc dodane do harmonogramu a bez tego nie zadziała
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

razor1995

Z tego co widzę, brakuje jednej klamry }
Zobacz tak (poprawiam na ślepo, nie chce mi się go uruchamiać):
# ================================
# Bluetooth monitoring script
# ================================

Add-Type -AssemblyName System.Windows.Forms

function Is-BluetoothEnabledAndInUse {
    Write-Host "Checking Bluetooth status..."
   
    $btStatus = Get-Service -Name bthserv -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Status
    if ($btStatus -ne 'Running') {
        Write-Host "Service bthserv is not running."
        return $false
    }

    $btDevices = Get-PnpDevice -Class Bluetooth | Where-Object { $_.Status -eq 'OK' }
    if ($btDevices.Count -gt 0) {
        Write-Host "Bluetooth devices detected and OK."
        return $true
    } else {
        Write-Host "No active Bluetooth devices found."
        return $false
    }
}

Write-Host "Script started. Waiting 30 seconds..."
Start-Sleep -Seconds 30

while ($true) {

    if (-not (Is-BluetoothEnabledAndInUse)) {

        Write-Host "Bluetooth is off or no devices. Stopping services..."

        Stop-Service -Name BluetoothUserService -Force -ErrorAction SilentlyContinue
        Stop-Service -Name BTAGService -Force -ErrorAction SilentlyContinue
        Stop-Service -Name BthAvctpSvc -Force -ErrorAction SilentlyContinue
        sc.exe stop bthserv | Out-Null

        Get-Service | Where-Object { $_.Name -like "DevicesFlowUserSvc*" } |
            ForEach-Object {
                Stop-Service -Name $_.Name -Force -ErrorAction SilentlyContinue
            }

        Write-Host "Stopping finished. Script will now exit."

        $result = [System.Windows.Forms.MessageBox]::Show(
            "Bluetooth and related services have been stopped.",
            "Information",
            [System.Windows.Forms.MessageBoxButtons]::OK,
            [System.Windows.Forms.MessageBoxIcon]::Information
        )

        if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
            Write-Host "OK clicked – exiting script."
            break
        }

    } else {
        Write-Host "Bluetooth is in use. Waiting another 30 seconds..."
    }

    Start-Sleep -Seconds 30
}
Laptop: Lenovo ThinkPad T480s | Intel Core i5 8250U | Intel HD 620 | 24GB RAM Hynix | Lexar NM620 NVME 1TB | Windows 10 Pro
Laptop testowy: Lenovo ThinkPad T430 | Intel Core i7 3740QM | Intel HD 4000 | 16GB RAM Hynix | Samsung 850 Pro 256GB | Windows 7 Pro | Windows XP Pro x64
PC: MSI Z87-G43 | Intel Xeon E3-1240 v3 | nVidia RTX 3060Ti 8GB | 32GB RAM Hynix | GoodRAM PX 500 NVME 512GB | Windows 10 Pro
Mobile: Google Pixel 6 | Google Tensor GS101 @2.8 GHz | Mali-G78 MP20 | 8GB RAM | 128GB MMC | Android 15
Sieć: Cudy WR3000 AX OpenWRT | Huawei HG8010H | Netia 1Gb/s
PlayStation 4 | CUH-1116A | 1TB SSD|

PijanySmerf

Dalej to samo.

At C:\Users\SASIK\Desktop\BT.ps1:60 char:71
+ ...       Write-Host "Bluetooth is in use. Waiting another 30 seconds..."
+                                                                         ~
The string is missing the terminator: ".
At C:\Users\SASIK\Desktop\BT.ps1:54 char:66
+         if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
+                                                                  ~
Missing closing '}' in statement block or type definition.
At C:\Users\SASIK\Desktop\BT.ps1:31 char:45
+     if (-not (Is-BluetoothEnabledAndInUse)) {
+                                             ~
Missing closing '}' in statement block or type definition.
At C:\Users\SASIK\Desktop\BT.ps1:29 char:15
+ while ($true) {
+               ~
Missing closing '}' in statement block or type definition.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

PijanySmerf

Jakiś pomysł?? Ktoś pomoże ?

razor1995

Ja tu już nic nie wykombinuję, jakbym nie zmieniał - wykracza się w tych samych miejscach.

Zobacz tak - przepuściłem go przez GPT:
# ================================
# Bluetooth monitoring script (stable version)
# ================================

Add-Type -AssemblyName System.Windows.Forms

function Is-BluetoothEnabledAndInUse {

    Write-Host "Checking Bluetooth status..."

    try {
        $btStatus = (Get-Service -Name bthserv -ErrorAction Stop).Status
    }
    catch {
        Write-Host "Service bthserv not found or inaccessible."
        return $false
    }

    if ($btStatus -ne 'Running') {
        Write-Host "Bluetooth service is not running."
        return $false
    }

    # Niektóre systemy rzucają błędy przy Get-PnpDevice — zabezpieczamy to
    try {
        $btDevices = Get-PnpDevice -Class Bluetooth -ErrorAction Stop | Where-Object { $_.Status -eq 'OK' }
    }
    catch {
        Write-Host "Could not query Bluetooth devices."
        return $false
    }

    if ($btDevices -and $btDevices.Count -gt 0) {
        Write-Host "Bluetooth devices detected and OK."
        return $true
    } else {
        Write-Host "No active Bluetooth devices found."
        return $false
    }
}

Write-Host "Script started. Waiting 30 seconds..."
Start-Sleep -Seconds 30

while ($true) {

    if (-not (Is-BluetoothEnabledAndInUse)) {

        Write-Host "Bluetooth off or unused. Stopping services..."

        $services = @(
            "BluetoothUserService",
            "BTAGService",
            "BthAvctpSvc"
        )

        foreach ($svc in $services) {
            try {
                Stop-Service -Name $svc -Force -ErrorAction Stop
                Write-Host "Stopped service: $svc"
            } catch {}
        }

        # bthserv wymaga sc.exe
        try {
            sc.exe stop bthserv | Out-Null
            Write-Host "Stopped service: bthserv"
        } catch {}

        # Usługi DevicesFlowUserSvc*
        try {
            Get-Service | Where-Object { $_.Name -like "DevicesFlowUserSvc*" } |
                ForEach-Object {
                    try {
                        Stop-Service -Name $_.Name -Force -ErrorAction Stop
                        Write-Host "Stopped: $($_.Name)"
                    } catch {}
                }
        } catch {}

        Write-Host "Stopping complete. Showing popup..."

        $result = [System.Windows.Forms.MessageBox]::Show(
            "Bluetooth and related services have been stopped.",
            "Information",
            [System.Windows.Forms.MessageBoxButtons]::OK,
            [System.Windows.Forms.MessageBoxIcon]::Information
        )

        if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
            Write-Host "OK clicked – exiting script."
            break
        }

    } else {
        Write-Host "Bluetooth active. Waiting another 30 seconds..."
    }

    Start-Sleep -Seconds 30
}
Laptop: Lenovo ThinkPad T480s | Intel Core i5 8250U | Intel HD 620 | 24GB RAM Hynix | Lexar NM620 NVME 1TB | Windows 10 Pro
Laptop testowy: Lenovo ThinkPad T430 | Intel Core i7 3740QM | Intel HD 4000 | 16GB RAM Hynix | Samsung 850 Pro 256GB | Windows 7 Pro | Windows XP Pro x64
PC: MSI Z87-G43 | Intel Xeon E3-1240 v3 | nVidia RTX 3060Ti 8GB | 32GB RAM Hynix | GoodRAM PX 500 NVME 512GB | Windows 10 Pro
Mobile: Google Pixel 6 | Google Tensor GS101 @2.8 GHz | Mali-G78 MP20 | 8GB RAM | 128GB MMC | Android 15
Sieć: Cudy WR3000 AX OpenWRT | Huawei HG8010H | Netia 1Gb/s
PlayStation 4 | CUH-1116A | 1TB SSD|