2017-03-19 1 views
0

ワークフローの前に定義されている5つの変数がありますが、その方法はわかりません。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 
関連する問題