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 ”

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

$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

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

4. see unique file extension in all subdirectories

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

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

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

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 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

get-wmiobject win32_networkadapter -filter "netconnectionstatus = 2" |
  select netconnectionid, name, InterfaceIndex, netconnectionstatus

 

 

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

 

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

熱門文章 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 3, 2006

標籤雲 Tag Cloud

amazon CentOS chroot DD-WRT Debian Domino DSM DWA ebook ebook calibre ESXi GCP git google gradle groovy HTML5 IE Java Javascript javscript Jenkins LikeCoin Linux log LotusScript mint MySQL N36L nanoboot nginx PowerShell Qwiklabs responsive route Tomcat VMware vpn vSphere wordpress XPEnology 專欄 網絡資訊 選股 風帆

日曆 Calendar

December 2026
M T W T F S S
  « Feb    
 123456
78910111213
14151617181920
21222324252627
28293031  

Follow Me

Follow Us on RSSFollow Us on TwitterFollow Us on YouTube

文章存檔 Archives

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