HanG321 Blog
Be Shine, Be Smile, Be Wild
  • Home
  • Blog
    • 好文閱讀 readings
    • 生活記事 diary
    • 時事評論 commentary
    • 科技資訊 technology
    • 電腦編程 programming
    • 金融財經 finance
    • 音樂電影 music/movie
  • About
batch file rename by PowerShell

batch file rename by PowerShell

August 3, 2014|PowerShell, rename|電腦編程

1. remove some text on file name of all files: e.g. “TVBOXNOW ”

PowerShell
1
ls | Rename-Item -NewName { $_.name -replace "TVBOXNOW ", "" }

2. move up one level of files in each directory, e.g. c:\folder1\folder2\file3 to c:\folder1\file3

PowerShell
1
2
3
4
5
6
$files = Get-ChildItem -Recurse "c:\folder1" | where {$_.PSIScontainer -eq $false}
foreach ($file in $files)
{
  $destinationFolder = Split-Path -Parent $file.Directory.FullName
  move-item $file.FullName $destinationFolder
}

3. Remove files by extension for all subdirectories: e.g. .url / .nfo

PowerShell
1
Get-ChildItem -File -Include *.url,*.nfo -Recurse | foreach ($_) {Remove-Item $_.FullName}

4. see unique file extension in all subdirectories

PowerShell
1
Get-Childitem -File -Recurse | Select-Object Extension -Unique

5. file name format: xxxx.2013.zzzz, strip out .zzzz.  “-WhatIf” for dry run

PowerShell
1
Get-ChildItem | ForEach-Object -Process { Rename-Item -Path $_.Name -NewName ($_.name -replace '(.*)(\d{4})(.*)$', '$1$2') -WhatIf }

6.  for all pdf or epub file, rename filename as folder name

PowerShell
1
2
Get-ChildItem -Recurse | Where-Object { $_.Extension -eq ".pdf" -or $_.Extension -eq ".epub" -and !$_.Name.StartsWith($_.Directory.Name) } |
  rename-item -newname {$_.Directory.Name + $_.Extension } -WhatIf

 

 

 

Add route after VPN

Add route after VPN

August 2, 2014|PowerShell, route, vpn, 網絡資訊|科技資訊, 電腦編程

For some business reasons, company provides FortiGate SSL VPN to connect office network, but some subnet cannot be accessed due to route did not add automatically.

here is a PowerShell script to find the gateway and interface of VPN connection then add route. It will be great if I can find out how to run script automatically after VPN connection made.

let say office VPN subnet is 10.50.10.0/24, add 2 new subnet by following

add route after SSL VPN
PowerShell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# add route required admin right, this will popup and ask for permission.
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}
 
# find VPN connection gateway IP and interface number
$vpnroute = Get-WmiObject win32_IP4RouteTable | Where-Object { $_.Destination -eq "10.50.10.0" }
$gateway = $vpnroute.NextHop
$interface = $vpnroute.InterfaceIndex
 
route add 10.50.23.0 mask 255.255.255.0 $gateway metric 20 if $interface
route add 10.60.0.0 mask 255.255.0.0 $gateway metric 20 if $interface

FortiGate SSL VPN client is based on PPP, different to Cisco or others as normal network adapter.

ref:
http://serverfault.com/questions/145259/powershell-win32-networkadapterconfiguration-not-seeing-ppp-adapter
http://stackoverflow.com/questions/3293629/script-for-add-route

photo credit: Brent Hensarling
cc

Upgrade to PowerShell 4.0 on Win 7

July 30, 2014|PowerShell|科技資訊

Windows 7 default with PowerShell 2.0, when I downloaded “Windows Management Framework 4.0 “, installed and reboot then update failure,  Error Code: 8004402F .

it is due to temp directory on my system environment pointing to Ram Disk….
Turn off Ram disk, set TEMP back to somewhere with physical disk.

M$, you win…..

solution source here

 

4.0 comes with better support on networking. All I need is just interface Index after SSL VPN has been established, then add route. Haven’t finished that part though.

Here is example to list connected adapters

connected network adapters
PowerShell
1
2
get-wmiobject win32_networkadapter -filter "netconnectionstatus = 2" |
  select netconnectionid, name, InterfaceIndex, netconnectionstatus

 

 

...10...17181920...50...

 

如果你喜歡我的文章,請幫忙按 1-10次 LikeButton 化讚為賞,非常感謝!越喜歡當然可以越按越多 😛

搜尋 Search

簡介 Bio

香港人,現居南十字星空下。

為人貪心,科技、生活、財經、散文 皆有興趣,周身刀冇張利。

思想矛盾,喜歡現在work-life balance 既生活又懷念a city never sleep。

 

每月送我一杯咖啡支持我: liker.land/hang321




分類 Categories

  • 好文閱讀
  • 時事評論
  • 未分類
  • 生活記事
  • 科技資訊
  • 金融財經
  • 電腦編程
  • 音樂電影

文章存檔 Archives




熱門文章 Popular Posts

  • Install XPEnology (DSM) 5.1 on ESXi 6 (HP MicroServer Gen 8)
    Install XPEnology (DSM) 5.1 on ESXi 6 (HP MicroServer Gen 8) June 8, 2015
  • 呢幾日個blogger 有問題….
    呢幾日個blogger 有問題…. October 28, 2004
  • assembly
    assembly February 11, 2006
  • 新工作
    新工作 January 6, 2009
  • 嫁人要嫁工程師
    嫁人要嫁工程師 April 27, 2006

標籤雲 Tag Cloud

CentOS Character chroot Cluster crash cryptography DD-WRT debug Domino DSM Dual Core DWA email ESXi GCP git google HylaFax IE Java Javascript JRE LikeCoin Linux log LotusScript mint MX MySQL nginx PKI PowerShell Qwiklabs srt telent VMware vpn vSphere WinXP wordpress XPEnology 專欄 網絡資訊 選股 風帆

日曆 Calendar

June 2025
M T W T F S S
  « Feb    
 1
2345678
9101112131415
16171819202122
23242526272829
30  

Follow Me

Follow Us on RSSFollow Us on TwitterFollow Us on YouTube

文章存檔 Archives

Copyright © 2004-2021 hang321.net. All Rights Reserved