Jen

人生第一支Power Shell

一陣子沒發文了,最近說忙也不是很忙,時間都安排的恰恰好,到新公司服務也1個半月了,這段期間有很多獲得,也由於一直在 pick up 進度,實在也沒有什麼時間整理一些心得來更新我的網誌,今天是4天連假的第3天,為了不讓網誌過於荒涼就灌水一篇 - 我的第一支Power Shell。其實也沒什麼特別的,就只是想灌水。

故事是這樣的:
我有一些任務是有排程的,並且是分好幾台機器在執行這些任務,然而要在所有的機器執行完這個階段的任務之後,再繼續請下一批的機器們去執行下一個階段的任務,那機器之間是透過網芳來傳遞檔案,所以我們需要在每台機器執行完該階段性的任務後,要去目的地產生flag,代表「我已經完成任務」,而下一階段的機器會持續檢查上一階段的機器們是否有正常完成工作。

以上聽起來很繞舌,但透過power shell ,很輕鬆就能達成任務。比起bat,更有彈性,也更能與.net 結合在一起。


# 檢查Flag
# 取得 Script 檔所在目錄
function Get-ScriptDirectory
{
    $Invocation = (Get-Variable MyInvocation -Scope 1).Value
    Split-Path $Invocation.MyCommand.Path
}
$Flags = 2
$SleepSeconds = 2
$today = Get-Date -Format "yyyyMMdd"
$pt = (Join-Path (Get-ScriptDirectory) $today)

if (-not (Test-Path $pt))
{
    echo $pt
    New-Item -Path $pt -type directory
}

while ((Get-ChildItem $pt).Count -lt $Flags)
{
    echo "wait..."
    Start-Sleep -Seconds $SleepSeconds
}


clear 
$FlagName = "Flag_1" 
$today = Get-Date -Format "yyyyMMdd" 
$flags = 2 
$UNCPathArray = 
  "\\myServier_a\c$\solution\Flags\", 
  "\\myServier_b\c$\solution\Flags\", 
  "\\myServier_c\c$\solution\Flags\", 
  "\\myServier_d\c$\solution\Flags\" 

# echo $UNCPathArray.length 
for ($index = 0; $index -lt ($UNCPathArray.length) ; $index ++) 
{ 
  $UNCPath = $UNCPathArray[$index] + $today + "\" 
  # Flag_n 
  $path = $UNCPath + $FlagName 
  echo $UNCPath 
  if (-not (Test-Path $UNCPath)) 
  { 
    New-Item -Path $UNCPath -type directory 
  } 

  if (-not (Test-Path $path)) 
  { 
    New-Item -Path $path -type file 
    $path2 = $path + "_2" 
    New-Item -Path $path2 -type file 
  } 

  if ((Get-ChildItem $UNCPath).Count -eq $flags) 
  { 
    .(Join-Path ($UNCPathArray[$index]) "Go.bat") 
  } 
}

5 意見:

  匿名

2014年6月17日 下午11:48

We won't even speak about red velvet cake, the sweet potato curry, and eggnog.
You ought to eat at least 3 portions of vegetables per day or even more.
Below are a few suggestions for a diabetes-free lifestyle.



My website - http://diabetesrecipesdietfood.wordpress.com/ (http://www.deadopsarcade.com/profile/negoodrich)

  匿名

2014年6月18日 上午1:56

First of all, wireless home security cameras are cheap.
The password protect features may allow you to access the home
through key, pin, password, or card systems. Some are rather cheap, but
the very best will cost over a hundred dollars and sometimes more than a thousand dollars.


Take a look at my website :: cctv test monitor

  匿名

2014年6月18日 上午2:00

It's difficult to find knowledgeable people in this particular topic, but you
seem like you know what you're talking about! Thanks

Here is my homepage - best weight loss pill

  匿名

2014年6月18日 上午4:06

For the individuals who are technically audio, it may
be a routine procedure but for others it is a tiresome task.
It may consider you a while (It took me about 20 minutes to backup 4GB files).


Feel free to visit my website apptrackr zombieville usa

  匿名

2014年6月19日 上午5:24

Yes! Finally something about psychotherapist.

Also visit my blog post - what is counseling psychology