2017-03-19 18 views
0

我有一個工作流程需要提供給工作流程之前定義的五個變量,但我找不到如何去做。使變量在Powershell工作流程內可見

將變量放到工作流中會使它們可見,但這會導致CSV導入問題,這意味着將額外屬性添加到與我不想要的工作流相關的對象。

代碼如下:

$source = 'C:\Users\Koozer\a place\' 
$rotateParams = 90, 90, 270 
$cropParams = @(64, 64), (32, 0) 

$images = Import-Csv "${source}images.csv" 
$colNames = $images[0].psobject.properties.Name 

Workflow StitchCropWorkflow { 

    foreach -parallel ($imageSet in $images) { 
     $magickRotParams = '' 
     $n = 0 

     foreach ($image in $colNames) { 
      $magickRotParams += '`('''+$source+$imageSet.($image)+''' -rotate '+$rotateParams[$n]+' `) ' 
      $n++ 
     } 

     $finfo = [io.fileinfo]$imagePathSets[0] 
     $command = 'magick '+$magickRotParams+' +append -crop '+$cropParams[0][0]+'x'+$cropParams[0][1]+'+'+$cropParams[1][0]+'+'+$cropParams[1][1]+' +repage '''+$finfo.DirectoryName+'\'+$finfo.BaseName+'_stitch_crop'+$finfo.Extension+'''' 
     echo $command 
     Invoke-Expression $command 
    } 
} 

StitchCropWorkflow 

回答

1

您可以將參數傳遞到工作流像你這樣的一個功能做到這一點:如果您在包含工作流中的腳本定義一個變量

$source = 'C:\Users\Koozer\a place\' 
$rotateParams = 90, 90, 270 
$cropParams = @(64, 64), (32, 0) 

$images = Import-Csv "${source}images.csv" 
$colNames = $images[0].psobject.properties.Name 

Workflow StitchCropWorkflow { 
    param (
     $source, 
     $rotateParams, 
     $cropParams, 
     $images, 
     $colNames 
    ) 

    # your code here 
} 

StitchCropWorkflow $source $rotateParams $cropParams $images $colNames 
0

,訪問工作流中的變量語法是$using:variable

作爲示例,下面是一個包含帶單個參數的工作流的powershell腳本。

param([string]$ComputerName) 
$VerbosePreference = "Continue" 
workflow Start-Reboot { 
    param($server) 

     Write-Verbose "Input server is $server" 

     InlineScript{ 
     New-Item -Path C:\Scripts\Logging\Start-Reboot-Single.log -ItemType File -ErrorAction SilentlyContinue | Out-Null 
     } 

      Sequence{ 

      InlineScript 
      { 
       Try 
       { 

    Get-WmiObject -ComputerName $using:server -Class Win32_Service -Filter "Name='HealthService'" | Invoke-WmiMethod -Name PauseService | Out-Null 

    $operatingSystem = Get-WmiObject Win32_OperatingSystem -ComputerName $using:server -ErrorAction stop 
    $LastReboot = [Management.ManagementDateTimeConverter]::ToDateTime($operatingSystem.LastBootUpTime).ToString().Trim() 

    Write-Verbose "Last reboot time for $using:server is $LastReboot" 

    Write-Verbose "Here we restart $using:server, for testing no reboot is done" 

    Restart-Computer -ComputerName $using:server -Wait -For Wmi -Force 

    $OSRecheck = Get-WmiObject Win32_OperatingSystem -ComputerName $using:server -ErrorAction stop 
    $CurrentReboot = [Management.ManagementDateTimeConverter]::ToDateTime($OSRecheck.LastBootUpTime).ToString().Trim() 

    $props = [Ordered]@{ 
     Server=$using:server 
     LastReboot=$LastReboot 
     CurrentReboot=$CurrentReboot 
     } 

      } Catch { 
       Write-Verbose "Oh no, problem with $using:server" 
       $rnd = Get-Random -Minimum 1 -Maximum 5 
       Start-Sleep -Seconds $rnd 
       $err = $_.Exception.GetType().FullName 
        $props = [Ordered]@{ 
        Server=$using:server 
        LastReboot=$err 
        CurrentReboot=$null 
        } 
       } Finally { 

     $object = New-Object -TypeName PSObject -Property $props 

     $random = Get-Random -Minimum 2 -Maximum 15 

     Start-Sleep -Seconds $random 

     Write-Output $object | Out-File -Append -FilePath C:\Scripts\Logging\Start-Reboot-Single.log 
     } 

      }#inline end 

      }#sequence end 

}#end workflow block 
Start-Reboot -server $ComputerName 
相關問題