You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PowerShell在VPN环境下获取所有活跃远程IP地址及删除远程主机临时文件的技术求助

Hey there! Let's break down your two PowerShell issues step by step—no worries, we've all fumbled through these early on with scripting.

1. Getting Active Remote IP Addresses in a VPN Environment

If you're struggling to pull full, valid remote IPs connected via VPN, here are a few reliable approaches tailored to your setup:

  • Check your VPN adapter's assigned IP (this is the IP you're using on the VPN network):
    First, identify your VPN adapter by name (it usually has "VPN" in the name):

    Get-NetAdapter | Where-Object {$_.Name -match "VPN"}
    

    Then grab its IPv4 address using the adapter name from the output:

    Get-NetIPAddress -InterfaceAlias "Your VPN Adapter Name" -AddressFamily IPv4
    
  • List all established remote connections (great for seeing active IPs you're communicating with over VPN):
    This command filters out local connections and shows unique remote IPs:

    Get-NetTCPConnection | Where-Object {$_.State -eq "Established" -and $_.RemoteAddress -notmatch "^127\.|^192\.168\.|^10\.|^172\.(1[6-9]|2[0-9]|3[0-1])\."} | Select-Object RemoteAddress -Unique
    

    Note: The regex filters out common private/local IP ranges—adjust if your VPN uses a different private subnet.

Common reasons you might not get full valid IPs:

  • Your VPN connection isn't fully established (double-check the adapter status with Get-NetAdapter)
  • You're not filtering out local IPs, so you're seeing your own network instead of VPN remote addresses
  • You don't have permissions to view network connections (run PowerShell as Administrator)
2. Fixing Remote Temp File Deletion Commands

Your current commands are designed for local execution and have a few syntax issues. To delete temp files on a remote computer, you need to use PowerShell's remote execution capabilities. Here's a corrected, reliable approach:

First, Fix Syntax Issues in Local Commands (if you ever need to run this locally)

Your original commands had typos and cmd-style variables—here's the cleaned-up local version:

# Delete temp files for all users
Get-ChildItem "C:\Users" -Directory | ForEach-Object {
    $tempPath = Join-Path $_.FullName "AppData\Local\Temp\*"
    if (Test-Path $tempPath) {
        Remove-Item $tempPath -Recurse -Force -ErrorAction SilentlyContinue
    }
}

# Delete temp files for the current user (already covered above, but included for clarity)
$currentTemp = Join-Path $env:USERPROFILE "AppData\Local\Temp\*"
if (Test-Path $currentTemp) {
    Remove-Item $currentTemp -Recurse -Force -ErrorAction SilentlyContinue
}

Why this works better:

  • Uses Get-ChildItem to safely iterate through user directories (avoids path parsing issues with .\*)
  • Replaces cmd's %UserName% with PowerShell's native $env:USERPROFILE environment variable
  • Adds Test-Path to avoid errors if the temp directory doesn't exist
  • Fixes the typo Set-loaction → Set-Location (though we removed unnecessary directory changes entirely)

Remote Execution (What You Actually Need)

To run this on a remote computer, you'll need:

  1. The remote computer has PowerShell Remoting enabled (Enable-PSRemoting -Force run as Administrator on the remote machine)
  2. You have administrative permissions on the remote computer
  3. Network access to the remote computer over WinRM (port 5985 for HTTP, 5986 for HTTPS)

Here's the remote command:

# Replace "RemotePCNameOrIP" with the actual name/IP of the remote computer
Invoke-Command -ComputerName "RemotePCNameOrIP" -ScriptBlock {
    # Delete temp files for all users
    Get-ChildItem "C:\Users" -Directory | ForEach-Object {
        $tempPath = Join-Path $_.FullName "AppData\Local\Temp\*"
        if (Test-Path $tempPath) {
            Remove-Item $tempPath -Recurse -Force -ErrorAction SilentlyContinue
        }
    }

    # Delete temp files for the user executing the command (optional)
    $currentTemp = Join-Path $env:USERPROFILE "AppData\Local\Temp\*"
    if (Test-Path $currentTemp) {
        Remove-Item $currentTemp -Recurse -Force -ErrorAction SilentlyContinue
    }
} -Credential (Get-Credential)

When you run this, it'll prompt you for the remote computer's admin credentials.


内容的提问来源于stack exchange,提问作者JFlour

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 22:37:47