使用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.
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 IPv4List 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 -UniqueNote: 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)
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-ChildItemto safely iterate through user directories (avoids path parsing issues with.\*) - Replaces cmd's
%UserName%with PowerShell's native$env:USERPROFILEenvironment variable - Adds
Test-Pathto 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:
- The remote computer has PowerShell Remoting enabled (
Enable-PSRemoting -Forcerun as Administrator on the remote machine) - You have administrative permissions on the remote computer
- 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

