2013-05-28 48 views
1

標題說,所有我有一個腳本塊的開始工作,而不是執行命令它輸出有關工作的信息。PowerShell啓動作業沒有執行scriptblock

在此被執行的順序如下

$location = $(Get-Location).toString() 

$oldModulePath = $env:PSModulePath 
$env:PSModulePath = $env:PSModulePath + ";" + $location + "\LongitudePowershellModules" 

$env:PSModulePath 

$configXML = [xml](Get-Content $location"\InstallationConfig.XML") 
$config = $configXML.root 

Import-Module CreateComponentsFolder -PassThru 
Import-Module CreateTransferFolder -PassThru 
Import-Module ConfigureDB -PassThru 
Import-Module FastProxyTools -PassThru 
Import-Module WspTools -PassThru 
Import-Module CopySharepointPowershellXML -PassThru 
Import-Module SearchCenterTools -PassThru 
Import-Module ConfigureFAST -PassThru 

# 1 - CreateComponentsFolder 

CreateLongitudeComponentsFolder -currentLocation $location 

並與啓動工作

function CreateLongitudeComponentsFolder 
{ 
    Param(
     [Parameter(Mandatory=$True, Position=1)] 
     [string]$currentLocation 
    ) 

    $scriptBlock = {Write-Host "Copying files to '"C:\Program Files\Ba-insight\Longitude Fast Component"'"` 

      Copy-Item $currentLocation"\Longitude Fast Component" "C:\Program Files\Ba-insight\Longitude Fast Component" -recurs` 
    } 


    Start-Job -ScriptBlock $scriptBlock -ArgumentList $currentLocation -Name "CopyingFiles" 

    $scriptBlock = {$fileAndFolderList = Get-ChildItem $currentLocation"\Longitude Fast Component" -recurs 
     $targetLocationFileAndFolderList = Get-ChildItem "C:\Program Files\Ba-insight\Longitude Fast Component" -recurs 

     $timer = new-object System.Timers.Timer 
     $timer.Interval = 500 #0.5 sec 
      $timer.AutoReset = $true 
     $itemsPresent = $fileAndFolderList | Measure-Object 

     $action = {foreach($item in $fileAndFolderList)` 
      {` 
       if($itemsPresent -ne 0)` 
       {` 
        if($targetLocationFileAndFolderList.IndexOf($item) -ne -1)` 
        {` 
         $itemsPresent = $itemsPresent - 1` 
        }` 
        else` 
        {` 
         $itemsPresent = $fileAndFolderList | Measure-Object` 
        }` 
       }` 
       else` 
       {` 
        $timer.stop()` 
       }` 
      }` 
    } 

    Register-ObjectEvent -InputObject $timer -EventName Elapsed -Action $action | Out-Null 

     $timer.Enabled = $true 
    } 

    Start-Job -ScriptBlock $scriptBlock -ArgumentList $currentLocation -Name "CheckingFiles" 

    Wait-Job -Name "CheckingFiles" 
    Write-Host "All files have been copied." 
} 

我沒有得到任何錯誤,但不是執行命令,它的模塊寫入以下兩個起始作業

HasMoreData : True 
StatusMessage : 
Location  : localhost 
Command  : Write-Host "Copying files to '"C:\Program Files\Ba-insight\Longitude Fast Component"'"` 

          Copy-Item $currentLocation"\Longitude Fast Component" "C:\Program Files\Ba-insight\Longitud 
       e Fast Component" -recurs` 

JobStateInfo : Running 
Finished  : System.Threading.ManualResetEvent 
InstanceId : 70ab6414-0ca4-467e-b283-20057e4141ad 
Id   : 1 
Name   : CopyingFiles 
ChildJobs  : {Job2} 
Output  : {} 
Error   : {} 
Progress  : {} 
Verbose  : {} 
Debug   : {} 
Warning  : {} 
State   : Running 

它可能與如何iw死記硬片腳本,但我無法弄清楚與我見過的所有其他例子有什麼不同。

也有可能有啓動等待用戶輸入使用read-host命令?

編輯:我發現問題,爲什麼工作沒有執行。該參數未被正確傳遞。這裏是我找到解決方案的鏈接。

Passing parameters to start-job

最後一個示例工作,你必須那樣做,即使是單一的參數。

我還有一個問題。我上面寫的東西仍然輸出到控制檯。有什麼辦法可以抑制開始作業cmdlet中的不需要的輸出嗎?

回答

0

我認爲你需要在腳本塊中包含這些參數,以便你可以使用參數列表參數將它傳遞給作業,因爲它代表我相信$currentLocation在腳本塊中將爲空。

因此,你需要這樣的事情在你的腳本塊:

$scriptBlock = { 
    param([string]$currentLocation) 

    $source - Join-Path -Path $currentLocation -ChildPath "Longitude Fast Component" 
    $destination = "C:\Program Files\Ba-insight\Longitude Fast Component" 
    Write-Host "Copying files to [$destination]" 
    Copy-Item $source $destination -recurse 
} 
+0

只是具有相同的結果試了一下。我想我可能需要添加一個參數,但我認爲,如果這是一個問題,我會得到一個空參數錯誤。仍然不能理解爲什麼它將命令作爲字符串輸出而不是執行它們。 – user1752736

+0

剛剛在寫主機後發現了一個續行字符,在一個腳本塊中,您可以簡單地使用換行符作爲下一個命令。 –

+0

這應該說'換行*或*分號' –